用Java显示图像

我想显示图像,但不知道该怎么做。 我是否必须安装一些库文件或者只是它可以完成我不知道。 实际上我想做图像处理,但首先我必须输入图像并显示图像,然后我可以将图像处理的效果作为输出,并决定它(算法)是否正确。 我只安装了日食。 我也在谷歌搜索过,但无论他们建议什么都运作不好。 要么我必须安装或不安装。 我试过以下代码:

import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JComponent; import javax.swing.JFrame; 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); } 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 0) g.copyArea(0, 0, imageWidth, imageHeight, i*imageWidth, j*imageHeight); } } 

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

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

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

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

如果您想加载/处理/显示图像,我建议您使用图像处理框架。 例如,使用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); } } 

输出:

在此处输入图像描述