Android texture image loading and modification,
Environment: eclipse, android, opengl es
Recently, we need to make some modifications to the texture image. First, we need to load the texture image, make some modifications to the texture image, and finally generate the texture object.
I. texture image loading
You can use the getResources (). openRawResource (int) function and bitmapFactory class to load texture images.
InputStream is = mContext.getResources().openRawResource( R.drawable.map_lr_new_n); Bitmap sbitmap = null; try { sbitmap = BitmapFactory.decodeStream(is); // dst = revertBitmapColor(bitmap); } finally { try { is.close(); } catch (IOException e) { // Ignore. } } @SuppressWarnings("deprecation") Drawable drawable = new BitmapDrawable(sbitmap); Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
Ii. Texture Image Modification
1. copy bitmap object
The image added from the file cannot be modified. Therefore, you need to copy the image to another bitmap object first.
Bitmap bitmap2=bitmap.copy(bitmap.getConfig(), true); ModifyBitmap(bitmap2);
2. modify a bitmap object
Modify the bitmap object using the setPixel function in bitmap, which also involves the rgb value assignment of the int type.
public void ModifyBitmap(Bitmap bitmap) { int nw=bitmap.getWidth(); int nh=bitmap.getHeight(); //int alpha= int col1=0; int col2=0; for(int i=0;i<nh;i++) { for(int j=0;j<nw-1;j++) { int p=bitmap.getPixel(j+1, i); int r=(p& 0x00FF0000)>>16; int g=(p& 0x0000FF00)>>8; int b= p& 0x000000FF; int a=255-k*50; p=(a<<24)|(r<<16)|(g<<8)|(b); int tempA=p; bitmap.setPixel(j+1-k, i,tempA) ; } } } }
3. Texture binding
Use functions in opengl es to bind the texture.
if (bitmap != null) { glBindTexture(GLES11.GL_TEXTURE_2D, drawTestTxId[0]); glTexParameteri(GLES11.GL_TEXTURE_2D, GLES11.GL_TEXTURE_MAG_FILTER, GLES11.GL_LINEAR); glTexParameteri(GLES11.GL_TEXTURE_2D, GLES11.GL_TEXTURE_MIN_FILTER, GLES11.GL_LINEAR); glTexParameteri(GLES11.GL_TEXTURE_2D, GLES11.GL_TEXTURE_WRAP_S, GLES11.GL_REPEAT); glTexParameteri(GLES11.GL_TEXTURE_2D, GLES11.GL_TEXTURE_WRAP_T, GLES11.GL_REPEAT); GLUtils.texImage2D(GLES10.GL_TEXTURE_2D, 0, (bitmap 0); bitmap.recycle(); }