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

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

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
相关问题
如何调用系统相机拍照
517浏览 • 1回复 待解决
如何获取拍照后的图片地址
706浏览 • 1回复 待解决
有哪位知道怎样调用系统相机拍照
631浏览 • 1回复 待解决
获取网络图片保存到相册
404浏览 • 1回复 待解决
harmonyOS基于api9如何调用相机拍照
2408浏览 • 1回复 待解决
如何调用测试一个wifi接口?
4789浏览 • 1回复 待解决
text内容如何实时获取添加修改?
1899浏览 • 1回复 待解决
复制图片到剪切板获取剪切板消息
389浏览 • 1回复 待解决
如何将so文件导入,完成调用
670浏览 • 1回复 待解决
关于如何使用相机拍照模块拍照问题
654浏览 • 0回复 待解决
进入相册或拍照选择图片做头像
12158浏览 • 2回复 已解决
怎样让用户选择加载图片
7498浏览 • 1回复 待解决
base64字符串如何转为图片保存
1068浏览 • 1回复 待解决