有没有一种方法可以包含用于放大SurfaceView的滑块?

时间:2019-07-19 12:15:58

标签: android

我正在尝试读取条形码和QR码。但是,我似乎无法找到一种使用相机变焦功能的方法。

我看过http://www.devexchanges.info/2016/10/reading-barcodeqr-code-using-mobile.html上可用的代码。但这并没有提供任何线索说明我如何使用滑块或双指缩放来实现缩放功能。

以下是我拥有的代码的摘录:

private void initialiseDetectorsAndSources() {

    Toast.makeText(getApplicationContext(), "Barcode scanner initiated", Toast.LENGTH_SHORT).show();

    barcodeDetector = new BarcodeDetector.Builder(this)
            .setBarcodeFormats(Barcode.ALL_FORMATS)
            .build();

    if(!barcodeDetector.isOperational()){
        Toast.makeText(getApplicationContext(), "Failed to start barcode scanner", Toast.LENGTH_SHORT).show();
        return;
    }else{
        Toast.makeText(getApplicationContext(), "Barcode scanner prepped.", Toast.LENGTH_SHORT).show();
    }

    cameraSource = new CameraSource.Builder(this, barcodeDetector)
            .setFacing(CameraSource.CAMERA_FACING_BACK)
            .setRequestedPreviewSize(1920, 1080)
            .setRequestedFps(15.0f)
            .setAutoFocusEnabled(true)
            .build();



    surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {



        @Override
        public void surfaceCreated(SurfaceHolder holder) {

            try{
                if(checkSelfPermission(Manifest.permission.CAMERA)==PackageManager.PERMISSION_GRANTED){
                    cameraSource.start(surfaceView.getHolder());

                }else{
                    Toast.makeText(getApplicationContext(), "Allow camera permission for this app", Toast.LENGTH_SHORT).show();
                }

            }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() {
            intentData=null;
        }

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> barcodes = detections.getDetectedItems();

            if (barcodes.size() != 0) {

                txtBarcodeValue.post(new Runnable() {

                    @Override
                    public void run() {

                        isEmail = true;
                        isUpload = true;
                        txtBarcodeValue.removeCallbacks(null);
                        intentData = "Code detected: "+barcodes.valueAt(0).displayValue;
                        Toast.makeText(getApplicationContext(), intentData, Toast.LENGTH_SHORT).show();
                        btnAction.setText("Ready for sharing");
                        btnUpload.setText("Press this to Upload");
                    }
                });

            }
        }
    });
}

我也看过Pinch zoom on SurfaceView。但这似乎不适合我。我也无法改编可用的https://stackoverflow.com/a/47709735/10878989代码。

0 个答案:

没有答案