如何通过opengl显示相机预览 [英] How to show camera preview by opengl

查看:439
本文介绍了如何通过opengl显示相机预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过SurfaceTexture显示相机预览.我已经阅读了此示例,它运行良好: 使用SurfaceTexture和OpenGL修改摄像头输出

I want to show camera preview by SurfaceTexture. I have read this sample and it work well:: Modifying camera output using SurfaceTexture and OpenGL

现在我要对预览帧进行一些处理,如何获取数据并推送到纹理?

Now I want to do some process to the preview frames, how to get the data and push to the texture?

推荐答案

有两种方法可以使用opengl显示此像素数据

There are two ways to use opengl to show this pixels data

  1. 在JAVA中使用Opengl

  1. Using Opengl in JAVA

在c或c ++(JNI)中使用opengl

Using opengl in c or c++ ( JNI )

实际上,无论哪种方式,如果您知道什么是opengl管道

Actually, either way, It is going to be easy if you know what opengl pipeline is

由于可重用性,我始终将opengl与c ++一起使用,只需遵循以下三个步骤

I alway use opengl with c++ because of reusability anyway, just follow three steps below

  1. 使用android API中的预览缓冲区制作纹理图像(像素是指向相机帧缓冲区的指针)

  1. make a texture image using the preview buffer from android API (pixel is a pointer which points the camera frame buffer)

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width, texture_height, 0,GL_RGBA, GL_UNSIGNED_BYTE, pixel); 

  • 将纹理传递给着色器

  • pass the texture to shader

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, m_TextureMain);
        glUniform1i(textures[0][displayMode], 0);
    

  • 在帧着色器上处理纹理(texutre0是我们刚刚制作的,此代码将其处理为灰度图像)

  • process the texture at the frament shader ( texutre0 is what we just made and this code process it to grayscale image )

          static char grayscale[] = 
                "precision highp float; \n"
                "uniform sampler2D texture0;\n"
                "varying vec2 textureCoordinate;\n"
                "void main()\n"
                "{\n"
                "    float grayValue = dot(texture2D(texture0, textureCoordinate), vec4(0.3, 0.59, 0.11, 0.0));\n"
                "   gl_FragColor = vec4(grayValue, grayValue, grayValue, 1.0);\n"
                "}\n";
    

  • 我省略了opengl所需的许多步骤,因为它太长了,例如绑定,生成帧缓冲区,渲染缓冲区,着色器设置和编译,以及如何进行双缓冲以避免轻拂. 如果您不是这些,请查找示例并更改三个步骤.

    I omitted many steps which is required in opengl because It's too long such as binding, generate a framebuffer, a renderbuffer, shader settings and compile it and how to do double buffering to avoid flicking. If you don't what these are, find an example and change the three steps.

    这篇关于如何通过opengl显示相机预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆