[java] Create a BufferedImage from file and make it TYPE_INT_ARGB

I have a PNG file with transparency that is loaded and stored in a BufferedImage. I need this BufferedImage to be of TYPE_INT_ARGB. However, when I use getType() the returned value is 0 (TYPE_CUSTOM) instead of 2 (TYPE_INT_ARGB).

This is how I load the .png:

public File img = new File("imagen.png");

public BufferedImage buffImg = 
    new BufferedImage(240, 240, BufferedImage.TYPE_INT_ARGB);

try { 
    buffImg = ImageIO.read(img ); 
} 
catch (IOException e) { }

System.out.Println(buffImg.getType()); //Prints 0 instead of 2

How can I load the .png, save in the BufferedImage and make it TYPE_INT_ARGB?

This question is related to java image image-manipulation bufferedimage argb

The answer is


BufferedImage in = ImageIO.read(img);

BufferedImage newImage = new BufferedImage(
    in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);

Graphics2D g = newImage.createGraphics();
g.drawImage(in, 0, 0, null);
g.dispose();

Create a BufferedImage from file and make it TYPE_INT_RGB

import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
public class Main{
    public static void main(String args[]){
        try{
            BufferedImage img = new BufferedImage( 
                500, 500, BufferedImage.TYPE_INT_RGB );
            File f = new File("MyFile.png");
            int r = 5;
            int g = 25; 
            int b = 255;
            int col = (r << 16) | (g << 8) | b;
            for(int x = 0; x < 500; x++){
                for(int y = 20; y < 300; y++){
                    img.setRGB(x, y, col);
                }
            }
            ImageIO.write(img, "PNG", f); 
        }
        catch(Exception e){ 
            e.printStackTrace();
        }
    }
}

This paints a big blue streak across the top.

If you want it ARGB, do it like this:

    try{
        BufferedImage img = new BufferedImage( 
            500, 500, BufferedImage.TYPE_INT_ARGB );
        File f = new File("MyFile.png");
        int r = 255;
        int g = 10;
        int b = 57;
        int alpha = 255;
        int col = (alpha << 24) | (r << 16) | (g << 8) | b;
        for(int x = 0; x < 500; x++){
            for(int y = 20; y < 30; y++){
                img.setRGB(x, y, col);
            }
        }
        ImageIO.write(img, "PNG", f);
    }
    catch(Exception e){
        e.printStackTrace();
    }

Open up MyFile.png, it has a red streak across the top.


try {
    File img = new File("somefile.png");
    BufferedImage image = ImageIO.read(img ); 
    System.out.println(image);
} catch (IOException e) { 
    e.printStackTrace(); 
}

Example output for my image file:

BufferedImage@5d391d: type = 5 ColorModel: #pixelBits = 24 
numComponents = 3 color 
space = java.awt.color.ICC_ColorSpace@50a649 
transparency = 1 
has alpha = false 
isAlphaPre = false 
ByteInterleavedRaster: 
width = 800 
height = 600 
#numDataElements 3 
dataOff[0] = 2

You can run System.out.println(object); on just about any object and get some information about it.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to image

Reading images in python Numpy Resize/Rescale Image Convert np.array of type float64 to type uint8 scaling values Extract a page from a pdf as a jpeg How do I stretch an image to fit the whole background (100% height x 100% width) in Flutter? Angular 4 img src is not found How to make a movie out of images in python Load local images in React.js How to install "ifconfig" command in my ubuntu docker image? How do I display local image in markdown?

Examples related to image-manipulation

How to upload files in asp.net core? Create a BufferedImage from file and make it TYPE_INT_ARGB Get average color of image via Javascript Resizing Images in VB.NET What is the best java image processing library/approach? High Quality Image Scaling Library Javascript Image Resize Cropping an UIImage

Examples related to bufferedimage

Java converting Image to BufferedImage How to save a BufferedImage as a File Create a BufferedImage from file and make it TYPE_INT_ARGB Bufferedimage resize How to convert buffered image to image and vice-versa? Java - get pixel array from image Convert RGB values to Integer How to scale a BufferedImage

Examples related to argb

Transparent ARGB hex value Create a BufferedImage from file and make it TYPE_INT_ARGB