How to draw a bitmap on Android using OpenGL

Viewed 18929

I have a byte array of RGB values, just like the contents(without header) of a *.bmp file. What I want to do is, draw the corresponding bitmap on Android, using OpenGL.

It seems that OpenGL ES doesn't have a single API that will do, it true?

If true, how can I do this?

PS: Actually, I can draw it in JAVA code, but it's too slow and costs too much CPU time. So I want to try drawing it with OpenGL. Is there any other advises? Or maybe OpenGL can't be the right answer?

Thank you all!

3 Answers

I'm not sure the answers you got were related to the question you asked. I believe I had the same question, however I was not interested in drawing a bitmap on the screen, I was rather interested in creating (drawing on) a bitmap fast, using GL instead of java computations. Since I could find no reply to my question anywhere on the web I just started reading the GL API's and the way I solved my problem was by reading the pixels drawn by my onDrawFrame method with glReadPixels after turning on a 'grabdata' flag, somewhere else in the program, something like this:

// imports
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import javax.microedition.khronos.opengles.GL10;
import android.graphics.Bitmap;
import android.opengl.GLES20;


//somewhere inside the class that implements GLSurfaceView.Renderer:
    
ByteBuffer data;
private int height;
private int width;
boolean grabdata=false;
    
public void onSurfaceChanged(GL10 glUnused, int width, int height)  {
this.width=width;
this.height=height;
data=ByteBuffer.allocateDirect(width*height*4).order(ByteOrder.nativeOrder());
/*
......
prepare the surface
.......                  
*/
}

public void onDrawFrame(GL10 glUnused) {

/*
actually draw whatever you want, set matrices, rotate, draw etc... 
drawTriangle(mTriangleVertices);
.
. 
.
*/
if (grabdata) {
 GLES20.glReadPixels(0,0,width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE,data);
 grabdata=false; //until we want to read again
 //create a bitmap now if you havent prepared it somewhere else.
 Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
 Bitmap bitmap = Bitmap.createBitmap(width, height, conf);
 bitmap.copyPixelsFromBuffer(data);
 //do something with it....
 // storeImage(bitmap);
 data.clear();
 }
}
Related