CameraPreview不能占据所有位置

时间:2020-01-26 15:41:57

标签: flutter

在Flutter应用程序中,我正在使用camera Plugin

它可以正常工作,但是"react-native-appearance": "^0.3.2", 不会占用所有可用大小。这是小部件树和结果:

CameraPreview

enter image description here

根据StackOverflow上的多个线程,我可以应用Scaffold( appBar: //AppBar(), body: Stack( children: <Widget>[ Center( child: AspectRatio( aspectRatio: cameraController.value.aspectRatio, child: CameraPreview(cameraController), ), ), _buildQRCodeOverlay(), ], ), ); 以获得“全屏”预览:

Transformation

但就我而言,它不起作用:

enter image description here

如何去除已有的白色边框并保持宽高比?

谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

经过一些测试,我终于找到了问题。全屏预览相机的示例仅在ResolutionPreset设置为medium时有效。在我的情况下,将其设置为high时不起作用。

根据文档,当CameraController.value.previewSizeResolutionPreset(720x480)和medium(1280x720)时,high是不同的。因此,CameraController.value.aspectRatio不能相同。

还请注意(至少在我的手机上),宽度为1280,高度为720。这意味着方向与手机不同。

因此,在我的情况下,我创建了一个返回CameraController宽高比的函数,在该函数中,我总是将“大”值除以较小的值:

  double _getCameraRatio()
  {
    final height = _cameraController.value.previewSize.height;
    final width = _cameraController.value.previewSize.width;

    if (width > 0.0 && height >= width)
    {
      return height / width;
    }

    if (height > 0.0 && width >= height)
    {
      return width / height;
    }

    return 1.0;
  }

然后,我做类似的事情以使Scaffold(减去AppBar)长宽比:

final scaffoldAspectRatio = _getScaffoldBodyAspectRatio(MediaQuery.of(context).size, Scaffold.of(context).appBarMaxHeight);
double _getScaffoldBodyAspectRatio(Size size, double appBarMaxHeight)
{
  return ((size.height - appBarMaxHeight) / size.width);
}

最后,我可以计算要应用的比例:

Transform.scale(
  scale: _getCameraPreviewScale(aspectRatio, scaffoldAspectRatio),
  child: AspectRatio(
    aspectRatio: cameraController.value.aspectRatio,
    child: CameraPreview(cameraController),
  ),
),
_getCameraPreviewScale(double cameraAspectRatio, double scaffoldAspectRatio)
  {
    if (cameraAspectRatio >= scaffoldAspectRatio)
    {
      return cameraAspectRatio / scaffoldAspectRatio;
    }

    if (scaffoldAspectRatio > cameraAspectRatio)
    {
      return scaffoldAspectRatio / cameraAspectRatio;
    }
  }

它似乎至少可以与ResolutionPreset.mediumResolutionPreset.high一起正常工作。

相关问题