【问题标题】:Get bitmap without transformations from TextureView从 TextureView 获取没有转换的位图
【发布时间】:2018-03-17 09:37:56
【问题描述】:

我目前正在开发一个使用 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 中看到的(这是合乎逻辑的),但我想要整个图像。

有可能吗?

谢谢, 氯化镍

【问题讨论】:

    标签: android camera2 android-textureview


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

          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 API2NDK (OpenCV) 的完整实现https://stackoverflow.com/a/49331546/471690

      【讨论】:

      • getBitmap 返回相机在某些设备上看到的完整图像,而在其他设备上,它返回您在 TextureView 中看到的翻译图像......现在有这个问题,所以这不是一个解决方案只需从纹理视图中获取位图
      猜你喜欢
      • 2016-07-15
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      • 2012-04-03
      • 2020-09-02
      相关资源
      最近更新 更多