19

I want to display an image but don't know what to do. Whether I have to install some library files or simply it can be done I don't know. Actually I want to do image processing, but first I have to take the image input and display image then I can get the effect of image processing as the output and decide whether it(algorithm) is correct or not. I have installed the eclipse only. I have searched in Google also but whatever they suggest is not working well. Either I have to install something or not.

I have tried the following code:

public class ImageTest {
    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable() {
            public void run(){
                ImageFrame frame = new ImageFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        }
        );
    }
}

class ImageFrame extends JFrame{
    public ImageFrame(){
        setTitle("ImageTest");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        ImageComponent component = new ImageComponent();
        add(component);
        getContentPane().validate();
        getContentPane().repaint();
    }

    public static final int DEFAULT_WIDTH = 300;
    public static final int DEFAULT_HEIGHT = 200;
}

class ImageComponent extends JComponent{
    private static final long serialVersionUID = 1L;
    private Image image;
    public ImageComponent(){
        try{
            File image2 = new File("bishnu.jpg");
            image = ImageIO.read(image2);
        } catch (IOException e){
            e.printStackTrace();
        }
    }
    public void paintComponent (Graphics g){
        if(image == null) return;
        int imageWidth = image.getWidth(this);
        int imageHeight = image.getHeight(this);

        g.drawImage(image, 50, 50, this);

        for (int i = 0; i*imageWidth <= getWidth(); i++)
            for(int j = 0; j*imageHeight <= getHeight();j++)
                if(i+j>0) g.copyArea(0, 0, imageWidth, imageHeight, i*imageWidth, j*imageHeight);
    }
}

It simply shows a graphical window but can't show the image "bishnu.jpg"

Should I install anything in eclipse? But I think nothing needs to install.

Gray
  • 115,027
  • 24
  • 293
  • 354
Bishnu Bhattarai
  • 2,800
  • 8
  • 36
  • 44

4 Answers4

24
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

public class DisplayImage {

    public static void main(String avg[]) throws IOException
    {
        DisplayImage abc=new DisplayImage();
    }

    public DisplayImage() throws IOException
    {
        BufferedImage img=ImageIO.read(new File("f://images.jpg"));
        ImageIcon icon=new ImageIcon(img);
        JFrame frame=new JFrame();
        frame.setLayout(new FlowLayout());
        frame.setSize(200,300);
        JLabel lbl=new JLabel();
        lbl.setIcon(icon);
        frame.add(lbl);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Abdul Rasheed
  • 241
  • 2
  • 2
13

Running your code shows an image for me, after adjusting the path. Can you verify that your image path is correct, try absolute path for instance?

OlavJ
  • 365
  • 4
  • 9
  • Thank you very much I also gave the full path of the image then it work. But the image is in same folder as the code is, so shouldn't it work (only by the name of image within the same folder as code is)? – Bishnu Bhattarai Jan 16 '13 at 09:49
  • 1
    See the accepted answer in this thread for tips on how to load resources using classpath: http://stackoverflow.com/questions/7014123/reading-an-image-in-netbeans/7014177#7014177. – OlavJ Jan 16 '13 at 09:58
5

If you want to load/process/display images I suggest you use an image processing framework. Using Marvin, for instance, you can do that easily with just a few lines of source code.

Source code:

public class Example extends JFrame{

    MarvinImagePlugin prewitt           = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.edge.prewitt");
    MarvinImagePlugin errorDiffusion    = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.halftone.errorDiffusion");
    MarvinImagePlugin emboss            = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.emboss");

    public Example(){
        super("Example");

        // Layout
        setLayout(new GridLayout(2,2));

        // Load images
        MarvinImage img1 = MarvinImageIO.loadImage("./res/car.jpg");
        MarvinImage img2 = new MarvinImage(img1.getWidth(), img1.getHeight());
        MarvinImage img3 = new MarvinImage(img1.getWidth(), img1.getHeight());
        MarvinImage img4 = new MarvinImage(img1.getWidth(), img1.getHeight());

        // Image Processing plug-ins
        errorDiffusion.process(img1, img2);
        prewitt.process(img1, img3);
        emboss.process(img1, img4);

        // Set panels
        addPanel(img1);
        addPanel(img2);
        addPanel(img3);
        addPanel(img4);

        setSize(560,380);
        setVisible(true);
    }

    public void addPanel(MarvinImage image){
        MarvinImagePanel imagePanel = new MarvinImagePanel();
        imagePanel.setImage(image);
        add(imagePanel);
    }

    public static void main(String[] args) {
        new Example().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Output:

enter image description here

Gabriel Archanjo
  • 4,547
  • 2
  • 34
  • 41
  • After downloading marvin1.5.1.jar from sourceforge, your example fails with java.io.FileNotFoundException: .\marvin\plugins\image\org.marvinproject.image.edge.prewitt.jar (The system cannot find the path specified) – Alex R Dec 22 '15 at 01:29
  • @AlexR: I guess you did not install Marvin properly. You need to copy "marvin" folder to the root folder of your project. Take a look at the following tutorial, if the problem persist, you can reach developers in the discussion group. http://marvinproject.sourceforge.net/en/tutorials/02_firstApplication/firstApplication.html – Gabriel Archanjo Dec 22 '15 at 02:14
  • Requiring the user to download an extra package diminishes the value of the the answer. Rasheed's answer used all standard jars. – user1062589 Sep 04 '18 at 18:16
0

As a beginer, I found that is easy to see the picture you draw:

Source code

public class CheckCodeTest  {

    private int width = 100, height = 50;
    private BufferedImage image = new BufferedImage(width, height,
                                      BufferedImage.TYPE_INT_RGB);

    @Test
    public void drawGraphicsTest() throws IOException {

        Graphics graphics = image.createGraphics();

        // draw an orange rectangle
        graphics.setColor(Color.orange);
        graphics.fillRect(0,0,width,height);

        // layout the picture right now!
        graphics.drawImage(image,0,0,null);
        ImageIO.write(image, "png", new File("checkcode.png"));
    }
}

Output

It produce a picture file under your projects content.

output-picture

Then you can see what change after adding draw code in small window, it is more convenient than closing an jump-out Frame / Label window:

output-picture-in-editor

Hope it helps.

Miozus
  • 1
  • 1