2014-04-04 55 views
0

UI出錯,無法傳遞2個文本框中的數據或用戶在單擊按鈕以壓縮圖像時選擇的路徑。圖像壓縮應用程序

下面是代碼:

// this is the button when clicked should compress the image 
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {    
     File imageFile = new File("myimage.jpg"); 
     File compressedImageFile = new File("myimage_compressed.jpg"); 

     try 
     { 
      InputStream is = new FileInputStream(imageFile); 
      OutputStream os = new FileOutputStream(compressedImageFile); 
      float quality = 0.5f; 

      // create a BufferedImage as the result of decoding the supplied InputStream 
      BufferedImage image = ImageIO.read(is); 

      // get all image writers for JPG format 
      Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); 

      if (!writers.hasNext()) 
       throw new IllegalStateException("No writers found"); 
      ImageWriter writer = (ImageWriter) writers.next(); 
      ImageOutputStream ios = ImageIO.createImageOutputStream(os); 
      writer.setOutput(ios); 

      ImageWriteParam param = writer.getDefaultWriteParam(); 

      // compress to a given quality 
      param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 
      param.setCompressionQuality(quality); 

      // appends a complete image stream containing a single image and 
      //associated stream and image metadata and thumbnails to the output 
      writer.write(null, new IIOImage(image, null, null), param); 

      // close all streams 
      is.close(); 
      os.close(); 
      ios.close(); 
      writer.dispose(); 
    } 
    catch(IOException e) 
    { 
     System.out.println(e); 
    } 

} 


// this is the button for choosing an image file 
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) { 
    JFileChooser chooser=new JFileChooser(); 
    FileNameExtensionFilter imgfilter=new FileNameExtensionFilter("Image files", "jpg"); 

    chooser.setFileFilter(imgfilter); 
    chooser.showOpenDialog(null); 

    File f=chooser.getSelectedFile(); 
    String filename=f.getAbsolutePath(); 

    jTextField1.setText(filename); 
} 



// this is the button for choosing the destination folder for compressed image to be saved          

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {           
    JFileChooser chooser=new JFileChooser(); 
    FileNameExtensionFilter imgfilter=new FileNameExtensionFilter("Image files", "jpg"); 
    chooser.setFileFilter(imgfilter); 
    chooser.showOpenDialog(null); 
    File f=chooser.getSelectedFile(); 
    String filename=f.getAbsolutePath(); 
    jTextField1.setText(filename); 
} 

在此我有兩個文本框和兩個按鈕一個按鈕,用於選擇從M/C和其他按鈕的圖像用於選擇其中圖像必須是目標文件夾保存。

第三個按鈕用於壓縮圖像文件。

+0

請包括(的一部分)你的代碼,並具體說出什麼地方出了問題。否則,我們很難幫助你。 – Chronio

+0

你說的是2個按鈕,並解釋了3個按鈕的使用 – KiKMak

+0

我很抱歉,但我的意思是選擇位置和保存位置的第二個按鈕和用於壓縮img文件的第三個按鈕 – user3402356

回答

0

你在第

// this is the button for choosing an image file 

,並在部分

// this is the button for choosing the destination folder for compressed image to be saved          

代碼是完全一樣的,jButton6應該是變化和jtextfield1應該根據自己的UI改變。

它會解決我猜的問題。

好運

+0

File imageFile = new File(「myimage.jpg」); File compressedImageFile = new File(「myimage_compressed.jpg」); 這裏是要傳遞用戶選擇的路徑,方法是單擊圖像存在的源文件夾按鈕以及新圖像的保存位置 – user3402356