我正在创建一个扫描器,需要为相机预览实现正方形叠加层。从相机预览获取图像流并将其发送到API后,我需要在将其发送到API之前对从相机预览中拍摄的 cameraImage 进行正方形裁剪。
我只有 cameraImage - 它是 YUV 420 格式 - 如何编程裁剪它?
```
Uint8List getBytes() {
final WriteBuffer allBytes = WriteBuffer();
for (final Plane plane in cameraImage.planes) {
allBytes.putUint8List(plane.bytes);
}
return allBytes.done().buffer.asUint8List();
}
int divider = 1; // for first plane: Y
for (final Plane plane in cameraImage.planes) {
for (int i = 0; i < cameraImage.height ~/ divider; i++) {
for (int j = 0; j < cameraImage.width ~/ divider; j++) {
allBytes.putUint8(plane.bytes[j + i * cameraImage.width ~/ divider]);
}
}
divider = 2; // for planes U and V
}
既然你在这里,我认为你已经知道如何裁剪了:
int divider = 1; // for first plane: Y
for (final Plane plane in cameraImage.planes) {
for (int i = cropTop ~/ divider; i < cropBottom ~/ divider; i++) {
for (int j = cropLeft ~/ divider; j < cropRight ~/ divider; j++) {
allBytes.putUint8(plane.bytes[j + i * cameraImage.width ~/ divider]);
}
}
divider = 2; // for planes U and V
}
在这里,裁剪*变量是从完整图像中计算出来的。
这是理论:这段代码没有考虑相机方向、奇数大小的可能副作用以及性能问题。但这是一个总体的想法。