CameraPreview在开始时未显示,但在热重载后

时间:2019-06-17 02:41:26

标签: android flutter dart

启动该应用程序时,它仅显示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.

1 个答案:

答案 0 :(得分:0)

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