HarmonyOS自定义相机横向拍照,摄像头方向差了90度

在开发自定义相机拍照功能时,因为需要横向拍照,所以将界面横向设置之后,发现摄像头看到的画面是差了90度,请问要怎么设置这个摄像头方向?

HarmonyOS
2024-08-12 15:05:53
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/camera-shooting-V5

指定参数:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-camera-V5#capture

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-camera-V5#photocapturesetting

参考代码:

import { common, Want } from '@kit.AbilityKit'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
import { fileIo, fileUri } from '@kit.CoreFileKit'; 
import { promptAction } from '@kit.ArkUI'; 
import { camera, cameraPicker } from '@kit.CameraKit'; 
const TAG = '[SystemCameraDemo]'; 
@Entry 
@Component 
struct Index { 
  @State path: string = ''; 
  @State isVideo: boolean = false; 
  private context = getContext(this) as common.UIAbilityContext; 
  build() { 
    Row() { 
      Column({ space: 20 }) { 
        Button('拉起系统相机拍照--startAbility方式') 
          .width(300) 
          .height(50) 
          .onClick(async () => { 
            this.startAbility('ohos.want.action.imageCapture', 'com.xxx.xxx.myapplication'); 
          }) 
        Button('拉起系统相机录像--startAbility方式') 
          .width(300) 
          .height(50) 
          .onClick(async () => { 
            this.startAbility('ohos.want.action.videoCapture', 'com.xxx.xxx.myapplication'); 
          }) 
        Button('拉起系统相机拍照--cameraPicker方式') 
          .width(300) 
          .height(50) 
          .onClick(async () => { 
            this.cameraPicker(cameraPicker.PickerMediaType.PHOTO); 
          }) 
        Button('拉起系统相机录像--cameraPicker方式') 
          .width(300) 
          .height(50) 
          .onClick(async () => { 
            this.cameraPicker(cameraPicker.PickerMediaType.Video); 
          }) 
        if (this.isVideo && this.path) { 
          Video({ src: this.path }) 
            .objectFit(ImageFit.Contain) 
            .width('100%') 
            .height(300) 
        } 
        if (!this.isVideo && this.path) { 
          Image(this.path) 
            .objectFit(ImageFit.Contain) 
            .width('100%') 
            .height(300) 
        } 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
  async cameraPicker(type: cameraPicker.PickerMediaType) { 
    try { 
      let pickerProfile: cameraPicker.PickerProfile = { 
        // 相机的位置  后置摄像头 
        cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK, 
        //saveUri 保存配置信息的uri 
        //videoDuration 录制的最大时长 
      }; 
      cameraPicker.pick(getContext(this), [type], pickerProfile) 
        .then((result: cameraPicker.PickerResult) => { 
          if (result.resultCode != 0) { 
            // 处理业务逻辑错误 
            console.error(TAG, `cameraPicker.pick failed, result is ${JSON.stringify(result)}`); 
            promptAction.showToast({ message: '拍摄失败' }) 
            return; 
          } 
          //若saveUri为空,resultUri为公共媒体路径。若saveUri不为空且具备写权限,resultUri与saveUri相同。若saveUri不为空且不具备写权限,则无法获取到resultUri 
          let uri: string = result.resultUri; 
          // 执行正常业务 
          console.info(TAG, `Succeeded in cameraPicker.pick. Data is ${JSON.stringify(result)}, uri is ${uri}`); 
          if (uri) { 
            // 保存到本地 
            this.save2Local(uri); 
          } else { 
            promptAction.showToast({ message: '拍摄失败' }) 
          } 
        }) 
    } catch (error) { 
      let err = error as BusinessError; 
      console.error(TAG, `the pick call failed. error code: ${err.code}`); 
      promptAction.showToast({ message: '拍摄失败' }) 
    } 
  } 
  startAbility(action: string, bundleName: string) { 
    let want: Want = { 
      action: action, 
      parameters: { 
        // 拍照完成后返回的应用BundleName 
        callBundleName: bundleName, 
        supportMultiMode: false 
      } 
    }; 
    try { 
      this.context.startAbilityForResult(want, (err: BusinessError, result: common.AbilityResult) => { 
        if (err.code) { 
          // 处理业务逻辑错误 
          console.error(TAG, `startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); 
          return; 
        } 
        let uri: string = result?.want?.parameters?.resourceUri as string; 
        // 执行正常业务 
        console.info(TAG, `Succeeded in starting ability for result. Data is ${JSON.stringify(result)}, uri is ${uri}`); 
        if (uri) { 
          // 保存到本地 
          this.save2Local(uri); 
        } else { 
          promptAction.showToast({ message: '拍摄失败' }) 
        } 
      }); 
    } catch (err) { 
      let error = err as BusinessError; 
      console.error(TAG, `startAbilityForResult failed, err is ${JSON.stringify(error)}`); 
      promptAction.showToast({ message: '拍摄失败' }) 
    } 
  } 
  save2Local(uri: string) { 
    try { 
      let file = fileIo.openSync(uri, fileIo.OpenMode.READ_ONLY); 
      let prefix = uri.substring(uri.lastIndexOf('.') + 1); 
      let tempFileName = getContext(this).filesDir + '/' + new Date().getTime() + '.' + prefix; 
      let tempFile = fileIo.openSync(tempFileName, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 
      fileIo.copyFileSync(file.fd, tempFile.fd); 
      this.path = fileUri.getUriFromPath(tempFileName); 
      this.isVideo = (prefix == 'mp4' || prefix == 'MP4'); 
      console.info(TAG, `resolve2Sandbox successful.`); 
      promptAction.showToast({ message: '拍摄成功' }) 
    } catch (err) { 
      let error = err as BusinessError; 
      console.error(TAG, `resolve2Sandbox failed, err is ${JSON.stringify(error)}`); 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-08-12 19:48:23
相关问题
HarmonyOS 摄像头预览画面方向错误
258浏览 • 1回复 待解决
相机预览及切换摄像头
949浏览 • 1回复 待解决
HarmonyOS 前置摄像头开启
57浏览 • 1回复 待解决
HarmonyOS 录制屏幕 录制摄像头咨询
357浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照后数据展示
587浏览 • 1回复 待解决
请问3.1如何调用摄像头
2319浏览 • 1回复 待解决
录制过程中HarmonyOS如何切换摄像头
341浏览 • 1回复 待解决
HiSpark_IPC_DIY 摄像头烧录失败
5614浏览 • 3回复 待解决
如何获取前置摄像头的预览图像
2294浏览 • 1回复 待解决
寻找鸿蒙系统灯控设备、鸿蒙摄像头
5872浏览 • 2回复 待解决
鸿蒙webview调用摄像头和麦克风
2109浏览 • 0回复 待解决
摄像头获取到的yuv数据是否有旋转
514浏览 • 1回复 待解决
HarmonyOS 自定义相机demo
148浏览 • 1回复 待解决
HarmonyOS 自定义相机演示demo
107浏览 • 1回复 待解决