0

I am trying to read images from external directory and for that I am using

               bufferedImage image=ImageIO.read(new File(imagefile));
               jlabel.seticon(new imageicon(image));

and getting a drastic change in colors. I tried many other things like:

               bufferedImage image=ImageIO.read(new File(imagefile));
               bufferedImage img=new bufferedImage(image.getWidth(),image.getHeight(),bufferedImage.TYPE_INT_RGB);

and I tried:

               img.setData(image.getData();
               jlabel.seticon(new imageicon(image));

and I tried:

Iterator readers = ImageIO.getImageReadersByFormatName("JPEG");
ImageReader reader = null;
while(readers.hasNext()) {
    reader = (ImageReader)readers.next();
    if(reader.canReadRaster()) {
        break;  
         }
        }
ImageInputStream input =   ImageIO.createImageInputStream(f); 
reader.setInput(input); 
Raster raster = reader.readRaster(0, null); 
BufferedImage bi = new BufferedImage(raster.getWidth(), raster.getHeight(), 
BufferedImage.TYPE_4BYTE_ABGR); 
bi.getRaster().setRect(raster);

but result are still same https://i.stack.imgur.com/jNVm0.jpg

Here is an example of the issue:

enter image description here

The minimal code for viewing is:

      bufferedImage image=ImageIO.read(new File(imagefile));
      jlabel.seticon(new imageicon(image));      
      lbitem.setIcon(im);

and for storing

        File f = new File(s);
            long size=f.length();
            FileInputStream fis1=new FileInputStream(f);
            FileOutputStream fos2=new FileOutputStream("src/image/"+tfpn.getText()+".jpg");
            byte b[]=new byte[1000];
            int r=0;
            long count=0;
            while(true)
            {
                r=fis1.read(b,0,1000);
                fos2.write(b,0,1000);
                count = count+r;
                if(count==size)
                break;
                System.out.println(count);
            }

What could be causing the bad colors?

matthias_h
  • 11,356
  • 9
  • 22
  • 40
344
  • 47
  • 1
  • 3
  • 2
    I wonder if your problem is that you're using the wrong color model. I think that you'll want to check out the answers to these two SO questions: 1) [Java CMYK to RGB with profile. Output is too dark](http://stackoverflow.com/questions/8118712/java-cmyk-to-rgb-with-profile-output-is-too-dark) 2) [Problem reading JPEG image using ImageIO.read(File file)](http://stackoverflow.com/questions/2408613/problem-reading-jpeg-image-using-imageio-readfile-file) – Hovercraft Full Of Eels May 17 '14 at 13:40
  • can you provide me code for solving this problem – 344 May 17 '14 at 14:14
  • 3
    No. You haven't even indicated that you've tried anything from the links. Please read the links, try out their solutions and see if they apply. If they don't and you still need help, then you will need to create and post your [minimal code example program](http://stackoverflow.com/help/mcve) for us to review, test, and possibly fix. You will also need to post your images, both the original and the one that is not working. – Hovercraft Full Of Eels May 17 '14 at 14:23
  • i have tried this code – 344 May 17 '14 at 15:07
  • I've yet to see your [minimal code example program](http://stackoverflow.com/help/mcve) (you need to click on the link) or your images. – Hovercraft Full Of Eels May 17 '14 at 15:19
  • i have tried this code Iterator readers = ImageIO.getImageReadersByFormatName("JPEG"); ImageReader reader = null; while(readers.hasNext()) { reader = (ImageReader)readers.next(); if(reader.canReadRaster()) { break; }} ImageInputStream input = ImageIO.createImageInputStream(f); reader.setInput(input); Raster raster = reader.readRaster(0, null); BufferedImage bi = new BufferedImage(raster.getWidth(), raster.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); bi.getRaster().setRect(raster); but result are still same – 344 May 17 '14 at 15:19
  • i dont know how to post image – 344 May 17 '14 at 15:30
  • 1) Post a link to your images and then we can update your question and add the images if we can find them. 2) Don't post code in comments since it loses its formatting making it unreadable. Instead, post any new code to the bottom of your original question by [editing your question](http://stackoverflow.com/posts/23711664/edit). 3) Again you will need to create and post your [minimal code example program](http://stackoverflow.com/help/mcve) for us to allow us to understand your problem. Again, please read [this link](http://stackoverflow.com/help/mcve) to see how to create this. – Hovercraft Full Of Eels May 17 '14 at 15:35
  • OK, I've edited your questions and images have been uploaded. Now post your [minimal program](http://stackoverflow.com/help/mcve) please. – Hovercraft Full Of Eels May 17 '14 at 15:57
  • Also, is this image from online? If so, post a link to the original image. I cannot replicate your problem with your posted image. – Hovercraft Full Of Eels May 17 '14 at 15:59
  • problem is same similar thing is happening to my image – 344 May 17 '14 at 16:03
  • Again, please post a link to the *original* image. Again, please create and post your minimal program. – Hovercraft Full Of Eels May 17 '14 at 16:07
  • 2
    Hell, that's not even your image! You borrowed that from [here](http://stackoverflow.com/questions/4386446/problem-using-imageio-write-jpg-file). Please stop wasting our time and show a link to the real image and post your minimal code. – Hovercraft Full Of Eels May 17 '14 at 16:11
  • 2
    as @HovercraftFullOfEels says **1)** READ the links he provided and try them; **2)** if that doesn't fit yor needs, then READ [this link](http://stackoverflow.com/help/mcve) that Hovercraft provided too, **3)** DO what that link says and **4)** after that we'll be more able to help you :) good luck – Frakcool May 17 '14 at 16:17

1 Answers1

2

This problem is cause by a mismatch between reading/writing (creating/using) an image that contains alpha (transparency) but you are expecting it to contain no alpha (or the inverse). For example, if your image is BufferedImage.TYPE_4BYTE_ABGR and you output it to a file type that does not support alpha (transparency) , or you writer does not support alpha, it will look like your sample after reading and displaying it.

Use type PNG (supports alpha channel) not JPG (does not support alpha channel)

Java42
  • 7,628
  • 1
  • 32
  • 50