2013-10-20 13 views
0

我有以下代碼中的問題,以調整圖像的大小(JPG-文件):0字節以JPG-圖像尺寸調整程序文件在JAVA

import java.awt.AlphaComposite; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.util.logging.Logger; 
import java.util.logging.Level; 
import java.io.IOException; 
import javax.imageio.ImageIO; 

public class ResizeImage { 


    /** 
    * 
    * @param sourceImg 
    *   The source of the image to resize. 
    * @param Width 
    *   The maximum width you want the new image to be, use 0 for 
    *   source width. 
    * @param Height 
    *   The maximum height you want the new image to be, use 0 for 
    *   source height. 
    * @return true if successful and false if unsuccessful. 
    */ 

    public static String resizeImage(String sourceImg, Integer Width, 
      Integer Height) { 

     BufferedImage origImage; 
     BufferedImage resizedImage = null; 
     // String tDir = System.getProperty("java.io.tmpdir"); 
     String imagePath; 

     try { 

      File resizedFile = File.createTempFile("ResizedImmoIMG", ".jpg"); 
      imagePath = resizedFile.getAbsolutePath(); 
      origImage = ImageIO.read(new File(sourceImg)); 
      int type = origImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB 
        : origImage.getType(); 

      // *Special* if the width or height is 0 use image src dimensions 
      if (Width == 0) { 
       Width = origImage.getWidth(); 
      } 
      if (Height == 0) { 
       Height = origImage.getHeight(); 
      } 

      int fHeight = Height; 
      int fWidth = Width; 

      // Work out the resized width/height 
      if (origImage.getHeight() > Height || origImage.getWidth() > Width) { 
       fHeight = Height; 
       int wid = Width; 
       float sum = (float) origImage.getWidth() 
         /(float) origImage.getHeight(); 
       fWidth = Math.round(fHeight * sum); 

       if (fWidth > wid) { 
        // rezise again for the width this time 
        fHeight = Math.round(wid/sum); 
        fWidth = wid; 
       } 
      } 

      resizedImage = new BufferedImage(fWidth, fHeight, type); 

      Graphics2D g2d = resizedImage.createGraphics(); 
      g2d.setComposite(AlphaComposite.Src); 

      g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
        RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
      g2d.setRenderingHint(RenderingHints.KEY_RENDERING, 
        RenderingHints.VALUE_RENDER_QUALITY); 
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
        RenderingHints.VALUE_ANTIALIAS_ON); 

      g2d.drawImage(origImage, 0, 0, fWidth, fHeight, null); 
      g2d.dispose(); 

      ImageIO.write(resizedImage, ".jpg", resizedFile); 
      System.out.println(imagePath); 

     } catch (IOException ex) { 
      Logger.getLogger(ResizeImage.class.getName()).log(Level.SEVERE, 
        null, ex); 
      return null; 
     } 
     return imagePath; 
    } 

    } 

程序應該調整所述源圖像,並保存它在系統臨時目錄中。但我得到的只是臨時目錄中的一個0 Bytes JPG文件。我看不到的地方,我做了錯誤的:(

有誰請給我一些建議或提示嗎?

迎接

THE-E

回答

2

對於ImageIO的第二個參數.write方法,你必須傳遞一個不是文件擴展名的圖像格式,只需用「jpg」而不是「.jpg」來嘗試。

Writing/Saving an Image