Android zbar中的相机黑屏

时间:2015-09-28 13:40:43

标签: android barcode-scanner zbar

我一直在我的一个Android项目中工作,我需要集成Zbar扫描程序。我在项目中集成了Zbar扫描仪。我面临的问题有时在三星Tab 3有扫描仪区域显示黑屏。它在其他设备上完美运行。我已经检查了nexus,画布等设备,它显示了完美的扫描屏幕。我的代码有问题吗?这是我的代码。

private void initControls() {
        try {

            {
                System.loadLibrary("iconv");
            }

            surfaceViewFlash = (SurfaceView) mView.findViewById(R.id.PREVIEW);
            surfaceViewFlash.setVisibility(View.INVISIBLE);
            surfaceViewFlash.setVisibility(View.VISIBLE);
            mCameraScanner = getCameraInstance();
            getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            previewingScanner = true;
            autoFocusHandlerScanner = new Handler();

            try {
                // Instance barcode zBarScanner
                zBarScanner = new ImageScanner();

                zBarScanner.setConfig(0, Config.X_DENSITY, 3);
                zBarScanner.setConfig(0, Config.Y_DENSITY, 3);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

            mPreviewScanner = new CameraPreview(getActivity(), mCameraScanner, previewCb, autoFocusCB);
            FrameLayout preview = (FrameLayout) mView.findViewById(R.id.cameraPreview);
            preview.addView(mPreviewScanner);

            if (barcodeScanned) {
                barcodeScanned = false;
                mCameraScanner.setPreviewCallback(previewCb);
                mCameraScanner.startPreview();
                previewingScanner = true;
                Log.e("initControls", "initControls");
            }

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

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


 /**
     * A safe way to get an instance of the Camera object.
     */
    public Camera getCameraInstance() {
        Camera c = null;
        int frontId = 0, backId = 0;
        try {


            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            int numberOfCameras = Camera.getNumberOfCameras();
            for (int i = 0; i < numberOfCameras; i++) {
                Camera.getCameraInfo(i, cameraInfo);
                if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                    frontId = i;
                } else if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                    backId = i;
                }

            }
            c = Camera.open(backId);
        } catch (Exception e) {
            e.printStackTrace();
            GeneralAlertDialog.createDialog(getActivity(), getString(R.string.app_name), "Camera is not working, Please try again.", new DialogDismiss() {
                @Override
                public void onDismiss() {
                    getCallBackForCloseScanner().OnCloseButtonClickOfScanner();
                }
            });

        }
        return c;
    }

 private void releaseCamera() {
        if (mCameraScanner != null) {
            mCameraScanner.cancelAutoFocus();
            previewingScanner = false;
            mCameraScanner.setPreviewCallback(null);
            mPreviewScanner.getHolder().removeCallback(mPreviewScanner);
            mCameraScanner.stopPreview();
            mCameraScanner.release();
            mCameraScanner = null;
            mPreviewScanner = null;
        }
    }

 Runnable doAutoFocus = new Runnable() {
        public void run() {

            try {
                if (previewingScanner) {
                    if (mCameraScanner == null)
                        mCameraScanner = getCameraInstance();
                    Camera.Parameters parameters = null;
                    Log.e("mCameraScanner", mCameraScanner + "");

                    if (null != mCameraScanner.getParameters()) {
                        parameters = mCameraScanner.getParameters();
                    }
                    List<String> focusModes = parameters.getSupportedFocusModes();
                    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
                        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                        mCameraScanner.setParameters(parameters);
                        mCameraScanner.autoFocus(autoFocusCB);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

请帮我解决问题。

1 个答案:

答案 0 :(得分:0)

在暂停和摧毁时释放相机。当相机没有被任何资源释放而你再次启动它时就会发生这种情况。这会对你有帮助。