在Flutter上使用相机是否有最终解决方案?

时间:2019-07-15 13:06:38

标签: flutter

我需要将相机嵌入到我的flutter应用程序中,至少要具有触摸对焦能力,并且要有最大能力来控制其他参数,例如亮度,ISO等。

我尝试使用camera软件包,但没有提供所需的API。我建议我必须使用通道才能完全控制camera2 API,但我仍然希望有另一个(更简单的)解决方案。 任何帮助表示赞赏!

现在我确实是这样的:

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

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

    availableCameras().then((cameras) {
      setState(() {
        _firstCamera = cameras.first;
        _controller = CameraController(
          _firstCamera,
          ResolutionPreset.high,
        );

        _initializeControllerFuture = _controller.initialize();
      });
    });
  }

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

  @override
  Widget build(BuildContext context) {
    if (_firstCamera == null) {
      // This is what we show while we're loading
      return new Container();
    }
    return Scaffold(
      appBar: AppBar(
        title: Text('Take a picture'),
        textTheme: TextTheme(
            title: TextStyle(
                color: AppColors.blackgradient,
                fontSize: 22,
                letterSpacing: 1.3)),
      ),
      body: FutureBuilder<void>(
        future: _initializeControllerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return CameraPreview(_controller);
          } else {
            return Center(child: CircularProgressIndicator());
          }
        },
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.camera_alt),
        onPressed: () async {
          try {
            await _initializeControllerFuture;

            final path = join(
              (await getTemporaryDirectory()).path,
              '${DateTime.now()}.png',
            );

            await _controller.takePicture(path);

            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => DisplayPictureScreen(imagePath: path),
              ),
            );
          } catch (e) {
            print(e);
          }
        },
      ),
    );
  }
}

0 个答案:

没有答案