这个函数太慢了。So FlutterCameraImage效率转换为TensorImage在飞镖吗?
var img = imglib.Image(image.width, image.height); // Create Image buffer
Plane plane = image.planes[0];
const int shift = (0xFF << 24);
// Fill image buffer with plane[0] from YUV420_888
for (int x = 0; x < image.width; x++) {
for (int planeOffset = 0;
planeOffset < image.height * image.width;
planeOffset += image.width) {
final pixelColor = plane.bytes[planeOffset + x];
// color: 0x FF FF FF FF
// A B G R
// Calculate pixel color
var newVal =
shift | (pixelColor << 16) | (pixelColor << 8) | pixelColor;
img.data[planeOffset + x] = newVal;
}
}
return img;
}```
似乎你的for循环是低效的。整行数据(具有相同的placeOffset,不同的x)将被一次缓存,因此切换两个循环的顺序会更快。
for (int y = 0; y < image.height; y++) {
for (int x = 0; x < image.width; x++) {
final pixelColor = plane.bytes[y * image.width + x];
// ...
}
}
但是,您的代码似乎没有从实际的相机流读取。请参考这个线程将cameraiimage转换为Image。
如何在Flutter中转换相机图像到图像?