Flutter-Android 如何在应用程序处于前台/后台时使用相机

发布于 2025-01-16 13:45:11 字数 1433 浏览 1 评论 0原文

当应用程序处于前台或后台时是否可以使用相机? 我尝试使用这两个包 flutter_foreground_taskflutter_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追星践月 2025-01-23 13:45:12

我通过编辑相机插件并使 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文