Question

I am writing an image library for fun and i came across a problem that i can't seem to solve. The class is pretty simple: take a picture, process it, display it through JFrame, and finally save it as a BufferedImage (javax.imageio.ImageIO). Here is what my picture looks like through the JFrame (this is my ColorEnhance class... on the Drustan nebula):

The image as seen from the JFrame

Here is what the saved version (a png, but all types ImageIO.write() supports look the same):

The image as seen after it has been saved

I'm not sure where the change occurs, but when I run this through my blur method entire lines appear from nothing in the png... Anyways, here is some code:

public void writeToFile(BufferedImage finalPic, String nameToAppend)
{
    String temp=fileName.replace(".", nameToAppend+".");
    String ext=fileName.substring(fileName.indexOf(".")+1);
    File file=new File(temp);
    try
    {
        ImageIO.write(finalPic, ext.toUpperCase(), file);
        System.out.println("Successfully saved to: "+temp);
    } catch (Exception e) { e.getMessage(); }
}

public void displayImage(String titleName)
{
    ImageIcon icon = new ImageIcon(newPic);
    JFrame frame = new JFrame(titleName);
    JLabel label = new JLabel(icon);
    label.setIcon(icon);
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.setSize(WIDTH, HEIGHT+22);
    frame.setVisible(true);
}

One last thing is that the save works for some processing classes better than others, if you need to see any more code just ask, thanks

No correct solution

OTHER TIPS

Try using PNGImageEncoder from Apache XML Graphics Commons:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
PNGImageEncoder encoder = new PNGImageEncoder(new FileOutputStream("file.png"), null);
encoder.encode((RenderedImage) image);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top