Java Imagio.write () changes quality while saving

I am writing an image library for fun, and I ran into a problem that I cannot solve. The class is quite simple: take a picture, process it, show it through a JFrame, and finally save it as a BufferedImage (javax.imageio.ImageIO). Here's what my picture looks like through JFrame (this is my ColorEnhance class ... on the Drustan Nebula):

The image as seen from the JFrame

Here is the saved version (png, but all ImageIO.write () types are supported the same way):

The image as seen after it has been saved

I'm not sure where the change happens, but when I run it through my blur method, all lines appear from nothing in png ... Anyway, 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); } 

The last thing that saving works for some processing classes is better than others, if you need to see more code, just ask, thanks

+4
source share
1 answer

Try using PNGImageEncoder from the Apache XML Graphic Community :

 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); PNGImageEncoder encoder = new PNGImageEncoder(new FileOutputStream("file.png"), null); encoder.encode((RenderedImage) image); 
+1
source

Source: https://habr.com/ru/post/1479084/


All Articles