0

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Chris
  • 1,766
  • 1
  • 21
  • 36

1 Answers1

0

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?

Community
  • 1
  • 1
tungi52
  • 632
  • 1
  • 8
  • 15
  • Do you know what "replaceTo" should be to allow that pixel to be tranparent? – Chris Jan 24 '12 at 13:38
  • use Color.argb(a, r, g, b) to define color. If you want fully transparent color the alpha is 0 – tungi52 Jan 24 '12 at 19:35
  • Hi thanks for the help, I got it working but an outline remains around the image I want to keep. Its because there are some alpha blended pixels and unfortunaly that colour is different – Chris Jan 26 '12 at 12:54
  • You can examine the colours by their components too. if(red1==red2 && blue1==blue2) etc. See this: http://developer.android.com/reference/android/graphics/Color.html – tungi52 Jan 26 '12 at 15:53
  • It was close but its going to be impossible to fully remove all the background colour. Thanks tho – Chris Feb 02 '12 at 18:07