【问题标题】:Flutter how to draw pressable rectangle on camera feed?颤振如何在相机提要上绘制可按下的矩形?
【发布时间】:2021-04-03 20:00:27
【问题描述】:

我正在使用camera 插件来获取摄像头信息。每 10 秒,我生成 4 个值 (x,y,w,h) 并在屏幕上(带有随机文本)绘制一个矩形(只有边框,内部是透明的)。如果用户点击这个框,它就会消失。

看起来像这张图片。 (x,y,w,h) 和文字是随机生成的。

使用camera 插件可以做到这一点吗?或者是否有其他软件包已经这样做了?

【问题讨论】:

  • 相机插件没有这个功能。您必须自定义构建它。
  • 所以插件没有可按下的矩形。但是它有绘制矩形的内置函数吗?
  • 相机插件没有绘制矩形的功能,但是您可以使用 Stack 小部件包装相机输出并用它绘制边界框。如果有帮助,我可以给你写一个示例代码。
  • @dshukertjr 是的,请!示例代码将非常有帮助。如果可能,在该矩形顶部(或矩形内部,如上图)添加文本也很有帮助

标签: flutter camera rectangles bounding-box


【解决方案1】:

相机插件没有任何可以在预览上绘制的功能,但您可以使用 Stack 小部件和容器进行类似的操作。

这是一个快速而肮脏的例子,但希望能给你一些想法:

class CameraApp extends StatefulWidget {
  @override
  _CameraAppState createState() => _CameraAppState();
}

class _CameraAppState extends State<CameraApp> {
  CameraController controller;

  // Holds the position information of the rectangle
  Map<String, double> _position = {
    'x': 0,
    'y': 0,
    'w': 0,
    'h': 0,
  };

  // Whether or not the rectangle is displayed
  bool _isRectangleVisible = false;

  Future<void> getCameras() async {
    final cameras = await availableCameras();
    controller = CameraController(cameras[0], ResolutionPreset.medium);
    controller.initialize().then((_) {
      if (!mounted) {
        return;
      }
      setState(() {});
    });
  }

  // Some logic to get the rectangle values
  void updateRectanglePosition() {
    setState(() {
      // assign new position
      _position = {
        'x': 0,
        'y': 0,
        'w': 0,
        'h': 0,
      };
      _isRectangleVisible = true;
    });
  }

  @override
  void initState() {
    getCameras();
    super.initState();
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (!controller.value.isInitialized) {
      return Container();
    }
    return Stack(
      children: [
        AspectRatio(
          aspectRatio: controller.value.aspectRatio,
          child: controller == null ? Container() : CameraPreview(controller),
        ),
        if (_isRectangleVisible)
          Positioned(
            left: _position['x'],
            top: _position['y'],
            child: InkWell(
              onTap: () {
                // When the user taps on the rectangle, it will disappear
                setState(() {
                  _isRectangleVisible = false;
                });
              },
              child: Container(
                width: _position['w'],
                height: _position['h'],
                decoration: BoxDecoration(
                  border: Border.all(
                    width: 2,
                    color: Colors.blue,
                  ),
                ),
                child: Align(
                  alignment: Alignment.topLeft,
                  child: Container(
                    color: Colors.blue,
                    child: Text(
                      'hourse -71%',
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                ),
              ),
            ),
          ),
      ],
    );
  }
}

【讨论】:

  • 我得到 "NoSuchMethodError: The getter 'value' was called on null" 。我的 main() 只是“WidgetsFlutterBinding.ensureInitialized(); runApp(new CameraApp());”也许缺少什么?
  • @DukeLe 我刚刚编辑了我的答案,但可能有一些小错误。我希望您从这段代码中得到的基本思想是如何使用 Stack 小部件在相机预览上进行绘制。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
相关资源
最近更新 更多