Zbar with Android : Scanner camera viewport remain inactive and black after showing the url in browser

只愿长相守 提交于 2019-12-03 15:35:52

At last I succeeded to find out the problem of my code and got the solution. When i try to reopen the camera in my onResume() method I missed a part. When I create/open a new camera in onResume(), the FrameLayout still has my previous camera. So All I do is remove my previous camera from FrameLayout on onPause() method and then recreate everything on onResume() method. That solve my problem and now it is working nicely without any error. Here is my onPause() and onResume() method with the fix. Hope this might help somebody in future.

    public void onPause() {
        super.onPause();
        releaseCamera();
        FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
        preview.removeView(mPreview);
    }

    public void onResume(){
        super.onResume();

        try {
            if(mCamera==null){

            //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            autoFocusHandler = new Handler();
            mCamera = getCameraInstance();
            this.getWindowManager().getDefaultDisplay().getRotation();

            scanner = new ImageScanner();
            scanner.setConfig(0, Config.X_DENSITY, 3);
            scanner.setConfig(0, Config.Y_DENSITY, 3);

            mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
            FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
            preview.addView(mPreview);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block

        }
    }

    /** A safe way to get an instance of the Camera object. */
    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open();
        } catch (Exception e){
        }
        return c;
    }

    private void releaseCamera() {
        //Toast.makeText(QRScannerActivity.this, "Paused State", Toast.LENGTH_SHORT).show();
        if (mCamera != null) {
            previewing = false;
            mCamera.setPreviewCallback(null);
            mPreview.getHolder().removeCallback(mPreview);
            mCamera.release();
            mCamera = null;
            mPreview= null;
        }

    }

Thanks, Sakib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!