英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论