其他分享
首页 > 其他分享> > Android多种方式实现相机圆形预览 看这一篇就够了,吐血整理

Android多种方式实现相机圆形预览 看这一篇就够了,吐血整理

作者:互联网

*/

void onSurfaceChanged(GL10 gl, int width, int height);

/**

*/

void onDrawFrame(GL10 gl);

}

在Surface创建或重建的情况下回调

在Surface的大小发生变化的情况下回调

在这里实现绘制操作。当我们设置的renderModeRENDERMODE_CONTINUOUSLY时,该函数将不断地执行;

当我们设置的renderModeRENDERMODE_WHEN_DIRTY时,将只在创建完成和调用requestRender后才执行。一般我们选择RENDERMODE_WHEN_DIRTY渲染模式,避免过度绘制。

一般情况下,我们会自己实现一个Renderer,然后为GLSurfaceView设置Renderer,可以说,Renderer的编写是整个流程的核心步骤。以下是在void onSurfaceCreated(GL10 gl, EGLConfig config)进行的初始化操作和在void onDrawFrame(GL10 gl)进行的绘制操作的流程图:

2. 具体实现

如图所示,和Android的View坐标系不同,OpenGL的坐标系是笛卡尔坐标系。

Android View的坐标系以左上角为原点,向右x递增,向下y递增

而OpenGL坐标系以中心为原点,向右x递增,向上y递增

/**

*/

private static String VERTEX_SHADER =

" attribute vec4 attr_position;\n" +

" attribute vec2 attr_tc;\n" +

" varying vec2 tc;\n" +

" void main() {\n" +

" gl_Position = attr_position;\n" +

" tc = attr_tc;\n" +

" }";

/**

*/

private static String FRAG_SHADER =

" varying vec2 tc;\n" +

" uniform sampler2D ySampler;\n" +

" uniform sampler2D uSampler;\n" +

" uniform sampler2D vSampler;\n" +

" const mat3 convertMat = mat3( 1.0, 1.0, 1.0, -0.001, -0.3441, 1.772, 1.402, -0.7141, -0.58060);\n" +

" void main()\n" +

" {\n" +

" vec3 yuv;\n" +

" yuv.x = texture2D(ySampler, tc).r;\n" +

" yuv.y = texture2D(uSampler, tc).r - 0.5;\n" +

" yuv.z = texture2D(vSampler, tc).r - 0.5;\n" +

" gl_FragColor = vec4(convertMat * yuv, 1.0);\n" +

" }";

VERTEX_SHADER代码里的gl_Position代表绘制的空间坐标。由于我们是二维绘制,所以直接传入OpenGL二维坐标系的左下(-1,-1)、右下(1,-1)、左上(-1,1)、右上(1,1),也就是{-1,-1,1,-1,-1,1,1,1}

FRAG_SHADER代码里的gl_FragColor代表单个片元的颜色

分别代表Y、U、V纹理采样器

根据以下公式:

R = Y + 1.402 (V - 128)

G = Y - 0.34414 (U - 128) - 0.71414 (V - 128)

B = Y + 1.772 (U - 128)

我们可得到一个YUV转RGB的矩阵

1.0, 1.0, 1.0,

0, -0.344, 1.77,

1.403, -0.714, 0

分别代表三维向量、四维向量。

以指定的矩阵将采样器的图像纹理转换为颜色值;如:

texture2D(ySampler, tc).r获取到的是Y数据,

texture2D(uSampler, tc).r获取到的是U数据,

texture2D(vSampler, tc).r获取到的是V数据。

根据图像宽高创建Y、U、V对应的ByteBuffer纹理数据;

根据是否镜像显示、旋转角度选择对应的转换矩阵;

public void init(boolean isMirror, int rotateDegree, int frameWidth, int frameHeight) {

if (this.frameWidth == frameWidth

&& this.frameHeight == frameHeight

&& this.rotateDegree == rotateDegree

&& this.isMirror == isMirror) {

return;

}

dataInput = false;

this.frameWidth = frameWidth;

this.frameHeight = frameHeight;

this.rotateDegree = rotateDegree;

this.isMirror = isMirror;

yArray = new byte[this.frameWidth * this.frameHeight];

uArray = new byte[this.frameWidth * this.frameHeight / 4];

vArray = new byte[this.frameWidth * this.frameHeight / 4];

int yFrameSize = this.frameHeight * this.frameWidth;

int uvFrameSize = yFrameSize >> 2;

yBuf = ByteBuffer.allocateDirect(yFrameSize);

yBuf.order(ByteOrder.nativeOrder()).position(0);

uBuf = ByteBuffer.allocateDirect(uvFrameSize);

uBuf.order(ByteOrder.nativeOrder()).position(0);

vBuf = ByteBuffer.allocateDirect(uvFrameSize);

vBuf.order(ByteOrder.nativeOrder()).p
osition(0);

// 顶点坐标

squareVertices = ByteBuffer

.allocateDirect(GLUtil.SQUARE_VERTICES.length * FLOAT_SIZE_BYTES)

.order(ByteOrder.nativeOrder())

.asFloatBuffer();

squareVertices.put(GLUtil.SQUARE_VERTICES).position(0);

//纹理坐标

if (isMirror) {

switch (rotateDegree) {

case 0:

coordVertice = GLUtil.MIRROR_COORD_VERTICES;

break;

case 90:

coordVertice = GLUtil.ROTATE_90_MIRROR_COORD_VERTICES;

break;

case 180:

coordVertice = GLUtil.ROTATE_180_MIRROR_COORD_VERTICES;

break;

case 270:

coordVertice = GLUtil.ROTATE_270_MIRROR_COORD_VERTICES;

break;

default:

break;

}

} else {

switch (rotateDegree) {

case 0:

coordVertice = GLUtil.COORD_VERTICES;

break;

case 90:

coordVertice = GLUtil.ROTATE_90_COORD_VERTICES;

break;

case 180:

coordVertice = GLUtil.ROTATE_180_COORD_VERTICES;

break;

case 270:

coordVertice = GLUtil.ROTATE_270_COORD_VERTICES;

break;

default:

break;

}

}

coordVertices = ByteBuffer.allocateDirect(coordVertice.length * FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();

coordVertices.put(coordVertice).position(0);

}

在Surface创建完成时进行Renderer初始化

private void initRenderer() {

rendererReady = false;

createGLProgram();

//启用纹理

GLES20.glEnable(GLES20.GL_TEXTURE_2D);

//创建纹理

createTexture(frameWidth, frameHeight, GLES20.GL_LUMINANCE, yTexture);

createTexture(frameWidth / 2, frameHeight / 2, GLES20.GL_LUMINANCE, uTexture);

createTexture(frameWidth / 2, frameHeight / 2, GLES20.GL_LUMINANCE, vTexture);

rendererReady = true;

}

其中createGLProgram用于创建OpenGL Program并关联着色器代码中的变量

private void createGLProgram() {

int programHandleMain = GLUtil.createShaderProgram();

if (programHandleMain != -1) {

// 使用着色器程序

GLES20.glUseProgram(programHandleMain);

// 获取顶点着色器变量

int glPosition = GLES20.glGetAttribLocation(programHandleMain, “attr_position”);

int textureCoord = GLES20.glGetAttribLocation(programHandleMain, “attr_tc”);

// 获取片段着色器变量

int ySampler = GLES20.glGetUniformLocation(programHandleMain, “ySampler”);

int uSampler = GLES20.glGetUniformLocation(programHandleMain, “uSampler”);

int vSampler = GLES20.glGetUniformLocation(programHandleMain, “vSampler”);

//给变量赋值

/**

*/

GLES20.glUniform1i(ySampler, 0);

GLES20.glUniform1i(uSampler, 1);

GLES20.glUniform1i(vSampler, 2);

GLES20.glEnableVertexAttribArray(glPosition);

GLES20.glEnableVertexAttribArray(textureCoord);

/**

*/

squareVertices.position(0);

GLES20.glVertexAttribPointer(glPosition, GLUtil.COUNT_PER_SQUARE_VERTICE, GLES20.GL_FLOAT, false, 8, squareVertices);

coordVertices.position(0);

GLES20.glVertexAttribPointer(textureCoord, GLUtil.COUNT_PER_COORD_VERTICES, GLES20.GL_FLOAT, false, 8, coordVertices);

}

}

其中createTexture用于根据宽高和格式创建纹理

private void createTexture(int width, int height, int format, int[] textureId) {

//创建纹理

GLES20.glGenTextures(1, textureId, 0);

//绑定纹理

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);

/**

*/

GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);

GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);

/**

*/

GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);

GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, format, width, height, 0, format, GLES20.GL_UNSIGNED_BYTE, null);

}

在数据源获取到时裁剪并传入帧数据

@Override

public void onPreview(final byte[] nv21, Camera camera) {

//裁剪指定的图像区域

ImageUtil.cropNV21(nv21, this.squareNV21, previewSize.width, previewSize.height, cropRect);

//刷新GLSurfaceView

roundCameraGLSurfaceView.refreshFrameNV21(this.squareNV21);

}

NV21数据裁剪代码

/**

*/

public static void cropNV21(byte[] originNV21, byte[] cropNV21, int width, int height, int left, int top, int right, int bottom) {

int halfWidth = width / 2;

int cropImageWidth = right - left;

int cropImageHeight = bottom - top;

//原数据Y左上

int originalYLineStart = top * width;

int targetYIndex = 0;

//原数据UV左上

int originalUVLineStart = width * height + top * halfWidth;

//目标数据的UV起始值

int targetUVIndex = cropImageWidth * cropImageHeight;

for (int i = top; i < bottom; i++) {

System.arraycopy(originNV21, originalYLineStart + left, cropNV21, targetYIndex, cropImageWidth);

originalYLineStart += width;

targetYIndex += cropImageWidth;

if ((i & 1) == 0) {

System.arraycopy(originNV21, originalUVLineStart + left, cropNV21, targetUVIndex, cropImageWidth);

originalUVLineStart += width;

targetUVIndex += cropImageWidth;

}

}

}

传给GLSurafceView并刷新帧数据

/**

*/

public void refreshFrameNV21(byte[] data) {

if (rendererReady) {

yBuf.clear();

uBuf.clear();

vBuf.clear();

putNV21(data, frameWidth, frameHeight);

dataInput = true;

requestRender();

}

}

其中putNV21用于将NV21中的Y、U、V数据分别取出

/**

*/

private void putNV21(byte[] src, int width, int height) {

int ySize = width * height;

int frameSize = ySize * 3 / 2;

//取分量y值

System.arraycopy(src, 0, yArray, 0, ySize);

int k = 0;

//取分量uv值

int index = ySize;

while (index < frameSize) {

vArray[k] = src[index++];

uArray[k++] = src[index++];

}

yBuf.put(yArray).position(0);

uBuf.put(uArray).position(0);

vBuf.put(vArray).position(0);

}

在执行requestRender后,onDrawFrame函数将被回调,在其中进行三个纹理的数据绑定并绘制

@Override

public void onDrawFrame(GL10 gl) {

// 分别对每个纹理做激活、绑定、设置数据操作

if (dataInput) {

//y

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, yTexture[0]);

GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D,

0,

0,

0,

frameWidth,

frameHeight,

GLES20.GL_LUMINANCE,

GLES20.GL_UNSIGNED_BYTE,

yBuf);

//u

GLES20.glActiveTexture(GLES20.GL_TEXTURE1);

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, uTexture[0]);

GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D,

0,

0,

0,

frameWidth >> 1,

frameHeight >> 1,

GLES20.GL_LUMINANCE,

GLES20.GL_UNSIGNED_BYTE,

uBuf);

//v

GLES20.glActiveTexture(GLES20.GL_TEXTURE2);

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, vTexture[0]);

GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D,

0,

0,

0,
ion(0);

}

在执行requestRender后,onDrawFrame函数将被回调,在其中进行三个纹理的数据绑定并绘制

@Override

public void onDrawFrame(GL10 gl) {

// 分别对每个纹理做激活、绑定、设置数据操作

if (dataInput) {

//y

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, yTexture[0]);

GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D,

0,

0,

0,

frameWidth,

frameHeight,

GLES20.GL_LUMINANCE,

GLES20.GL_UNSIGNED_BYTE,

yBuf);

//u

GLES20.glActiveTexture(GLES20.GL_TEXTURE1);

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, uTexture[0]);

GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D,

0,

0,

0,

frameWidth >> 1,

frameHeight >> 1,

GLES20.GL_LUMINANCE,

GLES20.GL_UNSIGNED_BYTE,

uBuf);

//v

GLES20.glActiveTexture(GLES20.GL_TEXTURE2);

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, vTexture[0]);

GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D,

0,

0,

0,

标签:gl,1.0,预览,int,就够,TEXTURE,Android,GL,GLES20
来源: https://blog.csdn.net/m0_66264134/article/details/122759394