gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:38:24 26 4
gpt4 key购买 nike

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

亚马逊要求图片格式为:

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

以前我使用 Camera 包来拍照,加载图片,然后将其转换为亚马逊所需的,但使用 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 响应。如果有人知道如何正确编码图像,那就太棒了!谢谢

最佳答案

将图片转成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

关于amazon-web-services - 如何将 Flutter CameraImage 从图像流转换为 Base64 编码的二进制数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54230291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com