如何实现前置镜头的录像镜像功能
===============

录像的前置镜头录像，需要开启镜像功能，调用[enableMirror()](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-videooutput#enablemirror15)可以启用/关闭镜像录像。

启用镜像录像前需要先通过[isMirrorSupported](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-videooutput#ismirrorsupported15)查询是否支持录像镜像功能，示例代码如下：

function testIsMirrorSupported(videoOutput: camera.VideoOutput): boolean {
  let isSupported: boolean = videoOutput.isMirrorSupported();
  return isSupported;
}

*   若支持录像镜像功能，调用[enableMirror()](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-videooutput#enablemirror15)可以启用/关闭镜像录像。示例代码如下：
    
    import { camera } from '@kit.CameraKit';
    import { media } from '@kit.MediaKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    _// ..._
    
    function enableMirror(videoOutput: camera.VideoOutput, mirrorMode: boolean): void {
      try {
        videoOutput.enableMirror(mirrorMode);
      } catch (error) {
        let err = error as BusinessError;
      }
    }
    
    启用/关闭录像镜像后，需要通过[getVideoRotation](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-videooutput#getvideorotation12)以及[updateRotation](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-media-avrecorder#updaterotation12)更新旋转角度。
    
*   若不支持录像镜像功能，可以使用[rotate](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-universal-attributes-transformation#rotate)实现组件翻转效果，对预览流进行镜像，方便用户对录像内容进行预览，之后对录像文件单独处理，利用三方库FFmpeg实现录像文件的内容镜像。
    1.  录像预览流设置：使用rotate对预览流组件XComponent进行镜像翻转，代码示例如下：
        
        _// Flip Angle_
        @State angle: number = 180;
        _// Use rotate to control whether the preview stream component XComponent is mirrored_
        XComponent({
          type: XComponentType.SURFACE,
          controller: this.mXComponentController,
          imageAIOptions: {
            types: \[ImageAnalyzerType.SUBJECT\],
            aiController: this.aiController
          }
        })
          .onLoad(() => {
           _ // The aspect ratio of the preview stream must match that of the recording output stream_
            this.mXComponentController.setXComponentSurfaceRect({
              surfaceWidth: this.videoSize.width,
              surfaceHeight: this.videoSize.height
            });
            this.surfaceId = this.mXComponentController.getXComponentSurfaceId()
          })
          .width(StyleConstants.FULL\_WIDTH)
          .height(StyleConstants.XCOMPONENT\_HEIGHT)
          .rotate({ y: 1, angle: this.angle, })
        
    2.  使用FFmpeg三方库的能力，对录像文件内容镜像。执行镜像操作前，需要先安装，具体步骤可参考[FFmpeg官网](https://ohpm.openharmony.cn/#/cn/detail/@sj%2Fffmpeg)。镜像命令执行代码如下：
        
        import { FFProgressMessageParser, FFmpeg } from '@sj/ffmpeg';
        
        let commands = \["ffmpeg", "-i", inputPath, "-vf", "hflip", outputPath\];
        FFmpeg.execute(commands, {
          logCallback: (logLevel: number, logMessage: string) => console.log(\`\[${logLevel}\]${logMessage}\`),
          progressCallback: (message: string) =>  console.log(\`\[progress\]${JSON.stringify(FFProgressMessageParser.parse(message))}\`),
        }).then(() => {
          console.info("FFmpeg execution succeeded.");
        }).catch((error: Error) => {
          console.error("FFmpeg execution failed with error: ${error.message}");
        });