Flutter camera autofocus timeout error

Asked

Viewed 37 times

0

I’m using the Flutter camera to create a camera component. The component works relatively well, if not for the fact that when I try to take a blurred footo, the component returns a timeout error. Apparently the camera Flutter tries to focus the photo in question and ends up resulting in this error. I’m already some time trying to solve this problem. I can’t find anything online that works either.

This is the camera component I developed:

class CameraPage extends StatefulWidget {
  @override
  _CameraPageState createState() => _CameraPageState();
}

class _CameraPageState extends State<CameraPage> {
  CameraController _controller;
  Future<void> _controllerInitializer;
  bool _toggleFlash = false;

  Future<CameraDescription> _getCamera() async {
    final c = await availableCameras();
    return c.first;
  }

  void onCapturePressed() async {
    try {
      final image = await _controller.takePicture();
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => ConfirmPhotoPage(image)),
      );
    } catch (e) {
      _showErrorModal(
        'Error when trying to take a photo',
        'Make sure your camera is focused before taking the photo',
      );
    }
  }

  @override
  void initState() {
    super.initState();
    _getCamera().then((camera) {
      if (camera == null) return;
      setState(() {
        _controller = CameraController(
          camera,
          ResolutionPreset.high,
        );
        _controllerInitializer = _controller.initialize();
      });
    });
  }

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

  Widget zoomButton(String label, VoidCallback callback) {
    return Padding(
      padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
      child: Container(
        height: 40,
        width: 40,
        decoration: BoxDecoration(
          color: Colors.grey.withOpacity(0.2),
          borderRadius: BorderRadius.all(
            Radius.circular(25),
          ),
        ),
        child: GestureDetector(
          child: Center(
            child: Text(
              label,
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 18,
                color: Colors.white,
              ),
            ),
          ),
          onTap: callback,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned.fill(
          child: FutureBuilder(
            future: _controllerInitializer,
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                return CameraPreview(_controller);
              } else {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }
            },
          ),
        ),
        Scaffold(
          backgroundColor: Colors.transparent,
          body: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              CustomAppbar(
                'Camera',
                'Take a photo',
                true,
                () {
                  setState(() {
                    _toggleFlash = !_toggleFlash;
                  });
                  _toggleFlash
                      ? _controller.setFlashMode(FlashMode.torch)
                      : _controller.setFlashMode(FlashMode.off);
                },
                flashValue: _toggleFlash,
              ),
              PainterFrame(),
              CustomBottombar(
                Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        zoomButton('1x', () => _controller.setZoomLevel(1.0)),
                        zoomButton('2x', () => _controller.setZoomLevel(2.0)),
                        zoomButton('3x', () => _controller.setZoomLevel(3.0)),
                      ],
                    ),
                    CameraMainActionButton(
                      true,
                      onCapturePressed,
                    ) //onCapturePressed
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Future _showErrorModal(String title, String info) {
    return showDialog(
      context: context,
      builder: (context) {
        return ErrorModal(title, info);
      },
    );
  }
}

The part that bothers me the most is that when this error occurs the camera fails to regain focus unless re-starting the camera component.

Someone would know how to get around this problem?

Thanks in advance.

  • 1

    You can try disabling autofocus to see if it helps you with something... cameraController.setFocusMode(FocusMode.locked); cameraController.setFocusPoint(null);

  • @Matheus Ribeiro Thanks for the comment but no help

  • @Matheus Ribeiro Another thing, I didn’t understand how this setFocusPoint() works, I passed some offset values but nothing seems to change.

  • 1

    I did not test the use of this method, but you can try to take a look at the example available in the package, which makes use of this method, to better understand and see if something else is missing.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.