19

我想显示图像但不知道该怎么做。我是否必须安装一些库文件或者只是可以完成我不知道。其实我想做图像处理,但首先我必须把图像输入和显示图像,然后我才能得到图像处理的效果作为输出,并决定它(算法)是否正确。我只安装了eclipse。我也在谷歌上搜索过,但无论他们建议什么都不好用。要么我必须安装一些东西。

我尝试了以下代码:

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

它只是显示一个图形窗口,但不能显示图像“bishnu.jpg”

我应该在eclipse中安装任何东西吗?但我认为不需要安装任何东西。

4

4 回答 4

21
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);
    }
}
于 2013-09-18T14:15:56.867 回答
13

调整路径后,运行您的代码会为我显示图像。您能否验证您的图像路径是否正确,例如尝试绝对路径?

于 2013-01-16T09:12:42.143 回答
4

如果您想加载/处理/显示图像,我建议您使用图像处理框架。例如,使用Marvin,您只需几行源代码即可轻松完成此操作。

源代码:

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

输出:

在此处输入图像描述

于 2013-10-30T12:19:27.810 回答
0

作为初学者,我发现很容易看到您绘制的图片:

源代码

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

输出

它会在您的项目内容下生成一个图片文件。

输出图片

然后可以看到在小窗口中添加绘制代码后有什么变化,比关闭一个跳出的Frame/Label窗口更方便:

输出编辑器中的图片

希望能帮助到你。

于 2021-04-23T03:40:31.817 回答