2013-01-16 92 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 –