4

In my android project, I load a bitmap from a image in the phone. I then do various image manipulations to it like cropping, resizing, editing pixel values.

But the problem is the format of the bitmap is not ARGB_8888, so its not really storing the alpha values. Or rather its always keeping them at 255.

How can I load the bitmap in ARGB_8888 format? This is my code to load and resize. How can I specify the format in any of these?

Thanks

private static Bitmap resize(Bitmap img, int newW, int newH) throws IOException {
    Bitmap resizedImg = Bitmap.createScaledBitmap(img, newW, newH, false);
    img.recycle();

    Bitmap newresizedImg = resizedImg.copy(Bitmap.Config.ARGB_8888, true);
    resizedImg.recycle();


    Pixel initialPixel = Function.getPixel(0, 0, newresizedImg, null);
    int a = initialPixel.getColor().getAlpha(); // -> 255
    newresizedImg.setPixel(0, 0, Pixel.getTransparentColor().getRGB());
    initialPixel = Function.getPixel(0, 0, newresizedImg, null);
    int new_a = initialPixel.getColor().getAlpha(); // -> 255

    return newresizedImg;
}

private static Bitmap getImage(String from) throws IOException {
    File file = new File(from);

    if (file.exists()) {
        BitmapFactory.Options op = new BitmapFactory.Options(); 
        op.inPreferredConfig = Bitmap.Config.ARGB_8888; 
        Bitmap bufferedImage = BitmapFactory.decodeFile(from, op);
        return bufferedImage;
    }
    return null;
}

Pixel class

public static Color getTransparentColor() {
    return new Color(0, 0, 0, 0);
}

Color class

public int getRGB() {
    return ((A << 24) | 0xFF) + ((R << 16) | 0xFF) + ((G << 8) | 0xFF) + (B | 0xFF);
}
omega
  • 40,311
  • 81
  • 251
  • 474

4 Answers4

8

You can copy your bitmap using this type of function (or you know, not use a function depending on how you want to use it)

private Bitmap ARGBBitmap(Bitmap img) {
  return img.copy(Bitmap.Config.ARGB_8888,true);
}
Grambot
  • 4,370
  • 5
  • 28
  • 43
  • What file format you reading into it? I've used code very similar to yours above with great success. What file format? Can you perhaps provide a sample of the image you're having issues with? Have you stepped through the function with a debugger to check all the getPixel functions to make sure they're returning the results you expect? – Grambot Sep 02 '14 at 13:55
1

BitmapFactory.Options op = new BitmapFactory.Options(); op.inPreferredConfig = Bitmap.Config.ARGB_8888; bitmap = BitmapFactory.decodeFile(path, op);

from: How can I specify the bitmap format (e.g. RGBA_8888) with BitmapFactory.decode*()?

Community
  • 1
  • 1
TehCoder
  • 209
  • 1
  • 8
1

Hope this helps as this works for me.

Bitmap bitmap = Bitmap.createBitmap(marker.getMeasuredWidth(), marker.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
AkashToSky
  • 107
  • 1
  • 11
0

You can use this:

public Bitmap highlightImage(Bitmap src) { 
    Bitmap bmOut = Bitmap.createBitmap(src.getWidth() + 96, src.getHeight() + 96, Bitmap.Config.ARGB_8888); 
    return bmOut;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Soham
  • 4,397
  • 11
  • 43
  • 71