2009-04-19 43 views
0

我的老師給了我這個代碼,這應該是一個正在進行的項目的組成部分。事情是,它不起作用。使這個代碼圖像調整大小()工作

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.awt.image.Kernel; 
import java.awt.image.ConvolveOp; 
import com.sun.image.codec.jpeg.JPEGImageEncoder; 
import com.sun.image.codec.jpeg.JPEGCodec; 
import com.sun.image.codec.jpeg.JPEGEncodeParam; 
import javax.swing.*; 


public class ImageUtil { 

    public static void resize(File originalFile, File resizedFile, int newWidth, float quality) throws IOException{ 

     if(quality <0||quality>1){ 
      throw new IllegalArgumentException ("quality has to be between 0 and 1"); 

     } 

     ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath()); 
     Image i = ii.getImage(); 
     Image resizedImage = null; 

     int iWidth = i.getWidth (null); 
     int iHeight = i.getHeight(null); 

     if (iWidth > iHeight){ 
      resizedImage = i.getScaledInstance(newWidth,(newWidth*iHeight)/iWidth, Image.SCALE_SMOOTH); 
     } else { 
      resizedImage = i.getScaledInstance((newWidth*iWidth)/iHeight,newWidth, Image.SCALE_SMOOTH); 
     } 

     Image temp = new ImageIcon (resizedImage).getImage(); 

     BufferedImage bufferedImage = new BufferedImage (temp.getWidth(null), temp.getHeight(null), 
                 BufferedImage.TYPE_INT_RGB); 

     // copy to a buffered image 
     Graphics g = bufferedImage.createGraphics(); 

     g.setColor(Color.white); 
     g.fillRect(0,0,temp.getWidth(null), temp.getHeight(null)); // causea el error de ejecución? 
     g.drawImage(temp,0,0,null); 
     g.dispose(); 

     float softenFactor = 0.05f; 
     float [] softenArray = {0, softenFactor,0, softenFactor, 1-(softenFactor*4), softenFactor,0, softenFactor,0}; 
     Kernel kernel = new Kernel (3,3,softenArray); 
     ConvolveOp cOp = new ConvolveOp (kernel, ConvolveOp.EDGE_NO_OP,null); 
     bufferedImage = cOp.filter (bufferedImage,null); 

     FileOutputStream out = new FileOutputStream(resizedFile); 

     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 

     JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage); 

     param.setQuality(quality,true); 

     encoder.setJPEGEncodeParam(param); 
     encoder.encode(bufferedImage); 



    } 

    public static void main(String args[]) throws IOException { 
     File originalImage= new File ("/images/goya.jpg"); 
     resize(originalImage,new File("/images/goyanuevotamanio.jpg"),350, 0.2f); 
     resize(originalImage, new File("/images/goyanuevotamanio.jpg"), 350, 1f); 

    } 

} 

他說,代碼應該能夠調整高度和寬度但他的大小調整方法:

public static void resize(File originalFile, File resizedFile, int newWidth, float quality) 

缺乏newHeight參數。

多數民衆贊成拋出的RuntimeException的說:

Exception in thread "main" java.lang.IllegalArgumentException: **Width (-1) and height (-1) cannot be <= 0** 
     at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:999) 
     at java.awt.image.BufferedImage.<init>(BufferedImage.java:312) 
     at ImageUtil.resize(ImageUtil.java:38) 
     at ImageUtil.main(ImageUtil.java:72) 
Java Result: 1 

我如何改變呢?

+0

正在使用哪些參數調整大小時被調用? – 2009-04-19 04:17:32

+0

resize(originalImage,new File(「/ images/goyanuevotamanio。jpg「),350,0.2f); – andandandand 2009-04-19 04:24:38

回答

1

如果不分析代碼以查看問題的可能性,我可以說如果意圖是保持圖像縱橫比,則不需要新的高度參數。它將始終是寬度的固定比例。

UPDATE:

我已經運行對我自己的測試圖像的代碼,並預期它確實工作。您收到的錯誤很可能是由於程序無法找到您指定的源圖像而導致的。當我指定不存在的源圖像時,我得到相同的錯誤。

嘗試爲源圖像使用絕對路徑(包括驅動器號等)。

更新2

添加以下代碼行以後。

ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath()); 

if(ii.getImageLoadStatus() == MediaTracker.COMPLETE) 
{ 
    System.out.print("Load image ok\n"); 
} 
else 
{ 
    System.out.print("Load image failed\n"); 
} 

這會告訴你,如果該項目未能加載源圖像。

0

由堆棧跟蹤判斷的異常是由該行拋出:

BufferedImage bufferedImage = new BufferedImage (temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB); 

我認爲這可能是因爲temp.getWidthtemp.getHeight正在返回-1,因爲圖像沒有完成加載。

有一些教程和示例可用於在Web上使用Java中的圖像。一個樣本在這裏http://snippets.dzone.com/posts/show/92。使用ImageObserver查找代碼。

0

你必須給程序一個介於0和1之間的起始參數。 我做到了,它完美地工作。

0

適應這樣的:爲什麼你申請軟化

private void ResizeImage(Image img, double maxWidth, double maxHeight) 
{ 
    double srcWidth = img.Source.Width; 
    double srcHeight = img.Source.Height; 

    double resizeWidth = srcWidth; 
    double resizeHeight = srcHeight; 

    double aspect = resizeWidth/resizeHeight; 

    if (resizeWidth > maxWidth) 
    { 
     resizeWidth = maxWidth; 
     resizeHeight = resizeWidth/aspect; 
    } 
    if (resizeHeight > maxHeight) 
    { 
     aspect = resizeWidth/resizeHeight; 
     resizeHeight = maxHeight; 
     resizeWidth = resizeHeight * aspect; 
    } 

    img.Width = resizeWidth; 
    img.Height = resizeHeight; 
} 
1

調整其大小必須銳化籽粒-1 -2 -1 -2 12 + Q -2 -1 -2 -1,其中一個tipical q可能是28,你是不是新課標下的drawImage的縮放版本

(q = 36,相當於你的F = 0.05銳化)