拍照与图片编辑,应用中可以直接唤起并调用系统的拍照以及图片编辑的功能页面吗?

应用中可以直接唤起并调用系统的拍照以及图片编辑的功能页面吗?

HarmonyOS
2024-11-13 11:49:17
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

配置module.json5声明使用相机权限。

"requestPermissions": [ 
{ 
  "name": 'ohos.permission.CAMERA' 
} 
],
import camera from '@ohos.multimedia.camera' 
import { BusinessError } from '@ohos.base' 
import common from '@ohos.app.ability.common'; 
import abilityAccessCtrl, { Context, PermissionRequestResult, Permissions } from '@ohos.abilityAccessCtrl'; 
 
@Entry 
@Component 
struct Index { 
  private surfaceId : string ='' 
  xcomponentController: XComponentController = new XComponentController() 
 
  onPageShow(){ 
    //调用授权接口 
    this.requestCameraPermission(); 
  } 
 
  //拉起授权窗口 
  requestCameraPermission():void{ 
    let permission:Permissions[] = ['ohos.permission.CAMERA']; 
    let  context:Context = getContext(this) as Context; 
    let atManager:abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 
    atManager.requestPermissionsFromUser(context,permission).then((data:PermissionRequestResult) =>{ 
      let grantStatus: Array<number> = data.authResults; 
      let length: number = grantStatus.length; 
      for (let i = 0; i < length; i++) { 
        if (grantStatus[i] != 0) { 
          // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限 
          return; 
        } 
      } 
      // 授权成功 
    }).catch((err: BusinessError) => { 
      console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`); 
    }) 
  } 
  build() { 
    Column(){ 
      Row() { 
        XComponent({ 
          id: 'xcomponent', 
          type: XComponentType.SURFACE, 
          controller: this.xcomponentController 
        }) 
          .onLoad(() => { 
            this.xcomponentController.setXComponentSurfaceSize({surfaceWidth:1920,surfaceHeight:1080}); 
            this.surfaceId = this.xcomponentController.getXComponentSurfaceId() 
          }) 
          .width('720px') 
          .height('480px') 
      } 
      .backgroundColor(Color.Black) 
      // .position({x: 0, y: 48}) 
      Button('开始预览') 
        .onClick(()=>{ 
          previewCamera(getContext(this),this.surfaceId); 
 
        }) 
    } 
 
  } 
} 
async function previewCamera(baseContext:common.BaseContext,surfaceId:string):Promise<void>{ 
  //获取CameraManager 
  let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext); 
 
  //获取相机列表 
  let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 
 
  //创建相机输入流 
  let cameraInput: camera.CameraInput | undefined = undefined; 
  cameraInput = cameraManager.createCameraInput(cameraArray[0]); 
 
  //打开相机 
  await cameraInput.open(); 
 
  //获取相机设备支持的输出流能力 
  let cameraOutputCap :camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraArray[0]); 
  let previewProfilesArray: Array<camera.Profile> = cameraOutputCap.previewProfiles; 
 
  //创建预览输出流 
  let previewOutput:camera.PreviewOutput |undefined = undefined; 
  previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0],surfaceId); 
 
  //创建会话 
  let captureSession :camera.CaptureSession |undefined = undefined; 
  captureSession = cameraManager.createCaptureSession(); 
 
  //开始配置会话 
  captureSession.beginConfig(); 
 
  // 向会话中添加相机输入流 
  captureSession.addInput(cameraInput); 
 
  // 向会话中添加预览输出流 
  captureSession.addOutput(previewOutput); 
 
  // 提交会话配置 
  await captureSession.commitConfig(); 
  // 启动会话 
  await captureSession.start(); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.

以上是配置权限和相机的代码。图片工具可以参考这个链接:​https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/image-tool-V5​。api提供拍照和图片编辑的能力。

分享
微博
QQ
微信
回复
2024-11-13 19:08:51
相关问题
如何调用系统拍照获取图片
1446浏览 • 1回复 待解决
图片视频编辑控件问题
1081浏览 • 1回复 待解决
HarmonyOS 如何实现图片编辑功能
934浏览 • 1回复 待解决
如何编辑裁剪相册图片
1634浏览 • 1回复 待解决
HarmonyOS 能否跳转到图片编辑功能
518浏览 • 1回复 待解决
图片编辑-如何添加多个贴纸功能
938浏览 • 0回复 待解决
HarmonyOS 本地图库选择器编辑图片
717浏览 • 1回复 待解决
如何调用系统相机拍照
2502浏览 • 1回复 待解决