HarmonyOS 相机旋转横屏拍照问题

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

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

​CRHTakePhotoPage.ets中。

1、增加三个属性​。

@State xcRotate: number = 0; 
@State mLeft: number = 0; 
@State mTop: number = 0;

2、onPageShow中。

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

3、Column()增加。

.alignItems(HorizontalAlign.Start) 
.justifyContent(FlexAlign.Start)

4、XComponent()增加。

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

5、XComponent()长宽设置。

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

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(); 
  }; 
}
分享
微博
QQ
微信
回复
4天前
相关问题
HarmonyOS 播放问题
65浏览 • 1回复 待解决
关于如何使用相机拍照模块拍照问题
1676浏览 • 0回复 待解决
HarmonyOS 相机拍照模糊
369浏览 • 0回复 待解决
HarmonyOS 拉起相机拍照
247浏览 待解决
HarmonyOS video如何播放?
191浏览 • 1回复 待解决
HarmonyOS 相机-拍照之后预览
82浏览 • 1回复 待解决
HarmonyOS上如何控制相机拍照
21浏览 • 0回复 待解决
HarmonyOS APP无法打开相机拍照
296浏览 • 1回复 待解决
openharmony jsFA 如何显示?
7090浏览 • 1回复 待解决
打开相机:直接使用相机拍照能力
1481浏览 • 1回复 待解决
横竖旋转demo有哪些?
712浏览 • 1回复 待解决
如何获取当前是还是竖啊?
4779浏览 • 1回复 待解决
如何调用系统相机拍照
1539浏览 • 1回复 待解决
如何设置屏幕方向为
1044浏览 • 1回复 待解决
harmonyOS基于api9如何调用相机拍照
3667浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照后数据展示
554浏览 • 1回复 待解决
应用如何适配华为悬浮窗?
2506浏览 • 1回复 待解决