Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZXing Autofocus issue

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?
like image 852
Dude Avatar asked Feb 22 '23 23:02

Dude


2 Answers

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();
    }
like image 62
Pavel Bobkov Avatar answered Mar 04 '23 06:03

Pavel Bobkov


I already answered this on the mailing list.

The app can't change the 'quality' of the camera preview: do you mean resolution? resolution is not the limiting factor. Maybe your app is selecting a particularly low resolution; at some point it doesn't help. But scanning 800x600 should be more than enough for QR codes.

Auto-focus is unrelated to brightness. The camera driver itself is always automatically adjusting exposure. The app does not control this, and can't; the best you can do is modify the exposure setting in later versions of the Android API.

like image 27
Sean Owen Avatar answered Mar 04 '23 05:03

Sean Owen