Android texture image loading and modification,

Source: Internet
Author: User

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();        }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.