简体   繁体   中英

Camera black screen in android zbar

I have been working in one of my android project in which I need to integrate Zbar scanner. I have integrated Zbar scanner in project. The problem I am facing is sometimes in Samsung Tab 3 has scanner area shows black screen. It is working perfectly in other devices. I have checked in nexus,canvas like devices and it shows perfect scanning screen. Is there any problem in my code? Here is my code.

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();
            }
        }
    };

Please help me out to solve the problem.

releaseCamera on pause and on destroyed. it happen when camera is not release by any resource and you are again starting it.hope this will help you.

It's late, but this finally helped me, after overlooking the obvious.

Make sure you have camera permission for your app. It must be enabled not only through the camera permission declaration in the manifest file, BUT you also have to make sure you grant your app access to your camera by Android OS. Go to Preferences -> Apps -> Your App -> Permissions -> Allow camera access.

Starting from Android 6.0 (API 23), users are not asked for permissions at the time of installation rather developers need to request the permissions at the run time. Only the permissions that are defined in the manifest file can be requested at run time. Request camera permission like here !.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM