0

In the code i'm setting the alpha value of a pixel to 100 for entire image and I want the Alpha value to be 100 while reading the image. But at the retrieving part it gives me 255(Default Value) . What is wrong ? and how to solve it ? Any Help would be appreciated...

class Demo {

Demo()
{     
  try
  {

    BufferedImage im2 = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
    File f2 = new File("test2.jpg");
    im2 = ImageIO.read(f2);       
    int width1 = im2.getWidth();
    int height1 = im2.getHeight();

    for(int i=0;i<height1;i++)
    {
        for(int j=0;j<width1;j++)
        {
            Color c = new Color(50,0,0,100);   //Set the alpha value to 100
           im2.setRGB(j,i,c.getRGB());         //  for every pixel
        }
    }
    File f = new File("Demo_copy.jpg");
    ImageIO.write(im2,"jpg",f);

    //  Retrieving.........
    BufferedImage im1;
    File f1 = new File("Demo_copy.jpg");
    im1 = ImageIO.read(f1);
    int width = im1.getWidth();
    int height = im1.getHeight();
    for(int i=0;i<height;i++)
    {
        for(int j=0;j<width;j++)
        {
        int pixel = im1.getRGB(j,i);
        Color c = new Color(pixel,true);
        int a = c.getAlpha();  
        System.out.println("Alpha value is :"+a);  // Printing Alpha : 255 for every pixel
        }
    }
  }catch(Exception e){}
}
public static void main(String [] ar)
{
    new Demo(); 
}

}

1 Answers1

0

The new BufferedImage(...) you assign to im2 is just thrown away (garbage collected) after you assign a new value from ImageIO.read(..). As the new value is a JPEG and doesn't have alpha, it does not matter what alpha values you set. They will always stay 255 (completely opaque).

Instead, you probably want to do something like this:

// Read opaque image...
BufferedImage img = ImageIO.read(new File("test2.jpg")); 

// ...convert image to TYPE_INT_ARGB...
BufferedImage im2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

Graphics2D g = im2.createGraphics();
try {
    g.drawImage(img, 0, 0, null);
}
finally {
    g.dispose();
}

// ... loop over and change alpha in im2 as before

Finally, you should write the image in a format that supports lossless alpha, like PNG instead of JPEG, to be sure you get the values you expect:

ImageIO.write(im2,"PNG", new File("Demo_copy.png"));

PS: It might just work using JPEG too, as the built-in Java ImageIO JPEG plugin supports reading/writing JPEGs with alpha values. However, most other software will misinterpret these as CMYK JPEGs, and the colors will look all wrong. Also, JPEG is lossy, so you will most likely not see the exact alpha value (100) as you would expect on the receiving end. That's why I suggest using PNG. TIFF or other format that supports alpha would also work, but requires extra plugins.

Harald K
  • 26,314
  • 7
  • 65
  • 111
  • Good stuff! Consider marking the answer as accepted, to indicate that it solved your problem. – Harald K Nov 03 '16 at 12:22
  • Sir, the above solution works perfectly for png files. But is there any method with which I can implement it for jpg files as well. I want to set values of my choice in jpg image and want to retrieve the same. – Keshav Joshi Nov 15 '16 at 11:34
  • The above solution will also work for JPEG, except that **a)** *JPEG is a lossy compression, so the values you read will not be exactly the same values you wrote* and **b)** *storing ARGB in a JPEG stream is not JFIF compliant, and most other software will misinterpret it as CMYK (or YCCK)*. That is why I *recommend* using PNG. As stated in my answer. But if you can live with the above, you can also use JPEG. – Harald K Nov 15 '16 at 12:17
  • So is there any method or trick with which I can retrieve the same values in jpg format. I am working on one project and for that project to be accomplished , I need the exact values at retrieving end. Atlast, Thanks for your so much help.... – Keshav Joshi Nov 15 '16 at 14:38
  • @KeshavJoshi You don't seem to understand the concept of lossy compression. Data is *lost*. Thrown away. **There's no way to recreate the exact same values after this process**. You can of course use JPEG-Lossless, but that is quite a different thing than the standard JPEG compression. It's not supported by ImageIO out of the box and less supported generally. It will be much easier to just use PNG. – Harald K Nov 16 '16 at 11:15