Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BarcodeDetector.Builder.setAutoFocusEnabled(true) not working on Nexus 4 #48

Open
malloth opened this issue Nov 27, 2015 · 8 comments
Open

Comments

@malloth
Copy link

malloth commented Nov 27, 2015

In Google Play Services 8.3 CameraSource class you use Camera.setFocusMode("continuous-video").

This mode is invalid for most of the devices on the market (including Nexus 4). It doesn't work in preview mode - ONLY when taking video.

Use Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE or give us an option to set focus mode just like in: https://github.com/googlesamples/android-vision/blob/master/visionSamples/barcode-reader/app/src/main/java/com/google/android/gms/samples/vision/barcodereader/ui/camera/CameraSource.java

@gregkorossy
Copy link
Contributor

Use the setFocusMode(String mode) method instead.

@malloth
Copy link
Author

malloth commented Nov 29, 2015

There's no setFocusMode(String mode) method in official CameraSource.Builder according to this link: https://developers.google.com/android/reference/com/google/android/gms/vision/CameraSource.Builder

@pm0733464
Copy link
Contributor

Yes. The official API provides setAutoFocusEnabled instead, which sets the focus mode to FOCUS_MODE_CONTINUOUS_VIDEO.

@malloth
Copy link
Author

malloth commented Dec 1, 2015

And that's the problem. Should be FOCUS_MODE_CONTINUOUS_PICTURE instead or new API version should have methods like in visionSamples.

@vpdsouza
Copy link

I am seeing the same freaking problem, if I use the VisionSamples which is outdated compare to the latest API version, it works just fine but per the new API it doesn't work. Someone at google please talk to us or update your documentation on how to make the Focus and Flash work with new version of this Barcode API, please.... ???

@irodrigo17
Copy link

I'm seeing this same error, did anybody find a solution or a workaround for this?

@Ankurjain30
Copy link

Anybody got the solution for this, pls share.

@koroshiya
Copy link

I've been having this issue with a number of phones. Some focus just fine, others don't focus at all. I was able to work around this by manipulating the properties of the underlying camera manually after vision has started.

private fun getCamera(cameraSource: CameraSource): Camera? {
        CameraSource::class.java.declaredFields
                .firstOrNull { it.type === Camera::class.java }
                ?.let {
                    it.isAccessible = true
                    try {
                        return it.get(cameraSource) as? Camera?
                    } catch (e: IllegalAccessException) {
                        e.printStackTrace()
                    }
                }

    return null
}

fun setFocus(){
    val delay = TimeUnit.SECONDS.toMillis(1L)
    Handler().postDelayed(delay, null){ //from androidx lib
        try {
            val c2 = getCamera(camera) ?: throw IOException("Camera is null")
            val params = c2.parameters
            params.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE
            c2.parameters = params
        }catch(e: Exception){
            e.printStackTrace()
        }
    }
}

setFocus is called after everything else has been loaded, and the camera has started looking for barcodes.

The postDelayed is because some devices don't seem to set the focus mode if it's too soon after initialization.

Anyway, the above isn't a great solution, but it makes vision usable again on my older test devices. Vision works fine as-is on a Pixel, but not so great on a 5 year old Samsung.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants