Android:OpenCV关闭相机预览

时间:2015-07-07 19:15:44

标签: android opencv opengl-es

我正在为爱普生BT 200眼镜写一个增强现实演示,我有一些我已经启动并运行的示例,如果你将它指向某个图片,它会检测到并在它上面渲染一个立方体。

现在很明显这对于手机来说很好,但是通过眼镜我希望它可以工作但不显示相机输入。我认为关闭视图会阻止它,或者将可见性设置为消失。但仍然无效。

到目前为止,我发现的唯一工作是不将OpenGL渲染设置为透明像素

// The OpenCV loader callback.
    private BaseLoaderCallback mLoaderCallback =
            new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(final int status) {
            switch (status) {
            case LoaderCallbackInterface.SUCCESS:
                Log.d(TAG, "OpenCV loaded successfully");
                mCameraView.enableView();
                mCameraView.enableFpsMeter();
                mBgr = new Mat();

                final ARFilter starryNight;
                try {
                    // Define The Starry Night to be 1.0 units tall.
                    starryNight = new ImageDetectionFilter(
                            CameraActivity.this,
                            R.drawable.the_lab,
                            mCameraProjectionAdapter, 1.0);
                } catch (IOException e) {
                    Log.e(TAG, "Failed to load drawable: " +
                            "starry_night");
                    e.printStackTrace();
                    break;
                }

                final ARFilter akbarHunting;
                try {
                    // Define Akbar Hunting with Cheetahs to be 1.0
                    // units wide.
                    akbarHunting = new ImageDetectionFilter(
                            CameraActivity.this,
                            R.drawable.akbar_hunting_with_cheetahs,
                            mCameraProjectionAdapter, 1.0);
                } catch (IOException e) {
                    Log.e(TAG, "Failed to load drawable: " +
                            "akbar_hunting_with_cheetahs");
                    e.printStackTrace();
                    break;
                }

                mImageDetectionFilters = new ARFilter[] {
                        //new NoneARFilter(),
                        starryNight,
                        akbarHunting
                };

                mARRenderer.filter = mImageDetectionFilters[
                        mImageDetectionFilterIndex];

                break;
            default:
                super.onManagerConnected(status);
                break;
            }
        }
    };

     // Suppress backward incompatibility errors because we provide
        // backward-compatible fallbacks.
        @SuppressLint("NewApi")
        @Override
        protected void onCreate(final Bundle savedInstanceState) {

            goFullScreen();

            super.onCreate(savedInstanceState);

            if (savedInstanceState != null) {
                mCameraIndex = savedInstanceState.getInt(
                        STATE_CAMERA_INDEX, 0);
                mImageSizeIndex = savedInstanceState.getInt(
                        STATE_IMAGE_SIZE_INDEX, 0);
                mImageDetectionFilterIndex = savedInstanceState.getInt(
                        STATE_IMAGE_DETECTION_FILTER_INDEX, 0);

            } else {
                mCameraIndex = 0;
                mImageSizeIndex = cameraSize; // was 0
                mImageDetectionFilterIndex = 0;
            }

            final FrameLayout layout = new FrameLayout(this);
            layout.setLayoutParams(new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT));
            layout.setBackgroundColor(Color.RED);
            setContentView(layout);

            mCameraView = new JavaCameraView(this, mCameraIndex);
            mCameraView.setLayoutParams(new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT));
            layout.addView(mCameraView);
            //mCameraView.setVisibility(View.INVISIBLE);

            GLSurfaceView glSurfaceView = new GLSurfaceView(this);
            glSurfaceView.getHolder().setFormat(
                    PixelFormat.TRANSPARENT);
            glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 0, 0);
            glSurfaceView.setZOrderOnTop(true);
            glSurfaceView.setLayoutParams(new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT));
            layout.addView(glSurfaceView);

            mCameraProjectionAdapter = new CameraProjectionAdapter();

            mARRenderer = new ARCubeRenderer();
            mARRenderer.cameraProjectionAdapter =
                    mCameraProjectionAdapter;
            // Earlier, we defined the printed image's size as 1.0
            // unit.
            // Define the cube to be half this size.
            mARRenderer.scale = 0.5f;
            glSurfaceView.setRenderer(mARRenderer);

            final Camera camera;
            if (Build.VERSION.SDK_INT >=
                    Build.VERSION_CODES.GINGERBREAD) {
                CameraInfo cameraInfo = new CameraInfo();
                Camera.getCameraInfo(mCameraIndex, cameraInfo);
                mIsCameraFrontFacing = 
                        (cameraInfo.facing ==
                        CameraInfo.CAMERA_FACING_FRONT);
                mNumCameras = Camera.getNumberOfCameras();
                camera = Camera.open(mCameraIndex);
            } else { // pre-Gingerbread
                // Assume there is only 1 camera and it is rear-facing.
                mIsCameraFrontFacing = false;
                mNumCameras = 1;
                camera = Camera.open();
            }
            final Parameters parameters = camera.getParameters();
            camera.release();
            mSupportedImageSizes =
                    parameters.getSupportedPreviewSizes();
            final Size size = mSupportedImageSizes.get(mImageSizeIndex);


            mCameraProjectionAdapter.setCameraParameters(
                    parameters, size);
            // Earlier, we defined the printed image's size as 1.0
            // unit.
            // Leave the near and far clip distances at their default
            // values, which are 0.1 (one-tenth the image size) and
            // 10.0 (ten times the image size).

            mCameraView.setMaxFrameSize(size.width, size.height);
            mCameraView.setCvCameraViewListener(this);
        }

0 个答案:

没有答案