Android谷歌移动视觉api打开相机和火炬

时间:2016-10-03 20:23:57

标签: android camera torch

我正在使用Android谷歌条码视觉API,我可以完美地检测我的QR码。我怎样才能同时打开火炬。它将需要在晚上扫描二维码。这是我到目前为止的工作代码

public class fragment_qrscan extends myFragment {

    SurfaceView cameraPreview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_qrscan, container, false);

        cameraPreview = (SurfaceView)view.findViewById(R.id.camera_preview);

        createCameraSource();

        return view;
    }


    private void createCameraSource() {

        BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(getActivity()).build();
        final CameraSource cameraSource = new CameraSource.Builder(getActivity(),barcodeDetector)
                .setAutoFocusEnabled(true)
                .setRequestedPreviewSize(1600,1024)
                .build();

        cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 1);
                    }

                    cameraSource.start(cameraPreview.getHolder());

                } catch (Exception 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> barcodes = detections.getDetectedItems();
                if (barcodes.size() >0) {
                    Log.i("myStuff",String.valueOf(barcodes.valueAt(0).displayValue));
                }
            }
        });

    }
}

一切都很完美我只是无法弄清楚如何正确打开手电筒并仍然使用移动视觉API。任何帮助或建议将不胜感激。

1 个答案:

答案 0 :(得分:4)

这是我完全可行的解决方案。通过在我的项目中包含CameraSource.java类found here。我能够扫描二维码,我可以打开和关闭相机手电筒。

public class fragment_qrscan extends myFragment {

    SurfaceView cameraPreview;
    private CameraSource mCameraSource;
    private Button btnLight;
    boolean lightOn = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_qrscan, container, false);

        cameraPreview = (SurfaceView) view.findViewById(R.id.camera_preview);
        btnLight = (Button) view.findViewById(R.id.btnLight);

        btnLight.setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View v) {
                        btnLightClicked();
                    }
                }
        );

        createCameraSource(true, false);

        return view;
    }

    private void btnLightClicked() {

       if (!lightOn) {
            mCameraSource.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
           lightOn = true;

       }
        else {
           mCameraSource.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
           lightOn = false;
       }
    }

    @SuppressLint("InlinedApi")
    private void createCameraSource(boolean autoFocus, boolean useFlash) {
        Context context = getActivity().getApplicationContext();
        BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
        CameraSource.Builder builder = new CameraSource.Builder(getActivity().getApplicationContext(), barcodeDetector)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setRequestedPreviewSize(1600, 1024)
                .setRequestedFps(15.0f);
        // make sure that auto focus is an available option
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            builder = builder.setFocusMode(
                    autoFocus ? Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE : null);
        }

        mCameraSource = builder
                .setFlashMode(useFlash ? Camera.Parameters.FLASH_MODE_TORCH : null)
                .build();

    cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 1);
                }

                mCameraSource.start(cameraPreview.getHolder());

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

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {

            mCameraSource.stop();
        }
    });

    barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
        @Override
        public void release() {

        }

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {

            final SparseArray<Barcode> barcodes = detections.getDetectedItems();
            if (barcodes.size() >0) {
                Log.i("myStuff",String.valueOf(barcodes.valueAt(0).displayValue));
            }
        }
    });

    }
}

我确信有一个更清晰的解决方案但是现在这个代码完美运行并且自动焦点设置为true非常酷,扫描qr代码的速度和速度都非常快。即使距离3英尺远,也能立即检测到代码。