How to convert image stream buffer into jpeg image bytes in flutter for iOS?
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));
}
Almas Adilbek
Geek designer. I'm focused on quality products, where every single pixel is indispensable., Founder of Sajda.mobi & Kaz.News
Updated on December 13, 2022Comments
-
Almas Adilbek over 1 year
When we deal with camera in flutter, we use Camera plugin.
It has.startImageStream
method which returnsCameraImage cameraImage
data type.In iOS,
cameraImage.format
is bgra8888.
For androidcameraImage.format
is yuv420.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
.For android,
cameraImage(yuv420) to List<int>
is discussed and implemented in this issue: https://github.com/flutter/flutter/issues/26348#issuecomment-462321428The question is, how do we construct flutter Image(jpeg|png) from bgra8888
cameraImage
? -
Kohls over 4 yearsOp requested conversion to JPG, not PNG
-
Kamlesh over 3 yearsany solution for jpg image??
-
rexxar about 3 yearsthe solution for JPG is to rename the file to
thumbnail.jpg
and use theencodeJpg
method instead ofencodePng
-
Taufik Nur Rahmanda about 2 yearsthen how to set jpg background to white?