Flutter 如何将相机图像转换为颤振中的图像?

Flutter 如何将相机图像转换为颤振中的图像?,flutter,Flutter,我想将Flatter中Camera插件的函数startImageStream()中的摄像头图像转换为图像以裁剪该图像,但我只能找到转换为FirebaseVisionImage的方法。编辑彩色图像 如果我明白你的意思。您正在尝试covnert YUV420格式 下面是来自的代码片段 const shift=(0xFF图像包来自https://pub.dartlang.org/packages/image var img=imglib.Image(宽度、高度);//创建图像缓冲区 //使用YUV42

我想将Flatter中Camera插件的函数startImageStream()中的摄像头图像转换为图像以裁剪该图像,但我只能找到转换为FirebaseVisionImage的方法。

编辑彩色图像

如果我明白你的意思。您正在尝试covnert YUV420格式
下面是来自的代码片段

const shift=(0xFF图像包来自https://pub.dartlang.org/packages/image
var img=imglib.Image(宽度、高度);//创建图像缓冲区
//使用YUV420_888中的平面[0]填充图像缓冲区
对于(int x=0;x图像数据[索引]=移位|(b谢谢你,先生。太好了。先生。你试过这个转换功能了吗?我试着在转换后显示图像,但新图像没有颜色。我已经更新了我的答案。你能再次测试吗?并让我知道结果。非常感谢。如果这个答案不好,我会删除。是的,我已经检查了你给我的链接并更改了对于之前的颜色,它工作得很好。顺便说一句,因为这个函数运行非常慢,取决于循环,所以我设置(0xFF)来试试这个,要点在这里
const shift = (0xFF << 24);
Future<Image> convertYUV420toImageColor(CameraImage image) async {
      try {
        final int width = image.width;
        final int height = image.height;
        final int uvRowStride = image.planes[1].bytesPerRow;
        final int uvPixelStride = image.planes[1].bytesPerPixel;

        print("uvRowStride: " + uvRowStride.toString());
        print("uvPixelStride: " + uvPixelStride.toString());

        // imgLib -> Image package from https://pub.dartlang.org/packages/image
        var img = imglib.Image(width, height); // Create Image buffer

        // Fill image buffer with plane[0] from YUV420_888
        for(int x=0; x < width; x++) {
          for(int y=0; y < height; y++) {
            final int uvIndex = uvPixelStride * (x/2).floor() + uvRowStride*(y/2).floor();
            final int index = y * width + x;

            final yp = image.planes[0].bytes[index];
            final up = image.planes[1].bytes[uvIndex];
            final vp = image.planes[2].bytes[uvIndex];
            // Calculate pixel color
            int r = (yp + vp * 1436 / 1024 - 179).round().clamp(0, 255);
            int g = (yp - up * 46549 / 131072 + 44 -vp * 93604 / 131072 + 91).round().clamp(0, 255);
            int b = (yp + up * 1814 / 1024 - 227).round().clamp(0, 255);     
            // color: 0x FF  FF  FF  FF 
            //           A   B   G   R
            img.data[index] = shift | (b << 16) | (g << 8) | r;
          }
        }

        imglib.PngEncoder pngEncoder = new imglib.PngEncoder(level: 0, filter: 0);
        List<int> png = pngEncoder.encodeImage(img);
        muteYUVProcessing = false;
        return Image.memory(png);  
      } catch (e) {
        print(">>>>>>>>>>>> ERROR:" + e.toString());
      }
      return null;
  }