41

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?

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
user1319734
  • 427
  • 1
  • 4
  • 7
  • 3
    Change `public BufferedImage buffImg = new BufferedImage(240, 240, BufferedImage.TYPE_INT_ARGB);` to `public BufferedImage buffImg;` & `catch (IOException e) { }` to `catch (IOException e) { e.printStackTrace(); }`. Report the new output. – Andrew Thompson Apr 30 '12 at 23:43
  • 6
    `System.Out.Println` ***That would not compile.*** For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Apr 30 '12 at 23:46

3 Answers3

85
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();
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • 2
    This is extremely inefficient and becomes progressively less feasible as image size increases. – alexantd Oct 05 '15 at 18:40
  • Err, @alexantd, why is it so inefficient? Yes, it takes O(width x height) memory, and it could be done with constant memory, but it has the same time-complexity as any other approach, and any other approach would be a *lot* more messy. – tucuxi Jun 04 '16 at 17:20
  • @tucuxi It's less efficient than not needing any conversion approach at all due to the reader creating a BufferedImage of the correct type in the first place – alexantd Jun 06 '16 at 01:16
12
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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
kb9agt
  • 137
  • 1
  • 3
3

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335