Flutter相机插件拍摄暗照片的问题

19
我从flutter相机插件中得到的图像太暗了。
相机预览显示正确,但拍照后变得太暗了。
我搜索了一下,发现这与相机的FPS和曝光有关。
我该如何解决这个问题?
我需要在我的应用程序中显示相机预览并拍照。
请不要告诉我使用image_picker包。
设备:红米Note 4
Android操作系统:7.0
这是图像 暗图 这是代码

import 'dart:async';
import 'dart:io';

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart' show join;
import 'package:path_provider/path_provider.dart';

Future<void> main() async {
  // Obtain a list of the available cameras on the device.
  final cameras = await availableCameras();

  // Get a specific camera from the list of available cameras.
  final firstCamera = cameras.first;

  runApp(
    MaterialApp(
      theme: ThemeData.dark(),
      home: TakePictureScreen(
        // Pass the appropriate camera to the TakePictureScreen widget.
        camera: firstCamera,
      ),
    ),
  );
}

// A screen that allows users to take a picture using a given camera.
class TakePictureScreen extends StatefulWidget {
  final CameraDescription camera;

  const TakePictureScreen({
    Key key,
    @required this.camera,
  }) : super(key: key);

  @override
  TakePictureScreenState createState() => TakePictureScreenState();
}

class TakePictureScreenState extends State<TakePictureScreen> {
  CameraController _controller;
  Future<void> _initializeControllerFuture;

  @override
  void initState() {
    super.initState();
    // To display the current output from the Camera,
    // create a CameraController.
    _controller = CameraController(
      // Get a specific camera from the list of available cameras.
      widget.camera,
      // Define the resolution to use.
      ResolutionPreset.medium,
    );

    // Next, initialize the controller. This returns a Future.
    _initializeControllerFuture = _controller.initialize();
  }

  @override
  void dispose() {
    // Dispose of the controller when the widget is disposed.
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Take a picture')),
      // Wait until the controller is initialized before displaying the
      // camera preview. Use a FutureBuilder to display a loading spinner
      // until the controller has finished initializing.
      body: FutureBuilder<void>(
        future: _initializeControllerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            // If the Future is complete, display the preview.
            return CameraPreview(_controller);
          } else {
            // Otherwise, display a loading indicator.
            return Center(child: CircularProgressIndicator());
          }
        },
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.camera_alt),
        // Provide an onPressed callback.
        onPressed: () async {
          // Take the Picture in a try / catch block. If anything goes wrong,
          // catch the error.
          try {
            // Ensure that the camera is initialized.
            await _initializeControllerFuture;

            // Construct the path where the image should be saved using the
            // pattern package.
            final path = join(
              // Store the picture in the temp directory.
              // Find the temp directory using the `path_provider` plugin.
              (await getTemporaryDirectory()).path,
              '${DateTime.now()}.png',
            );

            // Attempt to take a picture and log where it's been saved.
            await _controller.takePicture(path);

            // If the picture was taken, display it on a new screen.
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => DisplayPictureScreen(imagePath: path),
              ),
            );
          } catch (e) {
            // If an error occurs, log the error to the console.
            print(e);
          }
        },
      ),
    );
  }
}

// A widget that displays the picture taken by the user.
class DisplayPictureScreen extends StatelessWidget {
  final String imagePath;

  const DisplayPictureScreen({Key key, this.imagePath}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Display the Picture')),
      // The image is stored as a file on the device. Use the `Image.file`
      // constructor with the given path to display the image.
      body: Image.file(File(imagePath)),
    );
  }
}


我很乐意帮助你,但我已经尝试了你的代码,在我的 OnePlus 6T(搭载 Android 10)上,照片效果良好,不像你分享的示例那样暗。我无法重现你的问题。你使用了哪些其他相机包,它们是否都有同样的问题? - J. S.
2
你有没有看过我的评论?你为这个问题设置了赏金,但甚至没有回复你收到的唯一评论。 - J. S.
我认为这可能是摄像头问题,如果不是的话,请尝试使用image_picker包https://pub.dev/packages/image_picker。 - Omar Sherif
我在 Pixel 3 上尝试了一下,照片也像 Joao 提到的那样正常。然而,这可能是针对某些设备的,https://github.com/flutter/flutter/issues/19038 和 https://github.com/flutter/flutter/issues/26079 你能否也在那里发布你的问题?谢谢! - TWL
如果您使用平台特定的代码并遵循此帖子中的答案:https://dev59.com/C6rka4cB1Zd3GeqPdGmK,会发生什么? - Brandon Pillay
显示剩余4条评论
3个回答

1

对于我来说有效的一种方法是使用相机插件: 拍两张照片。 第一张照片为第二张照片提供时间来获得适当的曝光和焦点。

final image = await controller.takePicture(); // is not used
final image2 = await controller.takePicture(); 

1
我认为这不是延迟的问题,如果曝光没有处理好,图像就会变暗。此外,曝光需要焦点预捕获才能工作,而官方插件现在没有处理这个功能。
你可以使用这个插件:CamerAwesome
官方插件已经被放弃了。这个插件包括闪光灯、变焦、自动对焦、曝光等功能,并且不需要初始化。它使用值通知器直接在预览中更改数据,就像这样:
  // init Notifiers
  ValueNotifier<CameraFlashes> _switchFlash = ValueNotifier(CameraFlashes.NONE);
  ValueNotifier<Sensors> _sensor = ValueNotifier(Sensors.BACK);
  ValueNotifier<Size> _photoSize = ValueNotifier(null);

  @override
  Widget build(BuildContext context) {
    return CameraAwesome(
      onPermissionsResult: (bool result) { }
      selectDefaultSize: (List<Size> availableSizes) => Size(1920, 1080),
      onCameraStarted: () { },
      onOrientationChanged: (CameraOrientations newOrientation) { },
      zoom: 0.64,
      sensor: _sensor,
      photoSize: _photoSize,
      switchFlashMode: _switchFlash,
      orientation: DeviceOrientation.portraitUp,
      fitted: true,
    );
  };

获取CameraAwesome包的相同输出,对我没有帮助。谢谢。 - Kamlesh

1

在拍照前加入延迟。

Future.delayed(const Duration(milliseconds: 500), () {
_controller.takePicture(path);
});

你测试过这个吗? - shubham chhimpa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接