ZXing Autofocus issue

Dude picture Dude · Jan 3, 2012 · Viewed 11.1k times · Source

I write an application for Motorola Xoom tablet with Android 3.1 for my master thesis that can scan multiple QR Codes in real time with it's camera and that displays additional information in the display over recognised QR Codes.

The recognition is done with the ZXing android app (http://code.google.com/p/zxing/), I basically just changed the code of the ZXing app so that it can recognise multiple QR Codes at the same time and can do this scan continually, without freezing after a successful scan like the original app does. So my app is basically the ZXing app with continous scanning of multiple QR Codes.

But I'm facing a problem:

  1. The ZXing app makes some sort of continous autofocus. It starts the autofocus and when it is finished it automatically starts the autofocus again. But this method somehow makes the camera brightness settings too bright, so that the camera cannot recognize the QR Codes because the image is almost totally white. Disabling the autofocus solves the problem, but I need autofocus because otherwise the recognition of the QR Codes is only possible at a certain distance.

    • How can I change the brightness settings of the camera?
    • Does somebody know another possibility for autofocus?

Answer

Pavel Bobkov picture Pavel Bobkov · Sep 21, 2016

I had the same issue on Samsung Galaxy Grand 2 (Android 4) and I found one solution. I disable autofocus function before I start camera. 1-2 seconds later I enable it. I tried a few approaches and decided to switch autofocus function periodically. To implement this I created util class - FocusHandler.

public class FocusHandler implements Runnable{

    private final int FOCUS_OFF_TIME = 2000;
    private final int FOCUS_ON_TIME = 20000;
    private boolean flag = false;
    private boolean state = false;
    private Handler handler;
    private WeakReference<ZXingScannerView> scannerView;

    public FocusHandler(Handler handler, ZXingScannerView scannerView){
        this.handler = handler;
        this.flag = false;
        this.scannerView = new WeakReference<>(scannerView);
    }

    public void start(){
        state = true;
        this.handler.post(this);
    }

    public void stop(){
        state = false;
        scannerView.clear();
    }

    @Override
    public void run() {
        if (!state || this.scannerView.get() == null){
            return;
        }

        int time;
        if (!flag){
            this.scannerView.get().setAutoFocus(flag);
            time = FOCUS_OFF_TIME;
        }
        else{
            this.scannerView.get().setAutoFocus(flag);
            time = FOCUS_ON_TIME;
        }

        flag = !flag;
        handler.postDelayed(this, time);
    }
}

/*************** activity ******************/

    private ZXingScannerView scannerView;
    private FocusHandler focusHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanner);
        scannerView = new ZXingScannerView(this);
        focusHandler = new FocusHandler(new Handler(), scannerView);
        frameLayout.addView(scannerView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        scannerView.setResultHandler(this);
        scannerView.setAutoFocus(false);
        scannerView.startCamera();
        focusHandler.start();
    }

    @Override
    public void onPause() {
        super.onPause();
        scannerView.stopCamera();
        focusHandler.stop();
    }