创建用于人脸检测的应用程序。使用相机插件来捕获图片。以下是单击按钮时捕获图像的代码。 我想捕获图像并将其转换为InputImage 格式。并进行导航。转换代码不起作用。
floatingActionButton: FloatingActionButton(
onPressed: () async {
await controller!.takePicture().then((value) async {
// Convert the image to a file
final File _capimg = File(value.path);
final bytes = await _capimg.readAsBytes();
final directory = await getTemporaryDirectory();
final path = '${directory.path}/${DateTime.now().millisecondsSinceEpoch}.jpg';
final file = File(path);
await file.writeAsBytes(bytes);
print('The captured image path is: ${_capimg.path}');
setState(() {
_imageFile = _capimg;
_imagePath = path;
});
final File convimg = File('path/to/image.jpg');
if (!convimg.existsSync()) {
convimg.createSync();
}
// Convert the file to InputImage
final inputImage = InputImage.fromFilePath(_imagePath);
print('The input image is: ${inputImage.metadata?.size}');
processCameraImage(inputImage);
});
},
tooltip: 'Capture',
child: Icon(Icons.camera_alt),
),
下面是processCameraImage方法。
Future<void> processCameraImage( InputImage inputImage) async {
if (_isCameraReady && !_detectingFaces) {
try {
setState(() {
_detectingFaces = true;
});
final faces = await _faceDetector.processImage(_inputImage);
if (faces.isNotEmpty) {
setState(() {
_faces = faces;
});
}
} catch (e) {
print(e);
} finally {
setState(() {
_detectingFaces = false;
});
}
}
}
}
以下是日志:-
The Flutter DevTools debugger and profiler on RMX3231 is available at: http://127.0.0.1:9100?uri=http://127.0.0.1:61941/hYEWHKXbVS4=/
W/Camera (25390): The selected imageFormatGroup is not supported by Android. Defaulting to yuv420
I/Camera (25390): startPreview
D/IMGGralloc(25390): InsertBlankEntry: SPRD_DATA_SPACE ui64Stamp:2486938,eYUVColorSpace:2, yuv_info:2
D/IMGGralloc(25390): Gralloc Register w:1280, h:720, f:0x22, usage:0x20000106, ui64Stamp:2486938, sSize:1384448, line = 2335
D/IMGGralloc(25390): InsertBlankEntry: SPRD_DATA_SPACE ui64Stamp:2486942,eYUVColorSpace:2, yuv_info:2
D/IMGGralloc(25390): Gralloc Register w:1280, h:720, f:0x22, usage:0x20000106, ui64Stamp:2486942, sSize:1384448, line = 2335
D/IMGGralloc(25390): InsertBlankEntry: SPRD_DATA_SPACE ui64Stamp:2486947,eYUVColorSpace:2, yuv_info:2
D/IMGGralloc(25390): Gralloc Register w:1280, h:720, f:0x22, usage:0x20000106, ui64Stamp:2486947, sSize:1384448, line = 2335
D/ProfileInstaller(25390): Installing profile for com.example.flutterapp.facerecognition
D/IMGGralloc(25390): InsertBlankEntry: SPRD_DATA_SPACE ui64Stamp:2486955,eYUVColorSpace:2, yuv_info:2
D/IMGGralloc(25390): Gralloc Register w:1280, h:720, f:0x22, usage:0x20000106, ui64Stamp:2486955, sSize:1384448, line = 2335
D/IMGGralloc(25390): Gralloc Register w:720, h:1512, f:0x1, usage:0xb00, ui64Stamp:2486959, sSize:4378624, line = 2335
D/IMGGralloc(25390): Gralloc Register w:720, h:1512, f:0x1, usage:0xb00, ui64Stamp:2486963, sSize:4378624, line = 2335
I/Camera (25390): runPrecaptureSequence
I/Camera (25390): refreshPreviewCaptureSession
D/CameraCaptureCallback(25390): CameraCaptureCallback | state: STATE_WAITING_PRECAPTURE_START | afState: 2 | aeState: 2
D/CameraCaptureCallback(25390): CameraCaptureCallback | state: STATE_WAITING_PRECAPTURE_DONE | afState: 2 | aeState: 2
I/Camera (25390): captureStillPicture
D/Camera (25390): Updating builder with feature: ExposureLockFeature
D/Camera (25390): Updating builder with feature: ExposurePointFeature
D/Camera (25390): Updating builder with feature: ZoomLevelFeature
D/Camera (25390): Updating builder with feature: AutoFocusFeature
D/Camera (25390): Updating builder with feature: NoiseReductionFeature
I/Camera (25390): updateNoiseReduction | currentSetting: fast
D/Camera (25390): Updating builder with feature: FocusPointFeature
D/Camera (25390): Updating builder with feature: ResolutionFeature
D/Camera (25390): Updating builder with feature: SensorOrientationFeature
D/Camera (25390): Updating builder with feature: FlashFeature
D/Camera (25390): Updating builder with feature: ExposureOffsetFeature
D/Camera (25390): Updating builder with feature: FpsRangeFeature
I/Camera (25390): sending capture request
D/CameraCaptureCallback(25390): CameraCaptureCallback | state: STATE_CAPTURING | afState: 2 | aeState: 2
D/IMGGralloc(25390): Gralloc Register w:1596606, h:1, f:0x21, usage:0x6, ui64Stamp:2486996, sSize:1597440, line = 2335
D/CameraCaptureCallback(25390): CameraCaptureCallback | state: STATE_CAPTURING | afState: 2 | aeState: 2
D/CameraCaptureCallback(25390): CameraCaptureCallback | state: STATE_CAPTURING | afState: 2 | aeState: 2
I/Camera (25390): onImageAvailable
I/Camera (25390): unlockAutoFocus
I/Camera (25390): refreshPreviewCaptureSession
I/flutter (25390): The captured image path is: /data/user/0/com.example.flutterapp.facerecognition/cache/CAP4679855200037562703.jpg
E/flutter (25390): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PathNotFoundException: Cannot create file, path = 'path/to/image.jpg'
(OS Error: No such file or directory, errno = 2)
E/flutter (25390): #0 _File.throwIfError (dart:io/file_impl.dart:675:7)
E/flutter (25390): #1 _File.createSync (dart:io/file_impl.dart:306:5)
E/flutter (25390): #2 _MyHomePageState.build.<anonymous closure>.<anonymous closure> (package:facerecognition/main.dart:142:23)
E/flutter (25390): <asynchronous suspension>
E/flutter (25390): #3 _MyHomePageState.build.<anonymous closure> (package:facerecognition/main.dart:125:11)
E/flutter (25390): <asynchronous suspension>
E/flutter (25390):
I/facerecognitio(25390): Explicit concurrent copying GC freed 84019(2578KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 3386KB/6773KB, paused 180us total 71
.663ms
I/Camera (25390): runPrecaptureSequence
I/Camera (25390): refreshPreviewCaptureSession
D/CameraCaptureCallback(25390): CameraCaptureCallback | state: STATE_WAITING_PRECAPTURE_START | afState: 2 | aeState: 2
D/CameraCaptureCallback(25390): CameraCaptureCallback | state: STATE_WAITING_PRECAPTURE_DONE | afState: 2 | aeState: 2
I/Camera (25390): captureStillPicture
D/Camera (25390): Updating builder with feature: ExposureLockFeature
D/Camera (25390): Updating builder with feature: ExposurePointFeature
D/Camera (25390): Updating builder with feature: ZoomLevelFeature
D/Camera (25390): Updating builder with feature: AutoFocusFeature
D/Camera (25390): Updating builder with feature: NoiseReductionFeature
I/Camera (25390): updateNoiseReduction | currentSetting: fast
D/Camera (25390): Updating builder with feature: FocusPointFeature
D/Camera (25390): Updating builder with feature: ResolutionFeature
D/Camera (25390): Updating builder with feature: SensorOrientationFeature
D/Camera (25390): Updating builder with feature: FlashFeature
D/Camera (25390): Updating builder with feature: ExposureOffsetFeature
D/Camera (25390): Updating builder with feature: FpsRangeFeature
I/Camera (25390): sending capture request
D/CameraCaptureCallback(25390): CameraCaptureCallback | state: STATE_CAPTURING | afState: 2 | aeState: 2
D/CameraCaptureCallback(25390): CameraCaptureCallback | state: STATE_CAPTURING | afState: 2 | aeState: 2
D/CameraCaptureCallback(25390): CameraCaptureCallback | state: STATE_CAPTURING | afState: 2 | aeState: 2
D/CameraCaptureCallback(25390): CameraCaptureCallback | state: STATE_CAPTURING | afState: 2 | aeState: 2
I/Camera (25390): onImageAvailable
I/Camera (25390): unlockAutoFocus
I/Camera (25390): refreshPreviewCaptureSession
I/flutter (25390): The captured image path is: /data/user/0/com.example.flutterapp.facerecognition/cache/CAP8634855078985179270.jpg
E/flutter (25390): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PathNotFoundException: Cannot create file, path = 'path/to/image.jpg'
(OS Error: No such file or directory, errno = 2)
E/flutter (25390): #0 _File.throwIfError (dart:io/file_impl.dart:675:7)
E/flutter (25390): #1 _File.createSync (dart:io/file_impl.dart:306:5)
E/flutter (25390): #2 _MyHomePageState.build.<anonymous closure>.<anonymous closure> (package:facerecognition/main.dart:142:23)
E/flutter (25390): <asynchronous suspension>
E/flutter (25390): #3 _MyHomePageState.build.<anonymous closure> (package:facerecognition/main.dart:125:11)
E/flutter (25390): <asynchronous suspension>
E/flutter (25390):
错误显示“无法创建文件,路径 = 'path/to/image.jpg'”
您的代码中确实有“path/to/image.jpg”。
final File convimg = File('path/to/image.jpg');
将其更改为指向临时目录中或您具有写入权限的其他位置中的文件。
final File convimg = File('${directory.path}/${DateTime.now().millisecondsSinceEpoch}-converted.jpg');