在没有TextureView转换的情况下获取位图

时间:2016-02-26 16:04:17

标签: android camera2 android-textureview

我目前正在开发一个使用Camera2的应用程序。 我在TextureView上显示预览,该预览被缩放和翻译(我只需要显示图像的一部分)。我的问题是我需要分析整个图像。

我在CameraDevice.StateCallback中的内容:

@Override
    public void onOpened(CameraDevice camera) {
        mCameraDevice = camera;

        SurfaceTexture texture = mTextureView.getSurfaceTexture();
        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        Surface surface = new Surface(texture);

        try {
            mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        } catch (CameraAccessException e){
            e.printStackTrace();
        }

        try {
            mCameraDevice.createCaptureSession(Arrays.asList(surface), mPreviewStateCallback, null);
            mPreviewBuilder.addTarget(surfaceFull);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

并在我的SurfaceTextureListener中:

@Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                my_analyze(mTextureView.getBitmap());
            }
        });
        thread.start();
    }

位图只是我在TextureView中看到的(这是合乎逻辑的),但我想要整个图像。

有可能吗?

谢谢, NiCLO

2 个答案:

答案 0 :(得分:2)

您可以将帧发送到您创建的SurfaceTexture,而不是TextureView的一部分,然后通过将它们渲染到GLES pbuffer并使用glReadPixels()读取它们来获取像素。

如果您可以使用YUV而不是RGB,则可以通过将Camera2输出定向到ImageReader来更快,更简单地获取数据。

Grafika有一些有用的例子,例如“来自相机的纹理”。

答案 1 :(得分:-1)

对我来说,下一个实现对我来说很好:

    Bitmap bitmap = mTextureView.getBitmap(mWidth, mHeight);
    int[] argb = new int[mWidth * mHeight];
    // get ARGB pixels and then proccess it with 8UC4 OpenCV convertion
    bitmap.getPixels(argb, 0, mWidth, 0, 0, mWidth, mHeight);
    // native method (NDK or CMake)
    processFrame8UC4(argb, mWidth, mHeight);

Camera API2NDKOpenCV)的完整实施https://stackoverflow.com/a/49331546/471690