Search code examples
javaandroidopengl-esscreenshot

Android: Remove certain colour from image


I current have some code that takes a screenshot and saves this to the SD card (if present.) What I now need to do is remove the background colour from that screenshot so it becomes transparent however I cant figure out how that works.

Below is the code I used to take the screenshot

         int b[]=new int[w*h];
         int bt[]=new int[w*h];
         IntBuffer ib=IntBuffer.wrap(b);
         ib.position(0);
         gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
         for(int i=0; i<h; i++)
         {//remember, that OpenGL bitmap is incompatible with Android bitmap
          //and so, some correction need.        
              for(int j=0; j<w; j++)
              {
                   int pix=b[i*w+j];
                   int pb=(pix>>16)&0xff;
                   int pr=(pix<<16)&0x00ff0000;
                   int pix1=(pix&0xff00ff00) | pr | pb;
                   bt[(h-i-1)*w+j]=pix1;
              }
         }                  
         Bitmap sb=Bitmap.createBitmap(bt, w, h, Bitmap.Config.RGB_565);
         return sb;

How do I remove a certain colour from that image or save it with a transparent background?


Solution

  • Why don't you examine the value of 'pix1'? So

    if(pix1 == replaceFrom)
        pix1 = replaceTo
    bt[(h-i-1)*w+j]=pix1;
    

    By the way, if you want transparent colour use ARGB_8888 instead of RGB_565. Furthermore, take a look at this: How to change colors of a Drawable in Android?