2011-04-29 241 views
0

我需要一個邏輯來重新調整圖像的大小,使其始終爲320X240或更小,並且應保持寬高比。將圖像調整爲固定大小

+0

可能重複[調整Java中的圖像 - 任何開源庫](http://stackoverflow.com/questions/244164/resize-an-image-in-java-any-open-source -library) – 2011-04-29 21:55:32

+0

這有什麼具體的部分是你有麻煩嗎? – 2011-04-29 21:56:30

回答

0
1) determine which side of the image is the larger one 
2) if its width, calc: faktor = 320/width, else faktor = 240/height; 
3) downscale the image by size.x *faktor, size.y *faktor. 

例如:

考慮:圖像具有1024x640

1) its wider than high. 
2) faktor = 320/1024 -> 0.3125 
3) 1024 * 0.3125 = 320 (huh!)  
    640 * 0.3125 = 200. 
0

看一看ImgScalr,圖書館爲Java。有幾個質量和速度設置用於轉換,並自動縮放它。的

BufferedImage sourceImage = ImageIO.read(new File(sourceFile)); 
BufferedImage destImage = Scalr.resize(sourceImage, Scalr.Method.ULTRA_QUALITY, 320, 240, Scalr.OP_ANTIALIAS);