如何快速进入相机画面 [英] How to access camera frames in flutter quickly

查看:185
本文介绍了如何快速进入相机画面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的flutter应用程序的相机供稿上实现近实时OCR.为此,我想快速访问摄像机数据. 据我所知,我有两个选择,并且都遇到了障碍:

I would like to implement near real-time OCR on the camera feed of my flutter app. To do this I would like to access the camera data in a speedy manner. As far as I can tell I have two options, and have hit roadblocks with both:

  1. CameraPreview进行截图,方法是在其周围放置一个RepaintBoundary并创建一个RenderRepaintBoundary,然后调用boundary.toImage().此方法的问题在于.toImage方法似乎仅捕获边界中绘制的小部件,而不捕获摄像机预览中的数据.与此处描述的问题类似: https://github.com/flutter/flutter/issues/17687

  1. Take a screenshot of the CameraPreview by putting a RepaintBoundary around it and creating a RenderRepaintBoundary, and calling boundary.toImage(). The problem with this method is that the .toImage method only seems to capture the painted widgets in the boundary and not the data from the camera preview. Simmilar to the issue described here: https://github.com/flutter/flutter/issues/17687

使用Camera 0.2.1中的controller.takePicture(filePath)捕获图像,类似于示例文档.这里的问题是图像变得可用需要超长时间(2-3秒).我猜这是因为文件在捕获时已保存到光盘上,然后需要再次从文件中读取.

Capture an image with controller.takePicture(filePath) from Camera 0.2.1, similar to the example docs. The problem here is that it takes super long before the image becomes available (2-3 seconds). I guess that this is because the file is saved to the disc on capture and then needs to be read from the file again.

是否有任何方法可以在捕获后直接访问图片信息,以进行预处理和OCR等操作?

Is there any way that one can directly access the picture information after capture, to do things like pre-process and OCR?

推荐答案

对于近实时OCR",您需要CameraController#startImageStream

For "near real-time OCR", you need CameraController#startImageStream

示例代码

import 'package:camera/camera.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: _MyHomePage()));

class _MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<_MyHomePage> {
  dynamic _scanResults;
  CameraController _camera;

  bool _isDetecting = false;
  CameraLensDirection _direction = CameraLensDirection.back;

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

  Future<CameraDescription> _getCamera(CameraLensDirection dir) async {
    return await availableCameras().then(
      (List<CameraDescription> cameras) => cameras.firstWhere(
            (CameraDescription camera) => camera.lensDirection == dir,
          ),
    );
  }

  void _initializeCamera() async {
    _camera = CameraController(
      await _getCamera(_direction),
      defaultTargetPlatform == TargetPlatform.iOS
          ? ResolutionPreset.low
          : ResolutionPreset.medium,
    );
    await _camera.initialize();
    _camera.startImageStream((CameraImage image) {
      if (_isDetecting) return;
      _isDetecting = true;
      try {
        // await doSomethingWith(image)
      } catch (e) {
        // await handleExepction(e)
      } finally {
        _isDetecting = false;
      }
    });
  }
  Widget build(BuildContext context) {
    return null;
  }
}

此功能已合并到 https://github.com/flutter/plugins ,但并非如此有据可查.

This functionality was merged to https://github.com/flutter/plugins but it was not well documented.

参考:

  • https://github.com/flutter/flutter/issues/26348
  • https://github.com/flutter/plugins/pull/965
  • https://github.com/bparrishMines/mlkit_demo/blob/master/lib/main.dart#L43
  • https://youtu.be/OAEWySye0BQ?t=1460

这篇关于如何快速进入相机画面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆