2013-08-22 184 views
1

嗨我想獲得BufferedImage的路徑,但如何獲得加載的圖像我不知道的路徑。 我正在從Stack <>中提取圖像>。當用戶點擊下一個按鈕時從其中取出一個一個的圖像。 使用pop()方法堆棧更改圖像。如何獲得具體bufferedimage的路徑?

代碼:

 Stack<File> pictures ; 
     final JFileChooser file; 
     file = new JFileChooser(); 
     file.setCurrentDirectory(dir); 
     file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
     file.showOpenDialog(panel); 
     String path = file.getSelectedFile().getAbsolutePath(); 
     System.out.println(path); 
     pictures= getFilesInFolder(path.toString());  


     a=ImageIO.read(pictures.pop().getAbsoluteFile()); 

這裏a緩衝Image實例。 現在我想要在a加載的圖像的整個路徑。

有人指導我嗎?

+0

哈瓦一文件到堆棧或BufferedImage? – Gerret

+0

是的,我已經添加了這個概念,你教給我。 –

+0

在使用pop之前,只需使用stack.peek()。偷看向您展示堆棧的第一張圖片。所以:'stack.peek()。getAbsuluthPath();'或不? – Gerret

回答

2

A BufferedImage不保留關於如何生成或從何處加載的信息。你必須存儲在加載圖像之前在一個變量的文件路徑:

File file = pictures.pop().getAbsoluteFile(); 
    a=ImageIO.read(file); 
    // now you can use "file" for other purposes too 
+0

假設我想存儲那個字符串變量的路徑,那麼我應該怎麼做? –

+1

@ user2659972或者像這樣做。現在你可以使用file.getAbsolutePath(); – Gerret

+1

'String path = file.getPath();'http://docs.oracle.com/javase/7/docs/api/java/io/File.html#getPath%28%29 – Joni

2

的問題是,你是,你不能從一個BufferedImage讀取路徑,你有你做之前,使用的文件它的一個BufferedImage。

所以,你可以使用:

String path = stack.peek().getPath(); 

現在你已經救了你的路徑。目前您將其轉換爲BufferedImage,使用pop(),以便將其從堆棧中移除。使用peek()您只能看第一個項目而不刪除。或者您將文件保存到臨時文件像

File temp = stack.pop(); 

比你能夠使用:

temp.getPath(); 
+0

先生,我已經這樣做了,我給你發送了一個文件來縮放圖像,但你沒有回覆。我的整個應用程序準備就緒,我只需要縮放圖像,但我沒有得到它。 –

+1

它工作!? ^^是的,可能是我很抱歉,但是我給你的電子郵件實際上是一封假郵件,或者更好地說一封郵件來保存我的身份。我不看這個,所以我沒有看到它。 – Gerret

+1

@ user2659972我將在stackoverflow上看看它。如果我知道一個解決方案,我會幫你:) – Gerret

0

是的,我已經解決了,使用下面的代碼:你增加

  String p; 
      File f; 
      try 
      { 
       f= pictures.pop().getAbsoluteFile(); 
       a=ImageIO.read(f); 
       p = f.getPath(); 
       System.out.println(p); 


      } 
      catch (IOException e1) 
      { 
       e1.printStackTrace(); 
      }