方形相机覆盖层使用Flutter

2个回答

9

目前,您使用的库还没有内置的相机叠加支持。我创建了一个小部件,它位于CameraPreview堆栈中,可复制叠加效果。

@override
  Widget build(BuildContext context) {
    return AspectRatio(
          aspectRatio: _controller.value.aspectRatio,
          child: Stack(fit: StackFit.expand, children: [
            CameraPreview(_controller),
            cameraOverlay(
                padding: 50, aspectRatio: 1, color: Color(0x55000000))
          ]));
}

Widget cameraOverlay({required double padding, required double aspectRatio, required Color color}) {
    return LayoutBuilder(builder: (context, constraints) {
      double parentAspectRatio = constraints.maxWidth / constraints.maxHeight;
      double horizontalPadding;
      double verticalPadding;

      if (parentAspectRatio < aspectRatio) {
        horizontalPadding = padding;
        verticalPadding = (constraints.maxHeight -
                ((constraints.maxWidth - 2 * padding) / aspectRatio)) /
            2;
      } else {
        verticalPadding = padding;
        horizontalPadding = (constraints.maxWidth -
                ((constraints.maxHeight - 2 * padding) * aspectRatio)) /
            2;
      }
      return Stack(fit: StackFit.expand, children: [
        Align(
            alignment: Alignment.centerLeft,
            child: Container(width: horizontalPadding, color: color)),
        Align(
            alignment: Alignment.centerRight,
            child: Container(width: horizontalPadding, color: color)),
        Align(
            alignment: Alignment.topCenter,
            child: Container(
                margin: EdgeInsets.only(
                    left: horizontalPadding, right: horizontalPadding),
                height: verticalPadding,
                color: color)),
        Align(
            alignment: Alignment.bottomCenter,
            child: Container(
                margin: EdgeInsets.only(
                    left: horizontalPadding, right: horizontalPadding),
                height: verticalPadding,
                color: color)),
        Container(
          margin: EdgeInsets.symmetric(
              horizontal: horizontalPadding, vertical: verticalPadding),
          decoration: BoxDecoration(border: Border.all(color: Colors.cyan)),
        )
      ]);
    });
  }

enter image description here


非常感谢您。我有一个问题,如何制作像链接中的程序 https://www.youtube.com/watch?v=E0zVopYLrtQ - mohanad.99
@bikram,我能得到完整的代码吗?如果你能分享它的话,那将非常有帮助。谢谢。 - Ananthakrishna
@bikram 非常感谢。我已经寻找了好几天了。你是真正的救命恩人。 - Chetan Goyal
你在加上遮罩预览后,是否也处理过视频裁剪? - Chetan Goyal
请注意,通过将padding参数更改为verticalPadding和horizontalPadding,我能够使这个矩形成为身份证叠加层。 - JakeD

4
请使用 camera_camera 包,该包与相机相关。 https://pub.dev/packages/camera_camera API 参考文档: https://pub.dev/documentation/camera_camera/latest/ Github 地址: https://github.com/gabulsavul/camera_camera 若需要查看代码详细信息,请访问:https://github.com/gabulsavul/camera_camera/blob/master/lib/page/camera.dart
如包描述所述,您可以添加矩形、圆形或正方形对焦框,请参见以下代码片段。
Camera(
       mode: CameraMode.normal,
      imageMask: CameraFocus.rectangle(
                color: Colors.black.withOpacity(0.5),
                ),
     )

enter image description here enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接