首页 > 解决方案 > 尝试在 Flutter 中使用 opencv 显示过滤后的相机视频预览

问题描述

我正在制作一个应用程序,它使用设备的摄像头在当前视频预览上使用 opencv 进行过滤。

  1. 我正在使用相机插件
  2. 使用 startImageStream 我正在获取视频的帧
  3. 我在 C++ 中做过滤器,所以我使用ffi将信息发送到过滤器。这是c ++中函数的标头
__attribute__((visibility("default"))) __attribute__((used))
uint8_t* process_image(int32_t width, int32_t height, uint8_t *bytes)

我有一个native_opencv.dart如下

import 'dart:ffi' as ffi;
import 'dart:io';
import 'package:ffi/ffi.dart';

// C function signatures
typedef _process_image_func = ffi.Pointer<ffi.Uint8> Function(ffi.Int32 width, ffi.Int32 height, ffi.Pointer<ffi.Uint8> bytes);

// Dart function signatures
typedef _ProcessImageFunc = ffi.Pointer<ffi.Uint8> Function(int width, int height, ffi.Pointer<ffi.Uint8> bytes);

// Getting a library that holds needed symbols
ffi.DynamicLibrary _lib = Platform.isAndroid
  ? ffi.DynamicLibrary.open('libnative_opencv.so')
  : ffi.DynamicLibrary.process();

// Looking for the functions
final _ProcessImageFunc _processImage = _lib
  .lookup<ffi.NativeFunction<_process_image_func>>('process_image')
  .asFunction();

ffi.Pointer<ffi.Uint8> processImage(int width, int height, ffi.Pointer<ffi.Uint8> bytes)
{
  return _processImage(width, height, bytes);
}
  1. 我在这里卡住了。我需要将 C++ 中过滤器中处理的视频帧返回到应用程序并在屏幕上显示。我以为我可以使用 CameraController 并为其提供字节数组,但我不知道该怎么做(如果可能的话)。这就是我到目前为止在main.dart中所拥有的那部分内容:
 void _initializeCamera() async {
    // Get list of cameras of the device
    List<CameraDescription> cameras = await availableCameras();

    // Create the CameraController
    _camera = new CameraController(cameras[0], ResolutionPreset.veryHigh);
    _camera.initialize().then((_) async{
      // Start ImageStream
      await _camera.startImageStream((CameraImage image) => _processCameraImage(image));
    });
  }

  Future<void> _processCameraImage(CameraImage image) async
  {
    Pointer<Uint8> p = allocate(count: _savedImage.planes[0].bytes.length);

    // Assign the planes data to the pointers of the image
    Uint8List pointerList = p.asTypedList(_savedImage.planes[0].bytes.length);
    pointerList.setRange(0, _savedImage.planes[0].bytes.length, _savedImage.planes[0].bytes);

    // Get the pointer of the data returned from the function to a List
    Pointer<Uint8> afterP = processImage(_savedImage.width, _savedImage.height, p);
    List imgData = afterP.asTypedList((_savedImage.width * _savedImage.height));

    // Generate image from the converted data
    imglib.Image img = imglib.Image.fromBytes(_savedImage.height, _savedImage.width, imgData);

  }

我不知道如何在应用程序屏幕上显示来自相机的预览视频的过滤帧。

任何帮助表示赞赏,谢谢。

标签: c++flutteropencvvideo-processing

解决方案


推荐阅读