robyiset
robyiset

Reputation: 27

Android QR Code Scanner camera not opening in Fragment

I try to create QR Code scanner in fragment, but camera won't showing in surfaceview and just turn black.

here's my java class:

    public class ScanFragment extends Fragment {
        SurfaceView surfaceView;
        CameraSource cameraSource;
        TextView textView;
        BarcodeDetector barcodeDetector;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            final View view =  inflater.inflate(R.layout.fragment_scan, container, false);
    
            surfaceView = (SurfaceView) view.findViewById(R.id.cameraPreview);
            textView = (TextView) view.findViewById(R.id.scanText);
    
            barcodeDetector = new BarcodeDetector.Builder(view.getContext().getApplicationContext())
                    .setBarcodeFormats(Barcode.QR_CODE).build();
    
            cameraSource = new CameraSource.Builder(view.getContext().getApplicationContext(), barcodeDetector)
                    .setRequestedPreviewSize(640, 480).build();
    
            surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    if (ActivityCompat.checkSelfPermission(getContext().getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    try {
                        cameraSource.start(holder);
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
    
                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    
                }
    
                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                    cameraSource.stop();
                }
            });
    
            barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
                @Override
                public void release() {
    
                }
    
                @Override
                public void receiveDetections(Detector.Detections<Barcode> detections) {
                    final SparseArray<Barcode> qrCodes = detections.getDetectedItems();
    
                    if(qrCodes.size() != 0){
                        textView.post(new Runnable() {
                            @Override
                            public void run() {
                                Vibrator vibrator = (Vibrator) getContext().getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                                vibrator.vibrate(1000);
                                textView.setText(qrCodes.valueAt(0).displayValue);
                            }
                        });
                    }
                }
            });
            return view;
        }
    }

I gave the uses permissions from the android manifest file. compiles seamlessly in android studio but when I run it on the phone the camera just turn black and no crash from that.

Anyone know how to fix this?

Upvotes: 0

Views: 767

Answers (1)

BVantur
BVantur

Reputation: 1232

From Android 6.0(API 23) on, you need to request runtime permission from the users. That is why your camera doesn't show anything. Permission is only defined in AndroidManifest, but the user did not agree to allow your application to use a camera. You have a good example of how to request runtime permissions here.

If you want to read more about this, there is also documentation available on Android developer:

Upvotes: 1

Related Questions