Android glTexSubImage2D与GL_TEXTURE_EXTERNAL_OES问题(OpenGL + OpenCV + CameraX)
创始人
2024-08-13 23:00:10
0

在Android中使用OpenGL显示相机预览图像时,可以使用glTexSubImage2D函数将图像数据传递给纹理。然而,当相机使用CameraX库并且配置为使用TextureView作为预览视图时,需要使用GL_TEXTURE_EXTERNAL_OES纹理目标。

以下是一个示例代码,演示了如何在Android中使用OpenGL、OpenCV和CameraX来解决这个问题:

  1. 在项目的build.gradle文件中添加以下依赖项:
implementation 'androidx.camera:camera-core:1.0.0'
implementation 'androidx.camera:camera-camera2:1.0.0'
implementation 'org.opencv:opencv-android:4.5.3'
implementation 'org.opencv:opencv-core:4.5.3'
implementation 'org.opencv:opencv-java:4.5.3'
  1. AndroidManifest.xml文件中添加相机和权限的权限声明:


  1. activity_main.xml中添加一个TextureView作为相机预览视图:

  1. MainActivity.java中实现相机预览和OpenGL渲染的逻辑:
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageProxy;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.core.content.ContextCompat;

import android.graphics.SurfaceTexture;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Size;
import android.view.TextureView;

import com.google.common.util.concurrent.ListenableFuture;
import org.opencv.android.OpenCVLoader;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MainActivity extends AppCompatActivity {
    private TextureView textureView;
    private GLSurfaceView glSurfaceView;
    private SurfaceTexture surfaceTexture;
    private int cameraTextureId = -1;

    static {
        if (!OpenCVLoader.initDebug()) {
            // Handle initialization error
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textureView = findViewById(R.id.textureView);
        glSurfaceView = findViewById(R.id.glSurfaceView);
        glSurfaceView.setEGLContextClientVersion(2);
        glSurfaceView.setRenderer(new MyRenderer());
    }

    @Override
    protected void onResume() {
        super.onResume();

        ListenableFuture cameraProviderFuture = ProcessCameraProvider.getInstance(this);
        cameraProviderFuture.addListener(() -> {
            try {
                ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
                bindCameraPreview(cameraProvider);
            } catch (Exception e) {
                // Handle error
            }
        }, ContextCompat.getMainExecutor(this));
    }

    private void bindCameraPreview(ProcessCameraProvider cameraProvider) {
        int width = textureView.getWidth();
        int height = textureView.getHeight();

        surfaceTexture = new SurfaceTexture(createOESTexture());
        surfaceTexture.setDefaultBufferSize(width, height);

        CameraSelector cameraSelector = new CameraSelector.Builder()
                .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                .build();

        ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
                .setTargetResolution(new Size(width, height))
                .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                .build();

        imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(this), new ImageAnalysis.Analyzer() {
            @Override
            public void analyze(ImageProxy image) {
                // Process the image using OpenCV
                // ...

                // Update the OpenGL texture with the processed image
                updateTexture(image);

                image.close();
            }
        });

        cameraProvider.unbindAll();
        cameraProvider.bindToLifecycle(this, cameraSelector, imageAnalysis);
    }

    private void updateTexture(ImageProxy image) {
        image.getPlanes()[0].getBuffer().rewind();
        byte[] data = new byte[image.getPlanes()[0].getBuffer().remaining()];
        image.getPlanes()[0].getBuffer().get(data);

        surfaceTexture.attachToGLContext(cameraTextureId);
        surfaceTexture.updateTexImage();
        surfaceTexture.detachFrom

相关内容

热门资讯

详情透视!红龙poker辅助!... 详情透视!红龙poker辅助!竟然真的是有辅助脚本(了解有挂)-哔哩哔哩所有人都在同一条线上,像星星...
科普透视!xpoker辅助!其... 科普透视!xpoker辅助!其实是有辅助方法(有挂功能)-哔哩哔哩xpoker辅助能透视中分为三种模...
详细透视!newpoker怎么... 详细透视!newpoker怎么安装脚本!竟然真的是有辅助脚本(有挂教程)-哔哩哔哩;暗藏猫腻,小编详...
普及透视!智星德州可以透视吗!... 普及透视!智星德州可以透视吗!都是是有辅助神器(有挂细节)-哔哩哔哩1、智星德州可以透视吗破解器简单...
推荐透视!约局吧德州真的有透视... 推荐透视!约局吧德州真的有透视挂吗!一直是有辅助app(有挂方法)-哔哩哔哩1、不需要AI权限,帮助...
推荐透视!pokemmo免费脚... 推荐透视!pokemmo免费脚本!果然一直总是有辅助攻略(真是有挂)-哔哩哔哩1、超多福利:超高返利...
必备透视!pokemmo免费脚... 必备透视!pokemmo免费脚本!一贯真的有辅助攻略(有挂猫腻)-哔哩哔哩一、pokemmo免费脚本...
详细透视!pokemmo手机版... 详细透视!pokemmo手机版脚本免费!其实是有辅助插件(揭秘有挂)-哔哩哔哩1、这是跨平台的pok...
解迷透视!xpoker辅助怎么... 解迷透视!xpoker辅助怎么用!竟然存在有辅助插件(真的有挂)-哔哩哔哩1.xpoker辅助怎么用...
科普透视!约局吧开挂神器是真的... 科普透视!约局吧开挂神器是真的吗!原来是真的有辅助软件(有挂解惑)-哔哩哔哩1、任何约局吧开挂神器是...