如何将 CameraImage 保存到图像文件?
How do I save a CameraImage to an image file?
我用 flutter 开发了一个移动应用程序。我做对象检测使用“controller.startImageStream”这个方法 return 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;
}
去做
var temp = new Uint8List(500);
var list = new List.from(temp);
您可以使用以下代码片段将 CameraImage YUV420 或 BGRA8888 转换为图像
要点代码:https://gist.github.com/Alby-o/fe87e35bc21d534c8220aed7df028e03
// imgLib -> Image package from https://pub.dartlang.org/packages/image
import 'package:image/image.dart' as imglib;
import 'package:camera/camera.dart';
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
imglib.Image _convertBGRA8888(CameraImage image) {
return imglib.Image.fromBytes(
image.width,
image.height,
image.planes[0].bytes,
format: imglib.Format.bgra,
);
}
// CameraImage YUV420_888 -> PNG -> Image (compresion:0, filter: none)
// Black
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;
}
@hunhunghan 有没有办法让 _convertYUV420(CameraImage image)
方法也有颜色?由于这些图像是黑色的,而来自 _convertBGRA8888(CameraImage image)
的图像是彩色的。
Flutter 现在有将 List<int>
转换为 Uint8List
的方法。您可以使用以下内容:
Uint8List.fromList(List<int> elements);
见https://api.flutter.dev/flutter/dart-typed_data/Uint8List/Uint8List.fromList.html
你可以这样做
Uint8List
是 dart:typed_data
的一部分,Dart 核心库之一。要使用它,请添加以下导入:
import 'dart:typed_data';
void main() {
final testData = List.filled(1000000, 1);
final uint8List = Uint8List.fromList(testData);
final sw1 = Stopwatch()..start();
final result1 = Stream.value(
List<int>.from(uint8List),
);
sw1.stop();
print('case1: ${sw1.elapsedMicroseconds} µs');
final sw2 = Stopwatch()..start();
final result2 = Stream.value(
uint8List.map((e) => [e]),
);
sw2.stop();
print('case2: ${sw2.elapsedMicroseconds} µs');
final sw3 = Stopwatch()..start();
final result3 = Stream.fromIterable(
uint8List.map((e) => [e]),
);
sw3.stop();
print('case3: ${sw3.elapsedMicroseconds} µs');
}
我用 flutter 开发了一个移动应用程序。我做对象检测使用“controller.startImageStream”这个方法 return 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;
}
去做
var temp = new Uint8List(500);
var list = new List.from(temp);
您可以使用以下代码片段将 CameraImage YUV420 或 BGRA8888 转换为图像
要点代码:https://gist.github.com/Alby-o/fe87e35bc21d534c8220aed7df028e03
// imgLib -> Image package from https://pub.dartlang.org/packages/image
import 'package:image/image.dart' as imglib;
import 'package:camera/camera.dart';
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
imglib.Image _convertBGRA8888(CameraImage image) {
return imglib.Image.fromBytes(
image.width,
image.height,
image.planes[0].bytes,
format: imglib.Format.bgra,
);
}
// CameraImage YUV420_888 -> PNG -> Image (compresion:0, filter: none)
// Black
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;
}
@hunhunghan 有没有办法让 _convertYUV420(CameraImage image)
方法也有颜色?由于这些图像是黑色的,而来自 _convertBGRA8888(CameraImage image)
的图像是彩色的。
Flutter 现在有将 List<int>
转换为 Uint8List
的方法。您可以使用以下内容:
Uint8List.fromList(List<int> elements);
见https://api.flutter.dev/flutter/dart-typed_data/Uint8List/Uint8List.fromList.html
你可以这样做
Uint8List
是 dart:typed_data
的一部分,Dart 核心库之一。要使用它,请添加以下导入:
import 'dart:typed_data';
void main() {
final testData = List.filled(1000000, 1);
final uint8List = Uint8List.fromList(testData);
final sw1 = Stopwatch()..start();
final result1 = Stream.value(
List<int>.from(uint8List),
);
sw1.stop();
print('case1: ${sw1.elapsedMicroseconds} µs');
final sw2 = Stopwatch()..start();
final result2 = Stream.value(
uint8List.map((e) => [e]),
);
sw2.stop();
print('case2: ${sw2.elapsedMicroseconds} µs');
final sw3 = Stopwatch()..start();
final result3 = Stream.fromIterable(
uint8List.map((e) => [e]),
);
sw3.stop();
print('case3: ${sw3.elapsedMicroseconds} µs');
}