编辑问题似乎是,我试图使用的代码在隔离器中不起作用。当我在等高线之外尝试相同的颂歌时,这是可行的。所以可能应该检查颤振github上的问题,或者检查一个nex的问题。edit2没有找到这个问题,只是提交了一个:
我可以用这段代码( CameraImage )将一个png转换为png,然后用图像小部件显示png,但是png转换非常慢。我希望将CameraImage转换为ui.Image,然后使用RawImage小部件来显示它。听起来很有可能。但是,当我尝试这段代码时,在ui.decodeImageFromPixels行上会出现一个错误:
Future<ui.Image> makeUiImage(List<int> pixels,int width,int height) {
final c = Completer<ui.Image>();
ui.decodeImageFromPixels(
pixels,
width,
height,
ui.PixelFormat.rgba8888,
c.complete,
);
return c.future;
}
Future<ui.Image> convertCameraImageToUiImage(CameraImage image) async {
int startTime = DateTime.now().millisecondsSinceEpoch;
int time;
imglib.Image img = convertCameraImage(image);
time = DateTime.now().millisecondsSinceEpoch;
print("Converted in "+(time-startTime).toString()+"ms img "+img.toString()+" ("+img.width.toString()+","+img.height.toString()+")");
startTime=time;
ui.Image ret = await makeUiImage(img.getBytes(), image.width, image.height);
return ret;
}E/颤振( 1194):错误:颤振/运行时/dart_solate.cc(915)
未处理异常: E/flutter ( 1194):error:本机函数'instantiateImageCodec‘(5个参数)无法找到
E/颤振( 1194):#0 decodeImageFromPixels。
(飞镖:ui/油漆。飞镖:1740:36)E/颤振( 1194):#1 _futurize
(飞镖:ui/ decodeImageFromPixels .飞镖:4296:34)E/颤振( 1194):#2
1194):#3 makeUiImage
(package:flutterapp/utils/image_converter.dart:37:3) E/颤振(
1194):#4 convertCameraImageToUiImage
(package:flutterapp/utils/image_converter.dart:54:24) E/颤振(
1194):#5 processCameraImageIsolate。
(package:flutterapp/cameraoverlay2.dart:448:5) E/颤振( 1194):#6 _RootZone.runUnaryGuarded (飞镖:异步/zone.dart:1316:10)E/颤振( 1194):#7
_BufferingStreamSubscription._sendData
(dart:异步/stream_INST.DAT:338:11) E/flutter ( 1194):#8 _BufferingStreamSubscription._add (dart:dart/stream_INST.DAT:265:7) E/flutter ( 1194):#9 _SyncStreamControllerDispatch._sendData
(省:异步/流_能控器:766:19)E/颤振( 1194):#10 _StreamController._add (飞镖:异步/流_能控器。飞镖:642:7)E/颤振( 1194):#11 _StreamController.add
(省:异步/流_能控器:588:5)E/颤振( 1194):#12
_RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
发布于 2021-03-04 14:27:46
下面是来自此链接的代码片段,适合我使用:https://github.com/flutter/flutter/issues/26348#issuecomment-694092314
使用以下方法创建一个类或文件:
/// imgLib -> Image package from https://pub.dartlang.org/packages/image
static Future<List<int>> convertImagetoPng(CameraImage image) async {
try {
imglib.Image img;
if (image.format.group == ImageFormatGroup.yuv420) {
img = _convertYUV420(image);
} else if (image.format.group == ImageFormatGroup.bgra8888) {
img = _convertBGRA8888(image);
}
imglib.PngEncoder pngEncoder = new imglib.PngEncoder();
// Convert to png
List<int> png = pngEncoder.encodeImage(img);
return png;
} catch (e) {
print(">>>>>>>>>>>> ERROR:" + e.toString());
}
return null;
}
/// CameraImage BGRA8888 -> PNG
/// Color
static imglib.Image _convertBGRA8888(CameraImage image) {
return imglib.Image.fromBytes(
(image.planes[0].bytesPerRow / 4).round(),
image.height,
image.planes[0].bytes,
format: imglib.Format.bgra,
);
}
/// CameraImage YUV420_888 -> PNG -> Image (compresion:0, filter: none)
/// Black
static imglib.Image _convertYUV420(CameraImage image) {
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;
}然后在您的方法中使用此方法获取ui.Image,使用comp.future获取ui.Image
Completer<ui.Image> comp = Completer();
ui.decodeImageFromList(
await convertImagetoPng(cameraImage),
(result) {
comp.complete(result);
});https://stackoverflow.com/questions/61428123
复制相似问题