HarmonyOS 相机开发,横屏时,摄像头的预览画面能否正向?

相机开发,横屏时,摄像头的预览画面能否正向?在页面横屏时,打开前置摄像头,希望此时的预览画面是正向的,目前画面是向一边倒的。

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

可以给XComponent设置rotate旋转:

import { abilityAccessCtrl, PermissionRequestResult, Permissions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import common from '@ohos.app.ability.common';
import camera from '@ohos.multimedia.camera';
import { window } from '@kit.ArkUI';

@Entry
@Component
export struct CameraPage {
  @StorageProp('statusBarHeight') statusBarHeight: number = 0;
  private xcomponentController: XComponentController = new XComponentController;
  private surfaceId: string = '';

  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(`${err.code}:${err.message}`)
    })
  }

  async aboutToAppear(): Promise<void> {
    this.requestCameraPermission()
    window.getLastWindow(getContext(this)).then((data) => {
      data.setPreferredOrientation(window.Orientation.LANDSCAPE)})
  }

  build() {
    Column() {

      XComponent({
        id: 'xcomponent',
        type: XComponentType.SURFACE,
        controller: this.xcomponentController
      })
        .onLoad(() => {
          this.xcomponentController.setXComponentSurfaceSize({ surfaceWidth: 1920, surfaceHeight: 1080 });
          this.surfaceId = this.xcomponentController.getXComponentSurfaceId()
        })
        .rotate({ angle: -90})
        .width(200)
        .height(200)

      Button("开始预览")
        .onClick(() => {
          previewCamera(getContext(this), this.surfaceId);
        })
    }
  }
}

export 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;
  try {
    cameraInput = cameraManager.createCameraInput(cameraArray[0]);

    cameraInput.on('error', cameraArray[0], (error: BusinessError) => {
      console.error(`Camera input error code: ${error.code}`);
    });
    await cameraInput.open();
  } catch (e) {
    let error: BusinessError = e as BusinessError;
    console.error(`${error.code}:${error.message}`)
  }
  //获取相机设备支持的输出流能力
  let cameraOutputCap: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraArray[0]);
  let previewProfilesArray: Array<camera.Profile> = cameraOutputCap.previewProfiles;
  if (!previewProfilesArray) {
    console.error("cameraManager.getSupportedOutputCapability error");
    return undefined;
  }
  //创建预览输出流
  let previewOutput: camera.PreviewOutput | undefined = undefined;
  previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId);
  //创建会话
  let captureSession: camera.PhotoSession | undefined = undefined;
  captureSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession;
  try {
    captureSession.beginConfig();
    captureSession.addInput(cameraInput);
    captureSession.addOutput(previewOutput);
    await captureSession.commitConfig();
    await captureSession.start();
  } catch (e) {
    let error: BusinessError = e as BusinessError;
    console.error(`${error.code}:${error.message}`)
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 摄像头预览画面方向错误
493浏览 • 1回复 待解决
相机预览及切换摄像头
1242浏览 • 1回复 待解决
HarmonyOS 摄像头切换卡死
211浏览 • 1回复 待解决
如何获取前置摄像头预览图像
2599浏览 • 1回复 待解决
HarmonyOS 获取摄像头能力
239浏览 • 1回复 待解决
HarmonyOS 自定义相机前置摄像头变形
220浏览 • 1回复 待解决
HarmonyOS 录制屏幕 录制摄像头咨询
651浏览 • 1回复 待解决
HarmonyOS 摄像头录制,帧回调数据
225浏览 • 1回复 待解决
HarmonyOS 前置摄像头开启
429浏览 • 1回复 待解决
HarmonyOS 摄像头录制问题
278浏览 • 1回复 待解决
HarmonyOS 打开摄像头失败
219浏览 • 1回复 待解决