How to set bitmap to ARGB_8888 in android?

☆樱花仙子☆ 提交于 2019-12-22 00:16:29

问题


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

回答1:


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



回答2:


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*()?




回答3:


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



回答4:


Hope this helps as this works for me.

Bitmap bitmap = Bitmap.createBitmap(marker.getMeasuredWidth(), marker.getMeasuredHeight(), Bitmap.Config.ARGB_8888);


来源:https://stackoverflow.com/questions/25576869/how-to-set-bitmap-to-argb-8888-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!