Android camera focusing only once
Asked Answered
A

1

6

We are developing an app for the Vuzix M100, which should continuously read a barcode and display the result in a textView. Therefore, the camera takes a picture every 5 seconds and sends the bitmap to the zxing barcode scanner. We are almost done, but the camera is only focusing at the first picture. Any suggestions? This is the important part of our code:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.textView1);

        mPreview = (SurfaceView) findViewById(R.id.sv1);
        mPreview.getHolder().addCallback(this);

        mCamera = Camera.open();
        final Parameters param = mCamera.getParameters();

        param.setJpegQuality(100);
        param.setPictureSize(1600, 1200);
        param.setFocusMode(param.FOCUS_MODE_CONTINUOUS_PICTURE);
        param.setSceneMode(Parameters.SCENE_MODE_BARCODE);

        mCamera.setParameters(param);

        final Handler h = new Handler();
        final int delay = 5000; 

        h.postDelayed(new Runnable(){
            int count = 1;
            public void run(){
                tryAutoFocus();
                param.setFocusMode(param.FOCUS_MODE_FIXED);
                param.setFocusMode(param.FOCUS_MODE_AUTO);

                h.postDelayed(this, delay);
            }
        }, delay);

    }

.

private void tryAutoFocus() {

        final PictureCallback myPictureCallback = new PictureCallback() {

            @Override
            public void onPictureTaken(byte[] data, Camera camera) {

                try {
                    Options options = new BitmapFactory.Options();
                    options.inScaled = false;
                    Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options);
                    createExternalStoragePublicPicture(bmp);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        };

        AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback() {

            @Override
            public void onAutoFocus(boolean success, Camera arg1) {
                // TODO: Problem: Fokussiert nur beim ersten Foto           

                mCamera.takePicture(null, null, null, myPictureCallback);

                mCamera.cancelAutoFocus();
                mCamera.startPreview();
            }
        };

        mCamera.autoFocus(myAutoFocusCallback);

    }
Afterward answered 21/7, 2016 at 9:27 Comment(0)
B
0

This is an old question, but still:

Note that setting a scene via Camera.Parameters.setSceneMode() may actually override any previous setting. Quoting the API documentation:

Changing scene mode may override other parameters (such as flash mode, focus mode, white balance). For example, suppose originally flash mode is on and supported flash modes are on/off. In night scene mode, both flash mode and supported flash mode may be changed to off. After setting scene mode, applications should call getParameters to know if some parameters are changed.

Also the docs imply that not only the current settings may be overriden, but also the supported parameter values.

So I suggest to:

  • Set the scene mode before setting the focus mode
  • After setting the scene mode, check if the desired focus mode is still supported with getSupportedFocusModes()
Bandaranaike answered 16/12, 2021 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.