1

I have a bitmap something like that http://i.hizliresim.com/GYR6jV.jpg on file system. I need to make the black part of image transparent and obtain circular bitmap. the transparent method that i used:

 public static Bitmap makeTransparent(Bitmap bitmap, int replaceThisColor) {
        if (bitmap != null) {
            int picw = bitmap.getWidth();
            int pich = bitmap.getHeight();
            int[] pix = new int[picw * pich];
            bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);

            for (int y = 0; y < pich; y++) {
                // from left to right
                for (int x = 0; x < picw; x++) {
                    int index = y * picw + x;
                    int r = (pix[index] >> 16) & 0xff;
                    int g = (pix[index] >> 8) & 0xff;
                    int b = pix[index] & 0xff;

                    if (pix[index] == replaceThisColor) {
                        pix[index] = Color.TRANSPARENT;
                    } else {
                        break;
                    }
                }

                // from right to left
                for (int x = picw - 1; x >= 0; x--) {
                    int index = y * picw + x;
                    int r = (pix[index] >> 16) & 0xff;
                    int g = (pix[index] >> 8) & 0xff;
                    int b = pix[index] & 0xff;

                    if (pix[index] == replaceThisColor) {
                        pix[index] = Color.TRANSPARENT;
                    } else {
                        break;
                    }
                }
            }

            Bitmap bm = Bitmap.createBitmap(pix, picw, pich,
                    Bitmap.Config.ARGB_8888);

            return bm;
        }
        return null;
    }

Thanks.

baha
  • 99
  • 2
  • 11

1 Answers1

0

hey if you want to create a circle image you can use https://github.com/hdodenhof/CircleImageView

Edit: Another solution is to first draw a transparent image form ressource then draw the image in the disc (and only the disc)

Lary Ciminera
  • 1,270
  • 8
  • 15
  • no. i just need to upload the circular cropped image to server. So the cropping bitmap is necessary. – baha May 11 '15 at 13:59
  • oh, then maybe try Color.alpha(Color.TRANSPARENT) instead of Color.TRANSPARENT – Lary Ciminera May 11 '15 at 14:05
  • i think you have to call bitmap.setPixels(pix, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); somewhere. – Lary Ciminera May 11 '15 at 14:39
  • setpixels gives IllegalstateException on all possible lines. – baha May 11 '15 at 14:57
  • Ok, sorry i remember doing it but i can't remember how, sorry for the bad help, good luck. (maybe that can help you http://stackoverflow.com/questions/19019825/android-setpixels-explanation-and-example) – Lary Ciminera May 11 '15 at 14:59