【问题标题】:JFrame won't save as imageJFrame 不会保存为图像
【发布时间】:2020-10-07 20:49:23
【问题描述】:

我搜索了很多,但我仍然无法弄清楚问题出在哪里。图像按预期显示,但没有保存。

我看过这样的答案:
https://stackoverflow.com/questions/12575201/how-to-save-a-image-on-jframe\ How to save an Image from a JLabel

保存过程看起来非常相似。我什至使用类似的方式下载了一个简单的代码,它可以工作。我试过用“paint()”而不是“printAll()”,但也不行。

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();
            }
        }  
    }

【问题讨论】:

  • 到底出了什么问题?是否抛出异常?您找不到图像保存到的位置吗?如果是这样,您是否尝试过使用像 "C:/newImage.jpg" 这样的绝对路径?
  • 这是最大的问题。抛出任何异常,一切似乎都正确,是的,我尝试了绝对路径。
  • @Ginazai (1+) 好问题和minimal reproducible example。我以前从未注意到这种行为,所以我也学到了一些东西。

标签: java swing jlabel graphics2d javax.imageio


【解决方案1】:
BufferedImage save = new BufferedImage(..., ..., BufferedImage.TYPE_INT_ARGB); 

似乎jpg 文件不喜欢ARGB 的图像类型。

如果你想要 alpha,那么 png 可以工作(对我来说)。

如果您想要jpg,那么您可以使用RGB 的图像类型:

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

【讨论】:

  • 它有效。非常感谢,但真的非常非常感谢。这可能是一个简单的问题,但由于我不知道在哪里搜索它真的很令人沮丧。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-15
  • 2016-04-07
  • 1970-01-01
相关资源
最近更新 更多