How to convert Flutter CameraImage to a Base64-encoded binary data object from an Image Stream. A solution to convert the image to a png:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.