decode YUV 420 888 format image to Dart Image
时间: 2023-12-24 21:05:04 浏览: 28
To decode a YUV 420 888 format image to a Dart Image, you can use the following steps:
1. Convert the YUV 420 888 data to RGB data.
2. Create a new Image object with the RGB data.
Here's an example code snippet that demonstrates how to do this:
```dart
import 'dart:typed_data';
import 'package:image/image.dart';
// Assume that `yuvData`, `uData`, and `vData` are Uint8List objects containing the YUV 420 888 data.
// Calculate the size of the image.
final width = (yuvData.length * 3 / 2).sqrt();
final height = width * 3 / 4;
// Convert the YUV data to RGB data.
final rgbData = Uint8List(width.toInt() * height.toInt() * 3);
int p = 0;
for (int j = 0; j < height.toInt(); j++) {
final yp = j * width.toInt();
final up = (j >> 1) * (width.toInt() >> 1);
final vp = up;
for (int i = 0; i < width.toInt(); i++) {
final y = yuvData[yp + i] - 16;
final u = uData[up + (i >> 1)] - 128;
final v = vData[vp + (i >> 1)] - 128;
rgbData[p++] = (y + 1.402 * v).toInt().clamp(0, 255);
rgbData[p++] = (y - 0.34414 * u - 0.71414 * v).toInt().clamp(0, 255);
rgbData[p++] = (y + 1.772 * u).toInt().clamp(0, 255);
}
}
// Create a new Image object with the RGB data.
final image = Image.fromBytes(width.toInt(), height.toInt(), rgbData);
```
Note that this code assumes that the YUV 420 888 data is in planar format, where the Y, U, and V components are stored in separate arrays. If your YUV 420 888 data is in interleaved format, where the U and V components are interleaved with the Y component, you will need to modify the code accordingly.