You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在安卓设备的Flutter应用中可靠检测前置摄像头镜像状态

Flutter安卓前置摄像头镜像状态的可靠检测方案需求

我在开发Flutter应用的前置摄像头功能时遇到一个问题:部分安卓设备的前置摄像头预览呈现镜像效果,其他设备则显示正常。我需要一种可靠的方法来检测前置摄像头是否处于镜像状态。

已尝试的无效方案

1. 基于CameraController的sensorOrientation判断

我曾假设sensorOrientation为90或270的设备会对前置摄像头预览做镜像处理,但这个方法并不可靠。

实现代码:

bool isFrontCameraMirrored() {
  if (_cameraController?.description.lensDirection == CameraLensDirection.front) {
    int sensorOrientation = _cameraController?.description.sensorOrientation ?? 0;
    
    // 原逻辑:认为sensorOrientation为90或270的设备会镜像前置摄像头
    return sensorOrientation == 90 || sensorOrientation == 270;
  }
  return false;
}

问题:我有两台sensorOrientation均为270的设备,其中一台预览是镜像效果,另一台却正常,说明该判断逻辑不成立。

2. 拍摄照片对比左右像素差异

我尝试捕获一张照片,通过对比图像左右两侧的像素来判断是否镜像。

实现代码:

bool checkHorizontalMirroring(img.Image image) {
  int width = image.width;
  int height = image.height;

  var leftPixel = image.getPixel(10, height ~/ 2);
  var rightPixel = image.getPixel(width - 10, height ~/ 2);

  return leftPixel != rightPixel;
}

问题:无论图像是否处于镜像状态,这个方法始终返回true,无法正确检测。

检测结果的使用场景

一旦确定摄像头是否镜像,我会通过_isMirroredHorizontally变量控制UI中的预览显示,代码如下:

_isMirroredHorizontally
    ? Positioned.fill(
        child: Transform.flip(
          flipX: true,
          child: CameraPreview(_cameraController!),
        ),
      )
    : Transform.scale(
        scale: 1 / tempScale,
        child: Center(
          child: CameraPreview(_cameraController!),
        ),
      ),

内容的提问来源于stack exchange,提问作者Lala Naibova

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 13:40:01