2013-01-16 97 views
17

我想显示一个图像,但不知道该怎么办。无论我需要安装一些库文件,还是只需要安装它,我都不知道。其实我想做图像处理,但首先我必须把图像输入和显示图像,然后我可以得到图像处理的效果作为输出,并决定它(算法)是否正确。我只安装了eclipse。我也在谷歌搜索,但无论他们建议是不是很好。要么我必须安装或不安装。 我曾尝试下面的代码:在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 <= getHeight();j++) 
       if(i+j>0) g.copyArea(0, 0, imageWidth, imageHeight, i*imageWidth, j*imageHeight); 
    } 

} 

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

我应该安装在eclipse什么?但我认为没有必要安装。

+2

你已经尝试了什么......请,这样,代码... – Lionel

回答

7

运行您的代码后,调整路径后显示一个图像。你可以验证你的图像路径是否正确,例如尝试绝对路径?

+0

非常感谢你我也顾不得图像的完整路径然后工作。但图像与代码位于同一个文件夹中,所以不应该有效(只能通过与代码相同的文件夹中的图像名称)? –

+0

请参阅本主题中的接受答案,以获取有关如何使用classpath加载资源的提示:http://stackoverflow.com/questions/7014123/reading-an-image-in-netbeans/7014177#7014177。 – OlavJ

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

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

输出:

enter image description here

+0

从sourceforge下载marvin1.5.1.jar后,您的示例失败,出现java.io.FileNotFoundException:。\ marvin \ plugins \ image \ org.marvinproject.image.edge.prewitt.jar(系统找不到指定的路径) –

+0

@AlexR:我想你没有正确安装Marvin。您需要将“marvin”文件夹复制到项目的根文件夹中。看看下面的教程,如果问题依然存在,可以联系讨论组中的开发人员。 http://marvinproject.sourceforge.net/en/tutorials/02_firstApplication/firstApplication.html –