如何在iOS的flutter中将图像流缓冲区转换为jpeg图像字节? [英] How to convert image stream buffer into jpeg image bytes in flutter for iOS?

查看:591
本文介绍了如何在iOS的flutter中将图像流缓冲区转换为jpeg图像字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用 flutter 处理相机时,我们使用 Camera 插件。

它具有 .startImageStream 方法,该方法返回 CameraImage cameraImage 数据类型。

When we deal with camera in flutter, we use Camera plugin.
It has .startImageStream method which returns CameraImage cameraImage data type.

在iOS中, cameraImage.format bgra8888

对于android cameraImage.format yuv420

在将这些格式编码为JPEG或PNG,我们需要进行一些字节操作,并将每个字节放入图像缓冲区,然后在 JpegEncoder 中使用。

Before encoding these formats to JPEG or PNG, we need some bytes manipulation and put each byte into image buffer, which is then used in JpegEncoder.

对于android,在此问题中讨论并实现了 cameraImage(yuv420)到列表 > https://github.com/flutter/flutter/flutter/issues/26348#issuecomment-462321428

For android, cameraImage(yuv420) to List<int> is discussed and implemented in this issue: https://github.com/flutter/flutter/issues/26348#issuecomment-462321428

问题是,如何从 bgra8888 构造抖动 Image(jpeg | png) cameraImage

The question is, how do we construct flutter Image(jpeg|png) from bgra8888 cameraImage?

推荐答案

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

Take a look at the pub.dev image library API at https://pub.dev/packages/image. It converts from any image format, regardless of it being bgra8888 or yuv420. This example converts it to a PNG file:

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));
}

这篇关于如何在iOS的flutter中将图像流缓冲区转换为jpeg图像字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆