JFrame无法保存为图像。

huangapple go评论73阅读模式
英文:

JFrame won't save as image

问题

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class BorderImage extends JFrame {
	
    public static void main(String[] args){	
        try {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JFileChooser fc = new JFileChooser();

            if(fc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION){
                BufferedImage img = ImageIO.read(fc.getSelectedFile());
                JLabel label = new JLabel(new ImageIcon(img), JLabel.CENTER);

                int w = img.getWidth();
                int h = img.getHeight();
                if (w > h) {
                    int diff = (w - h)/2;
                    frame.getContentPane().setBackground(Color.BLACK);
                    frame.setSize(w, w);
                } else if (h > w) {
                    frame.getContentPane().setBackground(Color.BLACK);
                    frame.setSize(h, h);
                }		

                frame.getContentPane().add(label);
                frame.setVisible(true);

                try {
                    BufferedImage save = new BufferedImage(frame.getWidth(),
                            frame.getHeight(), BufferedImage.TYPE_INT_ARGB);	
                    Graphics2D graphics = save.createGraphics();	        	
                    frame.printAll(graphics);	        	
                    graphics.dispose();	
                    ImageIO.write(save, "jpg", new File("newImage.jpg"));

                } catch (IOException x) {
                    x.printStackTrace();
                }
            }  
        } catch(IOException e) {
            e.printStackTrace();
        }
    }  
}
英文:

I've search a lot, but yet i still can't figure out where's the issue. The image display as i expected but it doesn't save it.

I've watch answers like this:
https://stackoverflow.com/questions/12575201/how-to-save-a-image-on-jframe
https://stackoverflow.com/questions/22161550/how-to-save-an-image-from-a-jlabel

Looks very similar the save process. I even download a simple code using a similar way and it works. i've tried with "paint()" instead of "printAll()" and don't work neither.

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class BorderImage extends JFrame {
public static void main(String[] args){	
try {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFileChooser fc = new JFileChooser();
if(fc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION){
BufferedImage img = ImageIO.read(fc.getSelectedFile());
JLabel label = new JLabel(new ImageIcon(img), JLabel.CENTER);
int w = img.getWidth();
int h = img.getHeight();
if (w > h) {
int diff = (w - h)/2;
frame.getContentPane().setBackground(Color.BLACK);
frame.setSize(w, w);
} else if (h > w) {
frame.getContentPane().setBackground(Color.BLACK);
frame.setSize(h, h);
}		
frame.getContentPane().add(label);
frame.setVisible(true);
try {
BufferedImage save = new BufferedImage(frame.getWidth(),
frame.getHeight(), BufferedImage.TYPE_INT_ARGB);	
Graphics2D graphics = save.createGraphics();	        	
frame.printAll(graphics);	        	
graphics.dispose();	
ImageIO.write(save, "jpg", new File("newImage.jpg"));
} catch (IOException x) {
x.printStackTrace();
}
}  
} catch(IOException e) {
e.printStackTrace();
}
}  
}

答案1

得分: 3

BufferedImage save = new BufferedImage(..., ..., BufferedImage.TYPE_INT_ARGB);

似乎 `jpg` 文件不支持图像类型 `ARGB`。

如果您需要保留 alpha 通道,则 `png` 格式适用(对我来说)。

如果您需要使用 `jpg` 格式,则可以使用图像类型 `RGB`:

BufferedImage save = new BufferedImage(..., ..., BufferedImage.TYPE_INT_RGB);
英文:
BufferedImage save = new BufferedImage(..., ..., BufferedImage.TYPE_INT_ARGB); 

It appears that jpg files don't like the image type ARGB.

If you want the alpha then png works (for me).

It you want jpg then you can use an image type of RGB:

BufferedImage save = new BufferedImage(..., ..., BufferedImage.TYPE_INT_RGB); 

huangapple
  • 本文由 发表于 2020年10月8日 04:49:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64252107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定