使用OpenGL ES 2.0渲染Camera Preview时出现问题

时间:2016-01-28 07:14:24

标签: java android opengl-es camera opengl-es-2.0

这是我第一次尝试使用OpenGL ES 2.0渲染相机预览。我对代码有些疑问。这是下面的代码 -

MainActivity.java

public class MainActivity extends AppCompatActivity implements SurfaceTexture.OnFrameAvailableListener {

private Camera mCamera;
private GLSurfaceView glSurfaceView;
Bitmap bitmap;
FrameLayout frameLayout;
DrawOnTop drawOnTop;
FrameLayout.LayoutParams layoutParams;
private SurfaceTexture surface;
MyGLRenderer renderer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    frameLayout = (FrameLayout)findViewById(R.id.camera_preview);
    drawOnTop = null;
}

public void onCamViewButtonClicked (View view)
{
    glSurfaceView = new MyGLSurfaceView(this);
    renderer = MyGLSurfaceView.getRenderer();
    frameLayout.addView(glSurfaceView);
}

public void startCamera(int texture)
{
    surface = new SurfaceTexture(texture);
    surface.setOnFrameAvailableListener(this);
    renderer.setSurface(surface);

    mCamera = Camera.open();

    try
    {
        mCamera.setPreviewTexture(surface);
        mCamera.startPreview();
    }
    catch (IOException ioe)
    {
        ioe.printStackTrace();
    }
}

public void onFrameAvailable(SurfaceTexture surfaceTexture)
{
        glSurfaceView.requestRender();
}

public void onOverlayImageButtonClicked(View view)
{
    if(glSurfaceView == null)
    {
        Toast.makeText(getApplicationContext(),"Preview is not available now!",Toast.LENGTH_LONG).show();
        return;
    }
    else {
        if(drawOnTop != null)
        {
            frameLayout.removeView(drawOnTop);
            drawOnTop = null;
        }
        else
        {
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.internetothings);
            drawOnTop = new DrawOnTop(this, bitmap);
            layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
            frameLayout.addView(drawOnTop, layoutParams);
            Toast.makeText(getApplicationContext(),"Click again to remove!",Toast.LENGTH_LONG).show();
        }
    }
}

public void onPause()
{
    super.onPause();
    mCamera.stopPreview();
    mCamera.release();
}
}

MyGLSurfaceView.java

public class MyGLSurfaceView extends GLSurfaceView{
static MyGLRenderer myGLRenderer;

public MyGLSurfaceView(Context c)
{
    super(c);
    setEGLContextClientVersion(2);
    myGLRenderer = new MyGLRenderer((MainActivity)c);
    setRenderer(myGLRenderer);
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}

public static MyGLRenderer getRenderer()
{
    return myGLRenderer;
}
}

MyGLRenderer.java

public class MyGLRenderer implements GLSurfaceView.Renderer{
int texture;
private SurfaceTexture surfaceTexture;
MainActivity mainActivity;

public MyGLRenderer(MainActivity main)
{
    mainActivity = main;
}

public void onSurfaceCreated(GL10 unused, javax.microedition.khronos.egl.EGLConfig config)
{
    texture = createTexture();
    GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    mainActivity.startCamera(texture);
}

public void onDrawFrame(GL10 unused)
{
    float[] mtx = new float[16];
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    surfaceTexture.updateTexImage();
    surfaceTexture.getTransformMatrix(mtx);
}

public void onSurfaceChanged(GL10 unused, int width, int height)
{
    GLES20.glViewport(0, 0, width, height);
}

static private int createTexture()
{
    int[] texture = new int[1];

    GLES20.glGenTextures(1, texture, 0);
    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture[0]);
    GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
            GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);
    GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
            GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
            GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
            GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);

    return texture[0];
}

public void setSurface(SurfaceTexture _surface)
{
    surfaceTexture = _surface;
}
}

问题1:毕竟,我得到的是灰色纹理而不是相机预览。作为参考,我检查了this帖子。请帮我确定这里出了什么问题。

问题2:在调查问题1时,我觉得(不确定)在 MyGLSurfaceView.java 中,将 myGLRenderer 声明为静态可以是个问题。此外,我已将方法 getRenderer()声明为静态,这可能会导致问题。所以我删除了静态关键字,如下所示 -

public class MyGLSurfaceView extends GLSurfaceView {
MyGLRenderer myGLRenderer;

public MyGLSurfaceView(Context c)
{
    super(c);
    setEGLContextClientVersion(2);
    myGLRenderer = new MyGLRenderer((MainActivity)c);
    setRenderer(myGLRenderer);
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}

public MyGLRenderer getRenderer()
{
    return myGLRenderer;
}
}

但后来我在这一行遇到了一个非常奇怪的问题 -

glSurfaceView = new MyGLSurfaceView(this);
renderer = glSurfaceView.getRenderer();

它说“无法解析方法getRenderer()”。

有人可以帮我解决这两个问题吗? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

您可以尝试各种选项: 我有问题零点异常:MainActivity.startCamera。

首先,我要说检查您的设备设置:设置 - >应用 - > (您的应用) - >权限并将其更改为相机。

其次,您可以尝试更改startCamera(),因为我们需要在执行事件后释放相机。修改如下:

int main(int argc, char* *argv){
    int n=atoi(argv[1]);
    ...

只需在MainActivity()

的底部添加releaseCameraAndPreview()
./recursionO3 15

试试看,告诉我们。也可以有其他选择,例如。更改清单文件:添加以下内容

 public void startCamera(int texture) {

    releaseCameraAndPreview();
    surface = new SurfaceTexture(texture);
    surface.setOnFrameAvailableListener(this);
    renderer.setSurface(surface);

    mCamera = Camera.open();
    Camera.Parameters parameters = mCamera.getParameters();
    mCamera.setParameters(parameters);

    try
    {
        mCamera.setPreviewTexture(surface);
        mCamera.startPreview();
    }
    catch (IOException ioe)
    {
        Log.w("MainActivity","CAM LAUNCH FAILED");
    }
}

希望这有帮助