Reputation: 31
Me and my team are currently developping an Android app to do fast live and non-live image processing.
We are facing two problems:
First, we would like to convert a Bitmap into a Texture to process the picture with OpenGL shaders, and then convert it back into a Bitmap. We tried some implementations unsuccesfully, as using the GLUtils.texImage2D function inside a SurfaceTexture and a Renderer.
Our second problem is that we don't currently know how to save a Texture on our live camera activity. We use an OnFrameAvailableListener in which we process the images. But as for now, we can't keep the original Texture.
We hope someone can provide an answer to our problem. Thanks in advance !
Upvotes: 3
Views: 8287
Reputation: 559
Here's a Kotlin version of Mohamed's answer
/**
* Load Texture from Bitmap
**/
fun loadTexture(context: Context, resourceId: Int) : Int {
val textureHandle : IntArray = IntArray(1)
GLES20.glGenTextures(1, textureHandle, 0)
if (textureHandle[0] != 0)
{
val options : BitmapFactory.Options = BitmapFactory.Options()
options.inScaled = false // No pre-scaling
// Read in the resource
val bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options)
// Bind to the texture in OpenGL
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0])
// Set filtering
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_NEAREST)
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0)
// Recycle the bitmap, since its data has been loaded into OpenGL.
bitmap.recycle()
}
if (textureHandle[0] == 0) {
throw RuntimeException("Error loading texture.")
}
return textureHandle[0]
}
Upvotes: 0
Reputation: 3645
First
Bitmap to texture
Source:
http://www.learnopengles.com/android-lesson-four-introducing-basic-texturing/
public static int loadTexture(final Context context, final int resourceId){
final int[] textureHandle = new int[1];
GLES20.glGenTextures(1, textureHandle, 0);
if (textureHandle[0] != 0)
{
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false; // No pre-scaling
// Read in the resource
final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);
// Bind to the texture in OpenGL
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
// Set filtering
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_NEAREST);
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
// Recycle the bitmap, since its data has been loaded into OpenGL.
bitmap.recycle();
}
if (textureHandle[0] == 0)
{
throw new RuntimeException("Error loading texture.");
}
return textureHandle[0];
}
Texture to bitmap:
Source:
How do you convert opengl texture back to bitmap in android?
Second
There is also a save option in the SO link I linked.
Upvotes: 8