HarmonyOS 相机全屏预览流在折叠屏上适配问题咨询

自己实现的全屏相机双路预览时,预览流在折叠屏上存在分辨误差很大的问题

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

参考以下代码:

@Entry
@Component
struct CameraTestPage {
 private controller: XComponentController = new XComponentController;
 private renderId: string = '-1';
 @State cropImage: PixelMap | undefined = undefined;
 private context: common.BaseContext = getContext(this);
 private imageReceiver?: image.ImageReceiver;
 @State screenHeight: number = 0;
 @State screenWidth: number = 0;
 @State cameraHeight: number = 0;
 @State cameraWidth: number = 0;
 @State curFoldStatus: display.FoldStatus = 0;

 callback: Callback<display.FoldStatus> = async (data: display.FoldStatus) => {
  if (this.curFoldStatus === data) {
   return;
  }
  this.curFoldStatus = data;
  if (data === display.FoldStatus.FOLD_STATUS_EXPANDED || data === display.FoldStatus.FOLD_STATUS_FOLDED) {
   await this.initCamera();
   console.log('correct size=', JSON.stringify(this.controller.getXComponentSurfaceRect()))
  }
 };

 onPageShow(): void {
  this.initCamera()
 }

 async aboutToAppear() {
  let context = getContext() as common.UIAbilityContext;
  abilityAccessCtrl.createAtManager().requestPermissionsFromUser(context, ['ohos.permission.CAMERA']).then(() => {
   this.initCamera()
  });
  display.on('foldStatusChange', this.callback);
 }

 async initCamera() {
  this.getScreenInfo(); //获取屏幕宽高
  let size: image.Size = {
   width: this.screenWidth,
   height: this.screenHeight
  }
  console.log('size===', JSON.stringify(size))
  this.imageReceiver = image.createImageReceiver(size, image.ImageFormat.JPEG, 8);
  await this.createDualChannelPreview(this.renderId, this.imageReceiver);
  this.onImageArrival(this.imageReceiver);
 }

 private getScreenInfo() {
  this.screenHeight = display.getDefaultDisplaySync().height;
  this.screenWidth = display.getDefaultDisplaySync().width;
 }

 aboutToDisappear(): void {
  display.off('foldStatusChange', this.callback);
 }

 async createDualChannelPreview(XComponentSurfaceId: string, receiver: ESObject): Promise<void> {
  let cameraManager = camera.getCameraManager(this.context);
  let camerasDevices: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); // 获取支持的相机设备对象

  // 获取profile对象
  let profiles: camera.CameraOutputCapability =
   cameraManager.getSupportedOutputCapability(camerasDevices[0], camera.SceneMode.NORMAL_PHOTO); // 获取对应相机设备profiles
  let previewProfiles: Array<camera.Profile> = profiles.previewProfiles;
  let position: number = 0;
  if (previewProfiles != null) {
   const target = this.screenHeight / this.screenWidth;
   position = previewProfiles.reduce((closestIndex, currentArr, currentIndex) => {
    console.log('index=', currentIndex, ',size=', JSON.stringify(currentArr.size))
    const currentRatio = currentArr.size.width / currentArr.size.height;
    const closestRatio = previewProfiles[closestIndex].size.width / previewProfiles[closestIndex].size.height;
    const currentDiff = Math.abs(currentRatio - target);
    const closestDiff = Math.abs(closestRatio - target);
    return currentDiff < closestDiff ? currentIndex : closestIndex;
   }, 0);
   console.log(`screenHeight=${this.screenHeight},screenWidth=${this.screenWidth},target=${target},position=${position}`)
  }

  this.cameraWidth = previewProfiles[position].size.width;
  this.cameraHeight = previewProfiles[position].size.height;

  // 预览流1
  let previewProfilesObj: camera.Profile = previewProfiles[position];

  let previewOutput: camera.PreviewOutput =
   cameraManager.createPreviewOutput(previewProfilesObj, XComponentSurfaceId); // 创建 预览流1 输出对象
  let cameraInput: camera.CameraInput = cameraManager.createCameraInput(camerasDevices[0]); // 创建cameraInput对象
  await cameraInput.open(); // 打开相机
  let photoSession: camera.PhotoSession =
   cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession; // 会话流程

  photoSession.beginConfig(); // 开始配置会话
  photoSession.addInput(cameraInput); // 把CameraInput加入到会话
  photoSession.addOutput(previewOutput); // 把 预览流1 加入到会话
  await photoSession.commitConfig(); // 提交配置信息
  await photoSession.start(); // 会话开始
 }

 async onImageArrival(receiver: image.ImageReceiver): Promise<void> {
  receiver.on('imageArrival', () => {
   receiver.readLatestImage((err, nextImage: image.Image) => {
    if (err || nextImage === undefined) {
     return;
    }

    nextImage.getComponent(image.ComponentType.JPEG, async (err, imgComponent: image.Component) => {
     if (err || imgComponent === undefined) {
      return;
     }
     nextImage.release();
    })
   })
  })
 }

 build() {
  Stack() {
   XComponent({
    id: '',
    type: 'surface',
    libraryname: '',
    controller: this.controller
   })
    .onLoad(() => {
     this.renderId = this.controller.getXComponentSurfaceId();
    })
    .width(this.cameraHeight)
  }
 }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 折叠H5适配问题
819浏览 • 1回复 待解决
HarmonyOS 折叠适配资料
507浏览 • 1回复 待解决
HarmonyOS web组件内容适配折叠
282浏览 • 1回复 待解决
HarmonyOS 组件布局怎么适配折叠
386浏览 • 1回复 待解决
是否有相关折叠适配文档?
1910浏览 • 1回复 待解决
HarmonyOS折叠适配有什么方案吗?
450浏览 • 1回复 待解决
HarmonyOS GL封装相机预览流角度咨询
229浏览 • 1回复 待解决
HarmonyOS 折叠webview宽度问题
440浏览 • 1回复 待解决
HarmonyOS 相机权限问题咨询
40浏览 • 1回复 待解决
HarmonyOS 架权限问题咨询
79浏览 • 1回复 待解决
HarmonyOS 如何监听折叠展开折叠
64浏览 • 1回复 待解决
HarmonyOS 相机旋转横拍照问题
263浏览 • 1回复 待解决
如何区分折叠与非折叠手机?
349浏览 • 0回复 待解决
有人知道关于折叠适配问题吗?
1098浏览 • 1回复 待解决
HarmonyOS 相机-拍照之后预览
370浏览 • 1回复 待解决