2015-03-24 43 views
-1

基本上,我所要做的就是使用JFileChooser從用戶的計算機上打開位圖圖像,然後用一個簡單的過濾器編輯個人像素。我希望能夠像打開它一樣保存圖像,但在此之前,我需要弄清楚如何使用JFileChooser實際打開文件。我的問題是,我設置inputFile等於什麼input.getSelectedFIle()是,這將工作,但它是在一個if語句。這就是說,我該如何解決這個問題,並且在if語句之外使用該文件?另外,是否有一種特殊的方式可以在應用過濾器後保存位圖圖像?Java JFileChooser來選擇位圖圖像

這是我的代碼;它可能包含了一些錯誤,由於我測試的東西來嘗試解決問題:

import java.awt.Component; 
import java.io.*; 
import java.util.Scanner; 

import javax.swing.JFileChooser; 
import javax.swing.JOptionPane; 
import javax.swing.filechooser.FileNameExtensionFilter; 

public class JavaImage 
{ 
private static Component parent; 

public static void main(String[] args) throws IOException 
{ 
    FileInputStream in; 
    //try catch around the image selection 
    try 
    { 
     JFileChooser input = new JFileChooser(); 
     FileNameExtensionFilter filter = new FileNameExtensionFilter(
       "BMP Images", "bmp"); 
     input.setFileFilter(filter); 
     int returnVal = input.showOpenDialog(parent); 
     File inputFile = input.getSelectedFile(); 
     if(returnVal == JFileChooser.APPROVE_OPTION) { 
      inputFile = input.getSelectedFile(); 
      Scanner scan = new Scanner(inputFile); 
     } 
     new FileInputStream(inputFile); 
     FileOutputStream out = new FileOutputStream("output.bmp"); 

     //user selected bitmap image file 
     in=FileInputStream(inputFile); 


    int i = 0; 
    int counter = 0; 

    //edits the actual pixels of the bitmap image 
    while((i==in.read())&&i!=-1) { 
     if (++counter>54) // skip past Bitmap headers 
     { 
      //use BGR format subpixels to process 
      int b =i; //stores blue byte 
      int g = in.read(); //read green bye 
      int r = in.read();//read red byte 

      //greyscale 
      int gray = (r+g+b)/3; 
      b=gray; g=gray; b=gray; 



      out.write(b);// output the blue byte 
      out.write(g);// output the green byte 
      i=r; //prep red byte for output 

     } 
     out.write(i); 
    } 
    in.close(); 
    out.close(); 

} 
//the error message if user enters a non bitmap file 
    catch (IOException ImageError) 
     { 
      JOptionPane.showConfirmDialog(null, "No such file"); 
     } 
} 
} 
+0

爲一體,=的FileInputStream(INPUTFILE)變化';''來in = new FileInputStream(inputFile);' – 2015-03-24 22:01:45

+0

接下來請澄清一下這句話:''我的問題是我設置inputFile等於input.getSelectedFIle()的值,它可以工作,但它在if語句中「'。這是什麼意思?爲什麼不把所有的東西放在if區塊?爲什麼使用掃描儀來顯示一個位圖文件,這個文件顯然不是文本文件。 – 2015-03-24 22:02:21

+0

另外,只有在'returnVal == JFileChooser.APPROVE_OPTION'時調用'input.getSelectedFile()',而不是在測試之前。否則你陷入困境。 – 2015-03-24 22:08:37

回答

1

你種在正確的軌道上,但除非用戶選擇JFileChooser.APPROVE_OPTION,你應該什麼都不做(因爲對話是通過使用取消它)

JFileChooser input = new JFileChooser(); 
FileNameExtensionFilter filter = new FileNameExtensionFilter(
      "BMP Images", "bmp"); 
input.setFileFilter(filter); 
if (input.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) { 
    File inputFile = input.getSelectedFile(); 
    // Process file 
} 

您可以更輕鬆裝入用ImageIO這將允許您訪問到底層的象素數據的圖像關閉。有關更多詳細信息,請參閱Reading/Loading an Image

如果你仍然一意孤行手動處理的文件,那麼你真的應該也看一看The try-with-resources Statement和管理您的資源,更好地