public FileEntry getThumbnailFile(FileEntry fileEntry, String fileName, String contentType)
{
try
{
Image image = ImageIO.read(fileEntry.getFile());
// 為等比縮放計算輸出的圖片寬度及高度
double rate1 = ((double) image.getWidth(null)) / (double) outputWidth + 0.1;
double rate2 = ((double) image.getHeight(null)) / (double) outputHeight + 0.1;
// 根據縮放比率大的進行縮放控制
double rate = rate1 > rate2 ? rate1 : rate2;
int newWidth = (int) (((double) image.getWidth(null)) / rate);
int newHeight = (int) (((double) image.getHeight(null)) / rate);
// SCALE_SMOOTH 的縮略演算法 生成縮圖片的平滑度的 優先順序比速度高 生成的圖片質量比較好 但速度慢
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
// 繪製縮小後的圖
tag.getGraphics().drawImage(image, 0, 0, newWidth, newHeight, null);
// File destFile = new File("C:\\temp\\456.jpg");
// FileOutputStream out = new FileOutputStream(destFile); // 輸出到檔案流
UUID fileId = UUID.randomUUID();
File file = this.getFileById(fileId, true);
FileOutputStream out = new FileOutputStream(file);
// 可以正常實現bmp、png、gif轉jpg
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // JPEG編碼
out.close();
return new FileEntry(fileId, file, fileName, contentType);
}
catch (Throwable t)
{
return fileEntry;
}
}