-1

我正在尝试从 BufferImage 制作图像,但它不起作用。这是我的代码...

此代码不起作用,任何人都可以帮助我...

try {

       BufferedImage bimage = (BufferedImage)(new ImageIcon("str")).getImage();

       BufferedImage image = new BufferedImage(500, 500, bimage.TYPE_BYTE_GRAY);
       File outputfile = new File("saved.png");
       ImageIO.write(image, "png", outputfile); 
       Image image_1  = ImageIO.read(new File("saved.png"));

       lp2_2.setIcon(new ImageIcon(image_1));

   } catch (IOException e) {}
4

4 回答 4

2

希望这会更好,我已经尝试了很多次。

public void writeImage(String output, String fileName, BufferedImage img) throws IOException {
        File file = new File(output + "\\HE\\" + fileName + ".bmp");
        ImageIO.write(img, "bmp", file);
}

==================================================== ================================

如果您想在任何 JPanel 中使用此图像,那么这里是它的代码,它已经可以正常工作了,

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class ShowImage {


    public ShowImage(final String filename) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JFrame editorFrame = new JFrame("My Frame " +filename);
                editorFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);      
                BufferedImage image = null;
                try {
                    image = ImageIO.read(new File(filename));
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
                ImageIcon imageIcon = new ImageIcon(image);
                JLabel jLabel = new JLabel();
                jLabel.setIcon(imageIcon);
                editorFrame.getContentPane().add(jLabel, BorderLayout.CENTER);

                editorFrame.pack();
                editorFrame.setLocationRelativeTo(null);
                editorFrame.setVisible(true);
            }
        });
    }
}
于 2013-07-04T04:49:00.313 回答
2

也许你的转换方式IconImage不对BufferedImage

所以你可以试试下面的代码片段

BufferedImage bi = new BufferedImage(icon.getIconWidth(),icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
// paint the Icon to the BufferedImage.
icon.paintIcon(null, g, 0,0);
g.dispose();

在此之后,您可以BufferdImage像已经使用的那样使用。

或者, 如果您想了解如何转换为“BifferedImage”,您可以查看Java convert Image to BufferedImageImage这个问题,因为如本文所述,您不能只Image转换为BufferedImage.

虽然我会要求您添加更多信息,例如您遇到的错误或异常,如果有异常,请添加堆栈跟踪。

于 2013-07-04T04:54:41.693 回答
1

这是我的新代码,它工作正常......谢谢大家的支持......

尝试{

       BufferedImage cat = ImageIO.read(new File(str));

       for (int w = 0; w < cat.getWidth(); w++) {
       for (int h = 0; h < cat.getHeight(); h++) {
           Color color = new Color(cat.getRGB(w, h));
           //int averageColor = ((color.getRed() + color.getGreen() + color.getBlue()) / 3);
           //int averageColor = int((color.getRed())*0.21 +(color.getGreen())*0.71+(color.getBlue())*0.07);

           double r =color.getRed()*0.21;
           double g =color.getGreen()*0.71;
           double b =color.getBlue()*0.07;
           int averageColor = (int)(r+g+b);

           Color avg = new Color(averageColor, averageColor, averageColor);
           cat.setRGB(w, h, avg.getRGB());
                                               }
                                               }
           ImageIO.write(cat, "jpg", new File("image_greyscale.jpg"));
           lp2_2.setIcon(new ImageIcon((new ImageIcon("image_greyscale.jpg")).getImage().getScaledInstance( 600, 600,  java.awt.Image.SCALE_SMOOTH )));

       }catch(IOException e){
                e.printStackTrace();
                System.exit(1);}
于 2013-07-04T17:00:55.143 回答
0

这里的每个人都没有抓住重点。 ABufferedImage是一个Image

于 2015-05-27T05:21:01.603 回答