contactlutforrahman / flutter_qr_bar_scanner

A full screen mobile scanner for scanning QR Code and Bar Code.
MIT License
33 stars 70 forks source link

Android auto focus does not work #40

Open jswolf19 opened 2 years ago

jswolf19 commented 2 years ago

I'm using an Android8.1 Kyocera Torque G03 (Japan) phone, and the focal length does not seem to update after starting the barcode scan preview.

The QRCameraC2 camera class is what gets used, with afMode being set to 3 (CONTROL_AF_MODE_CONTINUOUS_VIDEO).

I believe the issue is with the CONTROL_AF_TRIGGER settings done in startCamera.

if (afMode == CONTROL_AF_MODE_AUTO) {
    previewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START);
} else {
    previewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_CANCEL);
}

According to the Android docs

Generally, applications should set this entry to START or CANCEL for only a single capture, and then return it to IDLE (or not set at all). Specifying START for multiple captures in a row means restarting the AF operation over and over again.

While the wording here leaves a bit up to interpretation, I believe the two options being suggested are:

  1. set this entry (CONTROL_AF_TRIGGER) to START or CANCEL for only a single capture, and then return it to IDLE
  2. do not set (CONTROL_AF_TRIGGER) at all

I have removed the above lines setting CaptureRequest.CONTROL_AF_TRIGGER on previewBuilder, and the problem seems to be resolved (option 2, not setting CONTROL_AF_TRIGGER at all). If you'd like a pull request, I'll be happy to create one.

Looking at the Camera plugin, they seem to go with option 1.

jswolf19 commented 2 years ago

Auto-focus seems to work on other Android phones, so I'm not sure if it's an Android OS version issue, a hardware issue, or a driver issue.

The patched version (that does not set CONTROL_AF_TRIGGER) work at least as well as the unpatched version on all devices I've tested.

Phone Android Camera2 Works Unpatched Works Patched
Kyocera C6742A 5.1.1 yes (legacy HAL) yes yes
Fujitsu Arrow M03 6.0.1 yes (legacy HAL) yes1 yes1
Kyocera Torque G03 8.1 yes (legacy HAL) no yes
Kyocera AndroidOne S2 9 yes (legacy HAL) yes yes
Sharp Aquos sense3 basic 11 yes yes yes
Google pixel4a 12 probably yes yes
  1. substantial movement is required to trigger refocus