我正在开发一个需要使用 OpenGL 显示相机预览的 Android 项目。但是,我总是在一台设备(Galaxy Nexus)上获得稳定的白色图片,而在另一台设备(HTC One X)上获得黑色图片。下面列出了我认为存在错误的代码。
第一个方法loadGLTexture
在内部调用onSurfaceCreated
,然后是其他 gl 初始化。
public void loadGLTexture(GL10 gl, Context context)
{
// harcoded size
// int width = 640;
// int height = 480;
// I'm dealing with RGB565 image, so 2 byte per pixels.
image = new byte[640*480*2];
// native method that take the new frame from the camera buffer
Native.takeCurrentFrame(image);
// generate one texture pointer
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// set filter parameters
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
// allocate the buffer for glTexImage2D
byte_buffer = ByteBuffer.allocateDirect(image.length * 2);
byte_buffer.order(ByteOrder.nativeOrder());
byte_buffer.put(image);
byte_buffer.position(0);
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGB, 640, 480, 0, GL10.GL_RGB, GL10.GL_UNSIGNED_SHORT_5_6_5, byte_buffer);
}
这第二种方法draw
在内部调用onDrawFrame
public void draw(GL10 gl)
{
// update the texture with new image from camera
Native.takeCurrentFrame(image);
byte_buffer.put(image);
byte_buffer.position(0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// Point to buffers
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGB, 640, 480, 0, GL10.GL_RGB, GL10.GL_UNSIGNED_SHORT_5_6_5, byte_buffer);
// Set the face rotation
gl.glFrontFace(GL10.GL_CW);
// Point to vertex and coord buffer
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
// Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
感谢您的任何帮助!