How can I convert CameraImageStream CameraImage to jpeg or bitmap in Flutter using Flutter's image-processing libraries?

huangapple go评论123阅读模式
英文:

How can I convert CameraImageStream CameraImage to jpeg or bitmap in Flutter using Flutter's image-processing libraries?

问题

I can help with the translation of the code you provided. Here's the translated code:

  1. 无法将CameraImagebgra8888转换为jpeg
  2. 我已经尝试了不同的方法,但都不起作用。
  3. 我最后尝试的是:
  4. ```dart
  5. void testB(CameraImage i) async {
  6. cameraRepository.stopRecording();
  7. print(i.format.group);
  8. imglib.Image im = imglib.Image.fromBytes(
  9. width: (i.planes[0].bytesPerRow / 4).round(),
  10. height: i.height,
  11. bytes: (i.planes[0].bytes).buffer);
  12. imglib.PngEncoder pngEncoder = new imglib.PngEncoder();
  13. Uint8List png = pngEncoder.encode(im);
  14. //Uint8List jpeg = imglib.encodeJpg(im);
  15. imageList.add(png);

当我像这样在小部件中显示它:

  1. return Scaffold(
  2. appBar: AppBar(
  3. title: const Text('Test'),
  4. ),
  5. body: ListView.builder(
  6. itemCount: 1,
  7. itemBuilder: (BuildContext context, int index) {
  8. return SizedBox(width:500, height:500, child: Container(
  9. decoration: BoxDecoration(
  10. image: new DecorationImage(fit: BoxFit.cover, image: MemoryImage(BlocProvider.of<CameraBloc>(context).imageList[0], scale: 0.5)),
  11. )
  12. ));
  13. }),
  14. );

图像会显示出来,但是转换似乎有问题:

图片链接

(这是我的键盘,所以图像不是完全的噪音)。

我尝试了这段代码:

  1. import 'package:image/image.dart' as imglib;
  2. import 'package:camera/camera.dart';
  3. Future<List<int>> convertImagetoPng(CameraImage image) async {
  4. try {
  5. imglib.Image img;
  6. if (image.format.group == ImageFormatGroup.yuv420) {
  7. img = _convertYUV420(image);
  8. } else if (image.format.group == ImageFormatGroup.bgra8888) {
  9. img = _convertBGRA8888(image);
  10. }
  11. imglib.PngEncoder pngEncoder = new imglib.PngEncoder();
  12. // 转换为png
  13. List<int> png = pngEncoder.encodeImage(img);
  14. return png;
  15. } catch (e) {
  16. print(">>>>>>>>>> ERROR:" + e.toString());
  17. }
  18. return null;
  19. }
  20. // CameraImage BGRA8888 -> PNG
  21. // 颜色
  22. imglib.Image _convertBGRA8888(CameraImage image) {
  23. return imglib.Image.fromBytes(
  24. image.width,
  25. image.height,
  26. image.planes[0].bytes,
  27. format: imglib.Format.bgra,
  28. );
  29. }
  30. // CameraImage YUV420_888 -> PNG -> Image(压缩:0,过滤器:无)
  31. // 黑色
  32. imglib.Image _convertYUV420(CameraImage image) {
  33. var img = imglib.Image(image.width, image.height); // 创建图像缓冲区
  34. Plane plane = image.planes[0];
  35. const int shift = (0xFF << 24);
  36. // 使用YUV420_888的plane[0]填充图像缓冲区
  37. for (int x = 0; x < image.width; x++) {
  38. for (int planeOffset = 0;
  39. planeOffset < image.height * image.width;
  40. planeOffset += image.width) {
  41. final pixelColor = plane.bytes[planeOffset + x];
  42. // 颜色:0x FF FF FF FF
  43. // A B G R
  44. // 计算像素颜色
  45. var newVal = shift | (pixelColor << 16) | (pixelColor << 8) | pixelColor;
  46. img.data[planeOffset + x] = newVal;
  47. }
  48. }
  49. return img;
  50. }

但似乎已经过时。尝试时出现不同的错误。

更新

  1. imglib.Image im = imglib.Image.fromBytes(
  2. width: (i.planes[0].bytesPerRow / 4).round(),
  3. height: i.height,
  4. bytes: (i.planes[0].bytes).buffer);

将高度更改为宽度,宽度更改为高度后,图像不会更清晰,几乎是好的。我搞混了这一点,因为看起来似乎不对。

但仍然不好。

  1. Please note that the code comments have also been translated for your convenience.
  2. <details>
  3. <summary>英文:</summary>
  4. I cant convert CameraImage from bgra8888 to an jpeg.
  5. I have found different approaches but none of them work.
  6. The last what I tried is:

void testB(CameraImage i) async {

  1. cameraRepository.stopRecording();
  2. print(i.format.group);
  3. imglib.Image im = imglib.Image.fromBytes(
  4. width: (i.planes[0].bytesPerRow / 4).round(),
  5. height: i.height,
  6. bytes: (i.planes[0].bytes).buffer);
  7. imglib.PngEncoder pngEncoder = new imglib.PngEncoder();
  8. Uint8List png = pngEncoder.encode(im);
  9. //Uint8List jpeg = imglib.encodeJpg(im);
  10. imageList.add(png);
  1. When I display it in a widget like this:

return Scaffold(
appBar: AppBar(
title: const Text('Test'),
),
body: ListView.builder(
itemCount: 1,
itemBuilder: (BuildContext context, int index) {
return SizedBox(width:500, height:500, child: Container(
decoration: BoxDecoration(
image: new DecorationImage(fit: BoxFit.cover, image: MemoryImage(BlocProvider.of<CameraBloc>(context).imageList[0], scale: 0.5)),
)
));
}),
);

  1. I images shows up, but something is wrong with the conversion:
  2. https://pasteboard.co/5yMYpLFxsPkR.jpg
  3. (Thats my Keyboard, so the image isnt complete Noise).
  4. I&#39;ve tried this code:

import 'package:image/image.dart' as imglib;
import 'package:camera/camera.dart';

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

  1. imglib.PngEncoder pngEncoder = new imglib.PngEncoder();
  2. // Convert to png
  3. List&lt;int&gt; png = pngEncoder.encodeImage(img);
  4. return png;

} catch (e) {
print(">>>>>>>>>>>> ERROR:" + e.toString());
}
return null;
}

// CameraImage BGRA8888 -> PNG
// Color
imglib.Image _convertBGRA8888(CameraImage image) {
return imglib.Image.fromBytes(
image.width,
image.height,
image.planes[0].bytes,
format: imglib.Format.bgra,
);
}

// 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;

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

}

return img;
}

  1. But it seems outdated. Got different errors when i tried it.
  2. **UPDATE**
  3. imglib.Image im = imglib.Image.fromBytes(
  4. width: (i.planes[0].bytesPerRow / 4).round(),
  5. height: i.height,
  6. bytes: (i.planes[0].bytes).buffer);
  7. After changing height to width and width to height, the image is no clearer and almost good. I confused this as it seems.
  8. But its still not good.
  9. </details>
  10. # 答案1
  11. **得分**: 1
  12. 如果有人在iPhone上将此流式相机图像(brga8888)转换时遇到困难,由于图像包的新版本,参数已更改:
  13. ```dart
  14. imglib.Image im = imglib.Image.fromBytes(
  15. height: i.height,
  16. width: i.width,
  17. bytes: (i.planes[0].bytes).buffer,
  18. format: imglib.Format.uint8,
  19. order: ChannelOrder.bgra
  20. );

这将生成一个清晰的图像。

英文:

If someone is struggling to convert this Streaming CameraImage on IPhone (brga8888),
since a new Version of Image Package the Parameter have changed:

  1. imglib.Image im = imglib.Image.fromBytes(
  2. height: i.height,
  3. width: i.width,
  4. bytes: (i.planes[0].bytes).buffer,
  5. format: imglib.Format.uint8,
  6. order: ChannelOrder.bgra
  7. );

This produced a clean Image.

huangapple
  • 本文由 发表于 2023年5月31日 22:45:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76374739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定