如何在 iOS 的 flutter 中将图像流缓冲区转换为 jpeg 图像字节?

2024-02-13

当我们处理相机时flutter, 我们用Camera plugin.
It has .startImageStream返回的方法CameraImage cameraImage数据类型。

In iOS, cameraImage.format is bgra8888.
适用于安卓cameraImage.format is yuv420.

在将这些格式编码为 JPEG 或 PNG 之前,我们需要一些字节操作并将每个字节放入图像缓冲区,然后在JpegEncoder.

对于安卓系统来说,cameraImage(yuv420) to List<int>本期讨论并实施:https://github.com/flutter/flutter/issues/26348#issuecomment-462321428 https://github.com/flutter/flutter/issues/26348#issuecomment-462321428

问题是,我们如何构建flutter图片(jpeg|png) from bgra8888 cameraImage?


看看pub.dev图像库API位于https://pub.dev/packages/image https://pub.dev/packages/image。它可以从任何图像格式进行转换,无论是 bgra8888 还是 yuv420。此示例将其转换为 PNG 文件:

import 'dart:io';
import 'package:image/image.dart';
void main() {
  // Read an image from file
  // decodeImage will identify the format of the image and use the appropriate
  // decoder.
  Image image = decodeImage(File('test.bgra8888').readAsBytesSync());

  // Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
  Image thumbnail = copyResize(image, width: 120);

  // Save the thumbnail as a PNG.
  File('thumbnail.png').writeAsBytesSync(encodePng(thumbnail));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 iOS 的 flutter 中将图像流缓冲区转换为 jpeg 图像字节? 的相关文章

随机推荐