Cordova:如何杀死SurfaceView和CameraSource创建的相机

时间:2018-06-12 14:36:11

标签: cordova surfaceview camera-view

我正在使用SurfaceView和CameraView打开相机

surfaceView = new SurfaceView(context);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT,
                Gravity.CENTER);

        cordova.getActivity().addContentView(surfaceView, params);
cameraSource = new CameraSource.Builder(context, textRecognizer)
                    .setFacing(CameraSource.CAMERA_FACING_BACK)
                    .setRequestedPreviewSize(1280, 1024)
                    .setRequestedFps(2.0f)
                    .setAutoFocusEnabled(true)
                    .build();

            surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder surfaceHolder) {

                    try {
                        cameraSource.start(surfaceView.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

                }

                @Override
                public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                    cameraSource.stop();
                }
            });

此后我试图关闭相机,但我无法关闭相机。有谁知道怎么做?

我尝试过的(哪些无效)

1. cordova.getActivity().finish();
2. cameraSource.stop();
3. cordova.getActivity().addContentView(null, null);
4. ((ViewGroup) surfaceView.getParent()).removeView(surfaceView);

0 个答案:

没有答案