Android二维码扫描器在Fragment中无法打开相机。

huangapple go评论65阅读模式
英文:

Android QR Code Scanner camera not opening in Fragment

问题

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 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&lt;Barcode&gt;() {
                @Override
                public void release() {
    
                }
    
                @Override
                public void receiveDetections(Detector.Detections&lt;Barcode&gt; detections) {
                    final SparseArray&lt;Barcode&gt; 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?

答案1

得分: 1

从Android 6.0(API 23)开始,您需要向用户请求运行时权限。这就是为什么您的相机没有显示任何内容。权限仅在AndroidManifest中定义,但用户没有同意允许您的应用使用相机。您可以在这里找到一个很好的请求运行时权限的示例。

如果您想要阅读更多相关信息,Android开发者文档中也有相关内容:

英文:

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:

huangapple
  • 本文由 发表于 2020年8月15日 17:16:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63424442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定