如何调用系统拍照并获取图片

如何调用系统拍照并获取图片

HarmonyOS
2024-06-12 00:01:51
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
anlan001

首先构造一个单例对象:

import common from '@ohos.app.ability.common'; 
 
// 构造单例对象 
export class GlobalThis { 
  private constructor() { 
  } 
 
  private static instance: GlobalThis; 
  private _uiContexts = new Map<string, common.UIAbilityContext>(); 
 
  public static getInstance(): GlobalThis { 
    if (!GlobalThis.instance) { 
      GlobalThis.instance = new GlobalThis(); 
    } 
    return GlobalThis.instance; 
  } 
 
  getContext(key: string): common.UIAbilityContext | undefined { 
    return this._uiContexts.get(key); 
  } 
 
  setContext(key: string, value: common.UIAbilityContext): void { 
    this._uiContexts.set(key, value); 
  } 
}

然后对EntryAbility.ets内的onCreate函数进行如下修改:

import { GlobalThis } from '../utils/GlobalThis' 
import { AbilityConstant, Want } from '@kit.AbilityKit'; 
import { hilog } from '@kit.PerformanceAnalysisKit'; 
 
export default class EntryAbility extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 
    GlobalThis.getInstance().setContext("context", this.context) 
  } 
  // ...... 
}

最后构建一个类,实现三方应用拉起系统相机并返回图像路径。

import wantConstant from '@ohos.ability.wantConstant'; 
import { GlobalThis } from './GlobalThis'; 
import Want from '@ohos.app.ability.Want'; 
import common from '@ohos.app.ability.common'; 
 
export default class ThirdApp { 
  constructor() { 
 
  } 
 
  private context: common.UIAbilityContext | undefined = GlobalThis.getInstance().getContext("context"); 
  private savePath: string = getContext().filesDir; 
 
  public async thirdPartyCall(type: string, supportMultiMode: boolean): Promise<common.AbilityResult | undefined> { 
 
    console.log("thirdPartyCall " + type) 
    let imagePathSrc: string; 
    let actionName = type === "image" ? wantConstant.Action.ACTION_IMAGE_CAPTURE : wantConstant.Action.ACTION_VIDEO_CAPTURE // 拍照or录像? 
    console.log("thirdPartyCall actionName=" + actionName) // ohos.want.action.imageCapture 
    console.log("thirdPartyCall savaPath=" + this.savePath) // ohos.want.action.imageCapture 
    let want: Want = { 
      "action": actionName, 
      "parameters": { 
        supportMultiMode: supportMultiMode, 
        callBundleName: "com.example.systemcapturegetphoto" 
      } 
    }; 
    if (this.context) { 
      let result: common.AbilityResult = await this.context.startAbilityForResult(want); 
      let params = result?.want?.parameters as Record<string, string | number> 
      imagePathSrc = params?.resourceUri as string; 
      console.info('Operation imagePathSrc= ' + imagePathSrc); 
      return result; 
    } 
    return undefined; 
  } 
}
分享
微博
QQ
微信
回复
2024-06-13 00:10:34
相关问题
如何调用系统相机拍照
1701浏览 • 1回复 待解决
如何调用系统相机,拍照、录视频?
397浏览 • 1回复 待解决
如何获取拍照后的图片地址
1992浏览 • 1回复 待解决
有哪位知道怎样调用系统相机拍照
1881浏览 • 1回复 待解决
HarmonyOS 相册,相机拍照裁剪
92浏览 • 1回复 待解决
获取网络图片保存到相册
1534浏览 • 1回复 待解决
HarmonyOS 调用相机拍照问题
26浏览 • 1回复 待解决