Android相机只关注一次

时间:2016-07-21 09:27:06

标签: android camera focus zxing vuzix

我们正在为Vuzix M100开发一个应用程序,它应该连续读取条形码并在textView中显示结果。因此,相机每5秒拍摄一张照片并将位图发送到zxing条形码扫描仪。我们差不多完成了,但相机只关注第一张照片。有什么建议? 这是我们代码的重要部分:

@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);

    }

0 个答案:

没有答案