Flutter-Android 如何在应用程序处于前台/后台时使用相机
当应用程序处于前台或后台时是否可以使用相机? 我尝试使用这两个包 flutter_foreground_task 和 flutter_background_service 但一旦启动摄像头流,我总是收到此错误消息未处理的异常: MissingPluginException(在通道plugins.flutter.io/camera上找不到方法availableCameras的实现)。
class FirstTaskHandler extends TaskHandler {
void initCamera() async {
final description = await availableCameras().then(
(cameras) => cameras.firstWhere(
(camera) => camera.lensDirection == CameraLensDirection.front,
),
);
final _cameraController = CameraController(
description,
ResolutionPreset.low,
enableAudio: false,
);
await _cameraController.initialize();
await Future.delayed(const Duration(milliseconds: 500));
_cameraController.startImageStream((img) async {
log("Image captures: ${img.width} x ${img.height} -- ${img.format.raw}");
});
}
@override
Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
initCamera();
}
@override
Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async { }
@override
Future<void> onDestroy(DateTime timestamp) async {
}
@override
void onButtonPressed(String id) {
}
}
is it possible to use the camera while the app is in the foreground or background?
I tried with these two packages flutter_foreground_task and flutter_background_service but i always get this error message once i start the camera stream Unhandled Exception: MissingPluginException(No implementation found for method availableCameras on channel plugins.flutter.io/camera).
class FirstTaskHandler extends TaskHandler {
void initCamera() async {
final description = await availableCameras().then(
(cameras) => cameras.firstWhere(
(camera) => camera.lensDirection == CameraLensDirection.front,
),
);
final _cameraController = CameraController(
description,
ResolutionPreset.low,
enableAudio: false,
);
await _cameraController.initialize();
await Future.delayed(const Duration(milliseconds: 500));
_cameraController.startImageStream((img) async {
log("Image captures: ${img.width} x ${img.height} -- ${img.format.raw}");
});
}
@override
Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
initCamera();
}
@override
Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async { }
@override
Future<void> onDestroy(DateTime timestamp) async {
}
@override
void onButtonPressed(String id) {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过编辑相机插件并使 Livestream 方法在应用程序位于前台/后台时工作来解决此问题。
这是编辑后的插件的链接 edited-flutter-camera-plugin,只需注意该版本可能会在某些功能中崩溃,因为我刚刚编辑了一些文件以使其符合我使 Livestream 方法在前台运行的目的
I fixed this issue by editing the camera plugin and making the Livestream method work when the app is in the foreground/background.
here is the link to the edited plugin edited-flutter-camera-plugin, just note that this version can be crashed in some functionalities because I just edited some files to make it fit my purpose of making the Livestream method works in the foreground