【问题标题】:CameraPreview not shown at beginning, but after hot reloadCameraPreview 在开始时未显示,但在热重新加载后
【发布时间】:2019-06-17 13:23:58
【问题描述】:

当这个应用程序启动时,它只显示 CircularProgressIndicator,而不是相机流。但是,如果触发了热重载,则会显示 CameraPreview,但仅此而已。在两个不同的 Android 设备上进行了测试。

这是与 Future 的某种竞争条件吗?我错过了什么?

附上代码:

import 'dart:async';
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';

Future<void> main() async {
  runApp(MaterialApp(home: CameraTestHome()));
}

class CameraTestHome extends StatefulWidget {
  @override
  _CameraTestHomeState createState() {
    return _CameraTestHomeState();
  }
}

class _CameraTestHomeState extends State<CameraTestHome> {
  CameraController _cameraController;
  Future<void> _initializeCameraControllerFuture;

  @override
  void initState() {
    super.initState();
    _initializeCamera();
  }

  void _initializeCamera() async {
    CameraDescription cameraDescription =
        await getCamera(CameraLensDirection.front);

    _cameraController = CameraController(
      cameraDescription,
      ResolutionPreset.high,
    );

    _initializeCameraControllerFuture = _cameraController.initialize();
    _initializeCameraControllerFuture
        .then((_) => _cameraController.startImageStream((CameraImage image) {
              // Some Image Processing to be inserted.
            }));
  }

  Future<CameraDescription> getCamera(CameraLensDirection direction) async {
    return await availableCameras().then(
      (List<CameraDescription> cameras) => cameras.firstWhere(
            (CameraDescription camera) => camera.lensDirection == direction,
          ),
    );
  }

  @override
  void dispose() {
    _cameraController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.orange,
      appBar: AppBar(title: Text('CameraTest')),
      body: FutureBuilder<void>(
        future: _initializeCameraControllerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return Center(
              child: AspectRatio(
                aspectRatio: _cameraController.value.aspectRatio,
                child: CameraPreview(_cameraController),
              ),
            );
          } else {
            return Center(child: CircularProgressIndicator());
          }
        },
      ),
    );
  }
}

调试日志:

Launching lib/main.dart on A0001 in debug mode...
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
Built build/app/outputs/apk/debug/app-debug.apk.
I/CameraManagerGlobal(24135): Connecting to camera service
W/LegacyMetadataMapper(24135): mapScalerStreamConfigs - failed to find any preview size matching JPEG aspect ratio 1.3487179
I/CameraManager(24135): Using legacy camera HAL.
I/CameraDeviceState(24135): Legacy camera service transitioning to state CONFIGURING
I/RequestThread-1(24135): Configure outputs: 2 surfaces configured.
D/Camera  (24135): app passed NULL surface
I/RequestThread-1(24135): configureOutputs - set take picture size to 2592x1944
W/Adreno-EGL(24135): <qeglDrvAPI_eglGetConfigAttrib:607>: EGL_BAD_ATTRIBUTE
D/vndksupport(24135): Loading /vendor/lib/hw/gralloc.msm8974.so from current namespace instead of sphal namespace.
W/Adreno-EGL(24135): <qeglDrvAPI_eglQueryContext:4370>: EGL_BAD_ATTRIBUTE
I/CameraDeviceState(24135): Legacy camera service transitioning to state IDLE
I/RequestQueue(24135): Repeating capture request set.
W/LegacyRequestMapper(24135): convertRequestMetadata - control.awbRegions setting is not supported, ignoring value
W/LegacyRequestMapper(24135): Only received metering rectangles with weight 0.
W/LegacyRequestMapper(24135): convertRequestToMetadata - Ignoring android.lens.focusDistance false, only 0.0f is supported
I/RequestQueue(24135): Repeating capture request cancelled.
I/Gralloc2(24135): Adding additional valid usage bits: 0x2200000
D/vndksupport(24135): Loading /vendor/lib/hw/gralloc.msm8974.so from current namespace instead of sphal namespace.
I/CameraDeviceState(24135): Legacy camera service transitioning to state CAPTURING
I/CameraDeviceState(24135): Legacy camera service transitioning to state IDLE
I/CameraDeviceState(24135): Legacy camera service transitioning to state CONFIGURING
I/RequestThread-1(24135): Configure outputs: 2 surfaces configured.
D/vndksupport(24135): Loading /vendor/lib/hw/gralloc.msm8974.so from current namespace instead of sphal namespace.
D/Camera  (24135): app passed NULL surface
W/Adreno-EGL(24135): <qeglDrvAPI_eglGetConfigAttrib:607>: EGL_BAD_ATTRIBUTE
D/vndksupport(24135): Loading /vendor/lib/hw/gralloc.msm8974.so from current namespace instead of sphal namespace.
W/Adreno-EGL(24135): <qeglDrvAPI_eglGetConfigAttrib:607>: EGL_BAD_ATTRIBUTE
W/Adreno-EGL(24135): <qeglDrvAPI_eglQueryContext:4370>: EGL_BAD_ATTRIBUTE
I/CameraDeviceState(24135): Legacy camera service transitioning to state IDLE
W/game.blink_gam(24135): Long monitor contention with owner main (24135) at void android.hardware.camera2.impl.CameraDeviceImpl.createCaptureSessionInternal(android.hardware.camera2.params.InputConfiguration, java.util.List, android.hardware.camera2.CameraCaptureSession$StateCallback, java.util.concurrent.Executor, int, android.hardware.camera2.CaptureRequest)(CameraDeviceImpl.java:636) waiters=0 in void android.hardware.camera2.impl.CameraDeviceImpl$CameraDeviceCallbacks.onCaptureStarted(android.hardware.camera2.impl.CaptureResultExtras, long) for 337ms
I/RequestQueue(24135): Repeating capture request set.
W/LegacyRequestMapper(24135): convertRequestMetadata - control.awbRegions setting is not supported, ignoring value
W/LegacyRequestMapper(24135): Only received metering rectangles with weight 0.
W/LegacyRequestMapper(24135): convertRequestToMetadata - Ignoring android.lens.focusDistance false, only 0.0f is supported
D/vndksupport(24135): Loading /vendor/lib/hw/gralloc.msm8974.so from current namespace instead of sphal namespace.
I/CameraDeviceState(24135): Legacy camera service transitioning to state CAPTURING
D/vndksupport(24135): Loading /vendor/lib/hw/gralloc.msm8974.so from current namespace instead of sphal namespace.
W/System  (24135): A resource failed to call release.
D/vndksupport(24135): Loading /vendor/lib/hw/gralloc.msm8974.so from current namespace instead of sphal namespace.
D/vndksupport(24135): Loading /vendor/lib/hw/gralloc.msm8974.so from current namespace instead of sphal namespace.
D/vndksupport(24135): Loading /vendor/lib/hw/gralloc.msm8974.so from current namespace instead of sphal namespace.
D/vndksupport(24135): Loading /vendor/lib/hw/gralloc.msm8974.so from current namespace instead of sphal namespace.
D/vndksupport(24135): Loading /vendor/lib/hw/gralloc.msm8974.so from current namespace instead of sphal namespace.
D/vndksupport(24135): Loading /vendor/lib/hw/gralloc.msm8974.so from current namespace instead of sphal namespace.
I/game.blink_gam(24135): Background concurrent copying GC freed 1200(144KB) AllocSpace objects, 14(32MB) LOS objects, 49% free, 22MB/44MB, paused 8.573ms total 52.028ms
I/game.blink_gam(24135): Background concurrent copying GC freed 1216(143KB) AllocSpace objects, 14(32MB) LOS objects, 50% free, 22MB/44MB, paused 5.317ms total 39.770ms
Application finished.

【问题讨论】:

    标签: android flutter dart


    【解决方案1】:

    好的,FutureBuilder 依赖于 _initializeCameraControllerFuture,它在调用 build() 时尚未初始化。 在解决问题之前简单地初始化_initializeCameraControllerFuture。

    【讨论】:

      猜你喜欢
      • 2019-12-23
      • 2020-07-03
      • 1970-01-01
      • 2011-01-01
      • 2019-06-14
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      相关资源
      最近更新 更多