【发布时间】:2026-02-19 18:55:01
【问题描述】:
我用 Flutter 开发了一个移动应用程序。我做对象检测使用“controller.startImageStream”这个方法返回CameraImage,我使用对象检测。我想保存这个图像文件。我试图将此文件转换为 List 和 jpg 文件进行保存。但 uint8list 无法转换为 List。这种结构是真的吗?如果您知道我的问题的不同解决方案,请分享给我。
这是我的视频流方法;
startVideoStreaming() {
if (cameras == null || cameras.length < 1) {
print('No camera is found');
} else {
controller = new CameraController(
cameras[0],
ResolutionPreset.medium,
);
if(!_busy){
controller.initialize().then((_) {
print("model yükleme bitmiş stream dinleme başlıyor ");
controller.startImageStream((CameraImage img){
print("img format: ${img.format} planes: ${img.planes}");
List<int> imageBytes = [];
img.planes.map((plane) {
imageBytes.addAll(plane.bytes.toList());
});
// call save image file method
saveImageFile(imageBytes).then((res) => {
print("save image file successfull filepath: $res")
}).catchError((err) => {
print("error on save image file error: $err")
});
if(!isDetecting){
isDetecting = true;
print("Tflite'a stream gönderildi");
Tflite.detectObjectOnFrame(
bytesList: img.planes.map((plane) {
return plane.bytes;
}).toList(),
model: "SSDMobileNet",
imageHeight: img.height,
imageWidth: img.width,
imageMean: 127.5,
imageStd: 127.5,
numResultsPerClass: 1,
threshold: 0.4,
).then((recognitions) {
int endTime = new DateTime.now().millisecondsSinceEpoch;
setState(() {
_recognitions=recognitions;
});
print("Recognitions: $recognitions");
isDetecting = false;
});
}
});
});
}
}
}
这是我的图片保存方法;
Future<String> saveImageFile(imageBytes) async {
final Directory extDir = await getApplicationDocumentsDirectory();
final String dirPath = '${extDir.path}/Pictures/flutter_test';
await Directory(dirPath).create(recursive: true);
final String filePath = '$dirPath/${timestamp()}.jpg';
if (controller.value.isTakingPicture) {
// A capture is already pending, do nothing.
return null;
}
try {
File file = new File(filePath);
file.writeAsBytes(imageBytes);
print("finish image saved $imageBytes");
} on CameraException catch (e) {
_showCameraException(e);
return null;
}
return filePath;
}
【问题讨论】:
-
Uint8list实现了List<int>- 换句话说:它是一个List<int> -
如果我直接发送发送 CameraImage 类型,file.writeAsBytes() 方法返回此代码:发生异常。 _TypeError(类型“CameraImage”不是类型“List
”的子类型) -
如果我使用
img.planes.map((plane) { return plane.bytes; }).toList();这是retunr List你知道这个文件类型像图片一样保存吗? -
这并不容易。见:*.com/questions/54312915/…
标签: flutter dart typeconverter uint8list