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

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

HarmonyOS
2024-12-20 16:23:21
1676浏览
收藏 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)
  }
 }
}
  • 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.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
分享
微博
QQ
微信
回复
2024-12-20 20:02:39


相关问题
HarmonyOS Navigation的折叠适配问题
858浏览 • 1回复 待解决
HarmonyOS 折叠H5适配问题
2059浏览 • 1回复 待解决
HarmonyOS 折叠设备适配
1040浏览 • 1回复 待解决
HarmonyOS 折叠适配资料
1396浏览 • 1回复 待解决
HarmonyOS flutter如何适配折叠
1090浏览 • 1回复 待解决
HarmonyOS uniapp如何适配折叠
1131浏览 • 1回复 待解决
HarmonyOS 图表绘制折叠适配
589浏览 • 1回复 待解决
HarmonyOS navigation支持不了折叠适配
921浏览 • 1回复 待解决
HarmonyOS 组件布局怎么适配折叠
1416浏览 • 1回复 待解决
HarmonyOS web组件内容适配折叠
976浏览 • 1回复 待解决
是否有相关折叠适配文档?
2763浏览 • 1回复 待解决
HarmonyOS折叠适配有什么方案吗?
1761浏览 • 1回复 待解决
HarmonyOS 折叠监听问题
699浏览 • 1回复 待解决
HarmonyOS GL封装相机预览流角度咨询
937浏览 • 1回复 待解决