convertYUV420ToImage static method
Image
convertYUV420ToImage(
- CameraImage image
Implementation
static Image convertYUV420ToImage(CameraImage image) {
final uvRowStride = image.planes[1].bytesPerRow;
final uvPixelStride = image.planes[1].bytesPerPixel ?? 0;
final img = Image(width: image.width, height: image.height);
for (final p in img) {
final x = p.x;
final y = p.y;
final uvIndex =
uvPixelStride * (x / 2).floor() + uvRowStride * (y / 2).floor();
final index = y * uvRowStride +
x; // Use the row stride instead of the image width as some devices pad the image data, and in those cases the image width != bytesPerRow. Using width will give you a distored image.
final yp = image.planes[0].bytes[index];
final up = image.planes[1].bytes[uvIndex];
final vp = image.planes[2].bytes[uvIndex];
p.r = (yp + vp * 1436 / 1024 - 179).round().clamp(0, 255).toInt();
p.g = (yp - up * 46549 / 131072 + 44 - vp * 93604 / 131072 + 91)
.round()
.clamp(0, 255)
.toInt();
p.b = (yp + up * 1814 / 1024 - 227).round().clamp(0, 255).toInt();
}
return img;
}