首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将Flutter CameraImage从图像流转换为base64编码的二进制数据对象

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

Stack Overflow用户
提问于 2019-01-17 14:37:11
回答 3查看 4.9K关注 0票数 8

随着flutter camera版本0.2.8中引入的图像流,我尝试将其集成到我的项目中,以便与AWS一起使用。

亚马逊要求图像格式为:

高达5MB的图像字节的Blob。

类型:base64编码的二进制数据对象

长度约束:最小长度为1,最大长度为5242880。

之前我使用相机包来拍摄照片,加载照片,然后根据亚马逊的要求进行转换,但使用ImageStream更适合我想做的事情。我之前的方法是:

代码语言:javascript
复制
// Take the picutre
await _cameraController.takePicture(path);

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

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

但是,使用图像流时,我找不到任何简单的方法来将

转换为亚马逊要求的格式。我对图片没有太多的经验,所以我被卡住了。

我试图操纵

firebase ml和摄像头流演示

代码语言:javascript
复制
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的回应是

..。如果有人知道如何正确地编码图像,那就太棒了!谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-06-13 09:52:12

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

代码语言:javascript
复制
Future 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 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

票数 2
EN

Stack Overflow用户

发布于 2021-03-01 00:41:12

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

图像编码:- var imageFilePath =等待picker.getImage(来源: ImageSource.gallery);

最终字节= ImageFilePath.readAsBytesSync();字符串

_

img64 =base64Encode(字节);

图像解码:-

_

img64 = base64Decode(response.bodyBytes);image.memory(

_

img64);

票数 0
EN

Stack Overflow用户

发布于 2021-03-01 00:16:30

我正在使用这个代码来转换YUV

_

420 888转PNG

代码语言:javascript
复制
// 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

代码语言:javascript
复制
Future> 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 png = pngEncoder.encodeImage(img);
    return png;
  } catch (e) {
    print(">>>>>>>>>>>> ERROR:" + e.toString());
  }
  return null;
}

巨大的

您也可以检查性能

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54230291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档