HarmonyOS 相机旋转横屏拍照问题

HarmonyOS 相机旋转横屏拍照问题。

HarmonyOS
2024-11-12 11:47:15
1319浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

​CRHTakePhotoPage.ets中。

1、增加三个属性​。

@State xcRotate: number = 0; 
@State mLeft: number = 0; 
@State mTop: number = 0;
  • 1.
  • 2.
  • 3.

2、onPageShow中。

let offset = px2vp((this.windowRectHeight-this.windowRectWidth)/2) 
this.xcRotate = 90; 
this.mTop = -offset; 
this.mLeft = offset;
  • 1.
  • 2.
  • 3.
  • 4.

3、Column()增加。

.alignItems(HorizontalAlign.Start) 
.justifyContent(FlexAlign.Start)
  • 1.
  • 2.

4、XComponent()增加。

.rotate({ angle: this.xcRotate }) 
.translate({x:this.mLeft, y: this.mTop})
  • 1.
  • 2.

5、XComponent()长宽设置。

.width(`${this.windowRectWidth}px`) 
.height(`${this.windowRectHeight}px`)
  • 1.
  • 2.

demo:

import router from '@ohos.router'; 
import prompt from '@ohos.promptAction'; 
import camera from '@ohos.multimedia.camera'; 
import CRHPhotoCamera from '../utils/CRHPhotoCamera' 
import Logger from '../utils/CRHLogger' 
import { CRHTakeIdCardModel, CRHTakeIdCardCallBackModel } from '../utils/CRHTakeIdCardModel' 
import { window } from '@kit.ArkUI'; 
import { CRHTakeIdCardCallBack } from '../utils/CRHTakeIdCardCallBack' 
const TAG: string = '[CRHTakePhotoPage]'; 
@Entry({ routeName: 'CRHTakePhotoPage' }) 
@Component 
export struct CRHTakePhotoPage { 
  private mXComponentController: XComponentController = new XComponentController(); 
  private surfaceId: string = '-1'; 
  private cameraModel: CRHPhotoCamera = new CRHPhotoCamera(getContext()); 
  @State windowRectWidth: number = 2688; 
  @State windowRectHeight: number = 1216; 
  @State crhTakeIdCardCallBackModel: CRHTakeIdCardCallBackModel | undefined = undefined; 
  @State crhTakeIdCardModel: CRHTakeIdCardModel = router.getParams() as CRHTakeIdCardModel; 
  @StorageLink('selectType_0') @Watch('rotationChange') rotation: number = 0; 
  @StorageLink('selectType_1') @Watch('resolutionChange') resolution: number = 0; 
  @State xcRotate: number = 0; 
  @State mLeft: number = 0; 
  @State mTop: number = 0; 
  /** 
   * 旋转角度改变监听方法 
   */ 
  rotationChange() { 
    Logger.info(TAG, `rotationChange begin ${this.rotation}`); 
    // 0° 
    if (this.rotation == 0) { 
      Logger.info(TAG, `rotationChange ${this.rotation}`); 
      this.cameraModel.setImageRotation(camera.ImageRotation.ROTATION_0); 
      // 90° 
    } else if (this.rotation == 1) { 
      Logger.info(TAG, `rotationChange ${this.rotation}`); 
      this.cameraModel.setImageRotation(camera.ImageRotation.ROTATION_90); 
      // 180° 
    } else if (this.rotation == 2) { 
      Logger.info(TAG, `rotationChange ${this.rotation}`); 
      this.cameraModel.setImageRotation(camera.ImageRotation.ROTATION_180); 
      // 270° 
    } else if (this.rotation == 3) { 
      Logger.info(TAG, `rotationChange ${this.rotation}`); 
      this.cameraModel.setImageRotation(camera.ImageRotation.ROTATION_270); 
    } 
    Logger.info(TAG, 'rotationChange end'); 
  } 
 
  /** 
   * 分辨率改变监听方法 
   */ 
  resolutionChange() { 
    Logger.info(TAG, `resolutionChange begin ${this.resolution}`); 
    // 不支持 则为默认800*600 
    if (this.resolution == 0) { 
      Logger.info(TAG, `resolutionChange ${this.resolution}`); 
      // this.cameraModel.setVideoFrameWH(VideoFrame.VIDEOFRAME_1920_1080); 
    } else if (this.resolution == 1) { 
      Logger.info(TAG, `resolutionChange ${this.resolution}`); 
      //this.cameraModel.setVideoFrameWH(VideoFrame.VIDEOFRAME_1920_1080); 
      // 1280*720 
    } else if (this.resolution == 2) { 
      Logger.info(TAG, `resolutionChange ${this.resolution}`); 
      //this.cameraModel.setVideoFrameWH(VideoFrame.VIDEOFRAME_1280_720); 
      // 800*600 
    } else if (this.resolution == 3) { 
      Logger.info(TAG, `resolutionChange ${this.resolution}`); 
      //this.cameraModel.setVideoFrameWH(VideoFrame.VIDEOFRAME_800_600); 
    } 
    Logger.info(TAG, 'resolutionChange end'); 
  } 
 
  build() { 
    Column() { 
      Stack({ alignContent: Alignment.Bottom }) { 
        Stack({ alignContent: Alignment.TopStart }) { 
          XComponent({ 
            id: 'componentId', 
            type: 'surface', 
            controller: this.mXComponentController 
          }) 
            .rotate({ angle: this.xcRotate }) 
            .translate({x:this.mLeft, y: this.mTop}) 
            .onLoad(() => { 
              //setTimeout(() => { 
              Logger.info(TAG, 'onLoad is called'); 
              this.mXComponentController.setXComponentSurfaceSize({ 
                surfaceWidth: AppStorage.get(`windowRectWidth`), 
                surfaceHeight: AppStorage.get(`windowRectHeight`) 
              }); 
              this.surfaceId = this.mXComponentController.getXComponentSurfaceId(); 
              Logger.info(TAG, `onLoad surfaceId: ${this.surfaceId}`); 
              this.cameraModel.initCamera(this.surfaceId); 
              // }, 200) 
            }) 
            .width(`${this.windowRectWidth}px`) 
            .height(`${this.windowRectHeight}px`) 
          Column() { 
            Column() { 
              Text('请将所拍证件放置在深色背景上') 
                .fontColor(Color.White) 
                .fontSize(20) 
                .align(Alignment.Center) 
            } 
            .margin({ top: 0 }) 
            .backgroundColor('#88000000') 
            .alignItems(HorizontalAlign.Center) 
            .size({ height: '10%', width: '100%' }) 
 
            Row() { 
              Column() { 
              } 
              .backgroundColor('#88000000') 
              .alignItems(HorizontalAlign.Start) 
              .size({ height: '100.1%', width: '20%' }) 
 
              Stack({ alignContent: Alignment.Bottom }) { 
                Column() { 
                }.backgroundColor('#88000000') 
                .alignItems(HorizontalAlign.End) 
                .size({ height: '100%', width: '100%' }) 
 
                Row() { 
                  Image($r('app.media.take_photo_normal')) 
                    .size({ width: 64, height: 64 }) 
                    .id('camera') 
                    .onClick(() => { 
                      Logger.info(TAG, 'takePicture begin'); 
                      prompt.showToast({ message: '拍照中...', duration: 200 }); 
                      this.cameraModel.takePicture(); 
                    }) 
                } 
                .alignItems(VerticalAlign.Center) 
                .size({ height: '100.1%', width: '100%' }) 
                .justifyContent(FlexAlign.Center) 
                .zIndex(1) 
 
                Row() { 
                  Text('返回') 
                    .fontColor(Color.White) 
                    .fontSize(25) 
                    .align(Alignment.BottomStart) 
                    .onClick((e)=>{ 
                      router.back(); 
                    }) 
                } 
                .alignItems(VerticalAlign.Bottom) 
                .size({ height: '10.1%', width: '100%' }) 
                .justifyContent(FlexAlign.Center) 
                .zIndex(1) 
              }.size({ height: '100.1%', width: '20%' }) 
            }.width('100%').height('90%').justifyContent(FlexAlign.SpaceBetween) 
          } 
          .width('100%') 
          .height('90%') 
        } 
        .width('100%') 
        .height('100%') 
 
        Column() { 
        } 
        .padding({ bottom: 10 }) 
        .backgroundColor('#88000000') 
        .alignItems(HorizontalAlign.Start) 
        .size({ height: '10%', width: '100%' }) 
      } 
      .width('100%') 
      .height('100%') 
      .layoutWeight(1) 
      .backgroundColor('#000000') 
    } 
    .width('100%') 
    .height('100%') 
    .alignItems(HorizontalAlign.Start) 
    .justifyContent(FlexAlign.Start) 
  } 
 
  onPageShow() { 
    let orientation = window.Orientation.LANDSCAPE_INVERTED; 
    window.getLastWindow(getContext(this), (err, w) => { 
      w.setWindowSystemBarEnable([]); 
      w.setWindowLayoutFullScreen(true); 
      w.setWindowKeepScreenOn(true); 
      w.setPreferredOrientation(orientation, (err, res) => { 
        if (err.code == 0) { 
          this.windowRectWidth = w.getWindowProperties().windowRect.width; 
          this.windowRectHeight = w.getWindowProperties().windowRect.height; 
          AppStorage.setOrCreate(`windowRectWidth`, this.windowRectWidth > this.windowRectHeight ? this.windowRectWidth : this.windowRectHeight); 
          AppStorage.setOrCreate(`windowRectHeight`, this.windowRectWidth > this.windowRectHeight ? this.windowRectHeight : this.windowRectWidth); 
          if (this.surfaceId) { 
            this.cameraModel.cameraRelease(); 
            //this.cameraModel.initCamera(this.surfaceId); 
          } 
          this.surfaceId = this.mXComponentController.getXComponentSurfaceId(); 
          // this.cameraModel.cameraRelease(); 
          setTimeout(() => { 
            this.cameraModel.initCamera(this.surfaceId); 
          },800); 
          Logger.info(TAG, `aboutToAppear,surfaceId=${this.surfaceId}`); 
          let offset = px2vp((this.windowRectHeight-this.windowRectWidth)/2) 
          this.xcRotate = 90; 
          this.mTop = -offset; 
          this.mLeft = offset; 
        } 
      }) 
    }) 
  } 
 
  onPageHide() { 
    // Logger.info(TAG, 'onPageHide begin'); 
    // Logger.info(TAG, 'onPageHide end'); 
    let orientation = window.Orientation.PORTRAIT; 
    window.getLastWindow(getContext(this), (err, w) => { 
      w.setWindowSystemBarEnable(['status', 'navigation']); 
      w.setWindowLayoutFullScreen(false) 
      w.setWindowKeepScreenOn(false); 
      w.setPreferredOrientation(orientation) 
      this.cameraModel.cameraRelease(); 
    }) 
  } 
 
  async aboutToAppear() { 
    // if (this.surfaceId) { 
    // this.cameraModel.initCamera(this.surfaceId); 
    // } 
    // this.surfaceId = this.mXComponentController.getXComponentSurfaceId(); 
    // Logger.info(TAG, `aboutToAppear,surfaceId=${this.surfaceId}`); 
    this.cameraModel.setTakePictureHandleCallback((photoUri: string): void => this.takePictureHandle(photoUri)); 
  } 
  onBackPress(): boolean | void { 
 
    return true; 
  } 
 
  async aboutToDisappear() { 
    // await this.cameraModel.cameraRelease(); 
  } 
 
  takePictureHandle = (imageStr: string) => { 
    this.crhTakeIdCardCallBackModel = { 
      img: imageStr, 
      callback: 'TakePhotoStruct' 
    } 
    if (CRHTakeIdCardCallBack.getInstance().crhTakeIdCardCallBack) { 
      CRHTakeIdCardCallBack.getInstance().crhTakeIdCardCallBack(this.crhTakeIdCardCallBackModel); 
    } 
    router.back(); 
  }; 
}
  • 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.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
分享
微博
QQ
微信
回复
2024-11-12 16:55:52
相关问题
HarmonyOS 播放问题
1462浏览 • 1回复 待解决
HarmonyOS 调用相机拍照问题
803浏览 • 1回复 待解决
HarmonyOS 后布局问题
1300浏览 • 1回复 待解决
HarmonyOS 相机拍照镜头问题
622浏览 • 1回复 待解决
关于如何使用相机拍照模块拍照问题
2555浏览 • 0回复 待解决
HarmonyOS Canvas后定位问题
613浏览 • 1回复 待解决
HarmonyOS 拉起相机拍照
972浏览 待解决
HarmonyOS 相机拍照模糊
1262浏览 • 0回复 待解决
HarmonyOS 相机-拍照之后预览
1379浏览 • 1回复 待解决
打开相机:直接使用相机拍照能力
2639浏览 • 1回复 待解决
HarmonyOS webView视频展示
866浏览 • 1回复 待解决
HarmonyOS video如何播放?
1091浏览 • 1回复 待解决
HarmonyOS 相册,相机拍照并裁剪
862浏览 • 1回复 待解决
HarmonyOS APP无法打开相机拍照
1229浏览 • 1回复 待解决
HarmonyOS 相机预览拍照流程报错
1185浏览 • 1回复 待解决
HarmonyOS 查询当前状态是还是竖
1207浏览 • 1回复 待解决