如何将Flutter CameraImage从图像流转换为以Base64编码的二进制数据对象

时间:2019-01-17 06:37:11

标签: amazon-web-services dart camera flutter

在0.2.8版的颤振相机中引入了图像流后,我试图将其集成到我的项目中以与AWS一起使用。

Amazon要求图像的格式为:

  • 图像字节数(最多5 MB)。
  • 类型:Base64编码的二进制数据对象
  • 长度限制:最小长度为1。最大长度为5242880。

以前,我使用Camera包拍摄图片,加载图片,然后根据需要将其转换为Amazon,但是使用ImageStream更适合我想做的事情。我以前的方法是:

// Take the picutre
await _cameraController.takePicture(path);

// Load it from my filesystem
File imagefile = new File(path); 

// Convert to amazon requirements
List<int> imageBytes = imagefile.readAsBytesSync();
String base64Image = base64Encode(imageBytes);

但是,使用图像流,我找不到将CameraImage转换为亚马逊所需格式的任何简便方法。我对图像没有太多的经验,所以我很困惑。

我试图操纵firebase ml & camera stream demo

中使用的代码
final int numBytes =
    image.planes.fold(0, (count, plane) => count += plane.bytes.length);
final Uint8List allBytes = Uint8List(numBytes);

int nextIndex = 0;
for (int i = 0; i < image.planes.length; i++) {
  allBytes.setRange(nextIndex, nextIndex + image.planes[i].bytes.length,
      image.planes[i].bytes);
  nextIndex += image.planes[i].bytes.length;
}

// Convert as done previously
String base64Image = base64Encode(allBytes);

但是,AWS回复了InvalidImageFormatException。如果有人知道如何正确编码图像,那就太棒了!谢谢

4 个答案:

答案 0 :(得分:0)

将图像转换为png的解决方案:

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] = (0xFF << 24) | (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;
}

来源:https://github.com/flutter/flutter/issues/26348#issuecomment-462321428

答案 1 :(得分:0)

您可以使用它直接将图像文件转换为 base64。

图像编码:- var imageFilePath = await picker.getImage(source: ImageSource.gallery);

final bytes = ImageFilePath.readAsBytesSync(); String_img64 = base64Encode(bytes);

图像解码:- _img64 = base64Decode(response.bodyBytes); image.memory(_img64);

答案 2 :(得分:0)

将图库图片转换为 base 64 Flutter

<manifest>
    ...
    <application>
    ...
        <activity>
        ...

            <intent-filter android:priority="999">
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.OPENABLE" />

                <data android:host="*" />
                <data android:mimeType="application/octet-stream" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.YOUR_CUSTOM_EXTENSION" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.YOUR_CUSTOM_EXTENSION" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\.YOUR_CUSTOM_EXTENSION" />
                <data android:pathPattern=".*\\..*\\..*\\.YOUR_CUSTOM_EXTENSION" />
                <data android:pathPattern=".*\\..*\\.YOUR_CUSTOM_EXTENSION" />
                <data android:pathPattern=".*\\.YOUR_CUSTOM_EXTENSION" />
                <data android:scheme="content" />
            </intent-filter>

        ...
        </activity>
    </application>
</manifest>

答案 3 :(得分:-1)

我正在使用此代码将 YUV_420 888 转换为 PNG

// CameraImage YUV420_888 -> PNG -> Image (compresion:0, filter: none)
// Black
imglib.Image _convertYUV420(CameraImage image) {
  var img = imglib.Image(image.width, image.height); // Create Image buffer

  Plane plane = image.planes[0];
  const int shift = (0xFF << 24);

  // Fill image buffer with plane[0] from YUV420_888
  for (int x = 0; x < image.width; x++) {
    for (int planeOffset = 0;
    planeOffset < image.height * image.width;
    planeOffset += image.width) {
      final pixelColor = plane.bytes[planeOffset + x];
      // color: 0x FF  FF  FF  FF
      //           A   B   G   R
      // Calculate pixel color
      var newVal = shift | (pixelColor << 16) | (pixelColor << 8) | pixelColor;

      img.data[planeOffset + x] = newVal;
    }
  }

  return img;
}

这是为了创建一个 PNG

    Future<List<int>> convertImagetoPng(CameraImage image) async {
  try {
    imglib.Image img;
    if (image.format.group == ImageFormatGroup.yuv420) {
      img = convertYUV420_ToPNG(image);
    } else if (image.format.group == ImageFormatGroup.bgra8888) {
      img = convertBGRA8888_ToPNG(image);
    }

    imglib.PngEncoder pngEncoder = new imglib.PngEncoder();

    // Convert to png
    List<int> png = pngEncoder.encodeImage(img);
    return png;
  } catch (e) {
    print(">>>>>>>>>>>> ERROR:" + e.toString());
  }
  return null;
}

by hugand
您也可以检查性能