readTexture
方法获取当前画面,查看画面中人脸的方向,根据下图设置对应的角度。public static Bitmap readTexture(int texture, int width, int height) { int[] frame = new int[1]; GLES20.glGenFramebuffers(1, frame, 0); GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frame[0]); GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, texture, 0); byte[] data = new byte[width * height * 4]; ByteBuffer buffer = ByteBuffer.wrap(data); GLES20.glPixelStorei(GLES20.GL_PACK_ALIGNMENT, GLES20.GL_TRUE); GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.copyPixelsFromBuffer(buffer); GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); GLES20.glDeleteFramebuffers(1, frame, 0); return bitmap; }
setImageOrientation
方法。readTexture
方法获取当前画面,查看画面中人脸的方向,根据下图设置对应的角度。#import <OpenGLES/ES2/gl.h>-(void)readTexture:(int)textureId width:(int)width height:(int)height{glBindTexture(GL_TEXTURE_2D, textureId);GLuint framebuffer;glGenFramebuffers(1, &framebuffer);glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureId, 0);GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);if (status != GL_FRAMEBUFFER_COMPLETE) {NSLog(@"Framebuffer is not complete.");}GLubyte *pixels = (GLubyte *)malloc(width * height * 4 * sizeof(GLubyte));glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);glBindFramebuffer(GL_FRAMEBUFFER, 0);glDeleteFramebuffers(1, &framebuffer);CVPixelBufferRef pixelBuffer = NULL;CVPixelBufferCreateWithBytes(NULL, width, height, kCVPixelFormatType_32BGRA, pixels, width * 4, NULL, NULL, NULL, &pixelBuffer);free(pixels);CVPixelBufferRelease(pixelBuffer);}
setImageOrientation
方法。
本页内容是否解决了您的问题?