2010-02-04 56 views

回答

0

一般有沒有這樣的東西「縮略圖格式」,而要使用圖片庫來調整圖像上傳到特定的體積小,對其進行壓縮,並保存原件一起。

我不熟悉Java的圖像格式庫,但肯定會有其他人回答了這一點。

1

http://viralpatel.net/blogs/2009/05/20-useful-java-code-snippets-for-java-developers.html

private void createThumbnail(String filename, int thumbWidth, int thumbHeight, int quality, String outFilename) 
02   throws InterruptedException, FileNotFoundException, IOException 
03  { 
04   // load image from filename 
05   Image image = Toolkit.getDefaultToolkit().getImage(filename); 
06   MediaTracker mediaTracker = new MediaTracker(new Container()); 
07   mediaTracker.addImage(image, 0); 
08   mediaTracker.waitForID(0); 
09   // use this to test for errors at this point: System.out.println(mediaTracker.isErrorAny()); 
10 
11   // determine thumbnail size from WIDTH and HEIGHT 
12   double thumbRatio = (double)thumbWidth/(double)thumbHeight; 
13   int imageWidth = image.getWidth(null); 
14   int imageHeight = image.getHeight(null); 
15   double imageRatio = (double)imageWidth/(double)imageHeight; 
16   if (thumbRatio < imageRatio) { 
17    thumbHeight = (int)(thumbWidth/imageRatio); 
18   } else { 
19    thumbWidth = (int)(thumbHeight * imageRatio); 
20   } 
21 
22   // draw original image to thumbnail image object and 
23   // scale it to the new size on-the-fly 
24   BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); 
25   Graphics2D graphics2D = thumbImage.createGraphics(); 
26   graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
27   graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); 
28 
29   // save thumbnail image to outFilename 
30   BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFilename)); 
31   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
32   JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage); 
33   quality = Math.max(0, Math.min(quality, 100)); 
34   param.setQuality((float)quality/100.0f, false); 
35   encoder.setJPEGEncodeParam(param); 
36   encoder.encode(thumbImage); 
37   out.close(); 
38  } 

你需要找出其中所有類從何而來,但它並不像他們使用任何非與JDK捆綁在一起(儘管我可能是錯誤)。

0

遵循以下步驟:

  1. 從上傳商品
  2. 使用ImageIO得到InputStream載入和重新調整圖像(谷歌的「java規模圖像」)
  3. 保存新縮放圖像imagename_thumb.extension
0

你可以看看this。這是一個德國集團,但提供的代碼有英語:-)

+0

由於Tenzen, 我找到的代碼,伯特I M沒有得到我需要填補圖像的字節數組,然後通過一個字節數組爲此功能 – bhard 2010-02-04 14:39:47