-1

I am trying to make image from BufferImage, but it's not working. here is my code ...

This code is not working, can anyone please help me ...

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) {}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
abhilash_goyal
  • 711
  • 1
  • 10
  • 31
  • what is your output or what is the error? – Mayank Tiwari Jul 04 '13 at 04:47
  • 2
    Can you clarify what you mean by 'it's not working'? Is there a compile time error? A run time exception? What is the exception and stack trace and message if so? Is there a logic error? – Patashu Jul 04 '13 at 04:47
  • if you are working in windows then change this line Image image_1 = ImageIO.read(new File("saved.png")); to Image image_1 = ImageIO.read(new File("d:\\saved.png")); and you will get the image, – Mayank Tiwari Jul 04 '13 at 04:50
  • Take a look at [this example](http://stackoverflow.com/questions/13871307/jframe-not-showing-a-picture/13871588#13871588) which loads a image from a file and paints onto a panel. Also take a look at [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) and [2D Graphics](http://docs.oracle.com/javase/tutorial/2d/) – MadProgrammer Jul 04 '13 at 05:17
  • How are you compiling the program ? How can you cast an [Image](http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html) to a [BufferedImage](http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html) using `BufferedImage bimage = (BufferedImage)(new ImageIcon("str")).getImage();`, isn't the compiler stopping you here itself ? Had you tried adding `e.printStackTrace()` to your catch block ? You must be getting some sort of an exception, which you not looking at (something like cannot cast Image to BufferedImage) – nIcE cOw Jul 04 '13 at 12:14
  • Instead you can do like this : `image.createGraphics().drawImage(youImageIcon.getImage(), 0, 0, null);`. Moreover, always try to refer to static fields with class Names like `BufferedImage.TYPE_BYTE_GRAY` instead of using objects like you doing in your code snippet :( – nIcE cOw Jul 04 '13 at 12:18
  • @nIcEcOw: I am not able to get your point sir ... What I want to do is take an image from a JPanel convert it to Gray-scale and then Display it on other JPanel ... – abhilash_goyal Jul 04 '13 at 14:50
  • This [example](http://stackoverflow.com/a/5853992/1057230) by @AndrewThompson, might can help you a bit more on the topic and this [thread](http://stackoverflow.com/q/11272938/1057230), I hope :-) – nIcE cOw Jul 04 '13 at 15:49

4 Answers4

2

Hopefully this will work better, I have tried it many times.

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

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

If you want to use this image in any JPanel then here is code for it, it is already working fine,

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);
            }
        });
    }
}
Mayank Tiwari
  • 2,974
  • 5
  • 30
  • 52
2

Maybe your way of converting IconImage to BufferedImageis not right.

So you can try the following snippet

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

After this you can use BufferdImage as you are already using.

Or you can look this question Java converting Image to BufferedImage if you want to see how to convert Image to 'BifferedImage' because as given in this post you can't just cast Image to BufferedImage.

Although i would request you to add more information like what error or exception you are getting and may be if there is an exception add the stacktrace .

Community
  • 1
  • 1
kaysush
  • 4,797
  • 3
  • 27
  • 47
1

here is my new code and its working properly ... thank you all for your kind support ...

try{

       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);}
abhilash_goyal
  • 711
  • 1
  • 10
  • 31
0

Everyone here missed the point. A BufferedImage is an Image.