📄 previewimage.java
字号:
/*
* Created on 2004-11-14
* Author: Xuefeng, Copyright (C) 2004, Xuefeng.
*/
package org.crystalblog.logic;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.crystalblog.domain.Image;
/**
* Create preview image.
*
* @author Xuefeng
*/
public class PreviewImage {
private static final int IMAGE_SIZE = 120;
private static AffineTransform transform = new AffineTransform();
public static void createPreviewImage(String srcFile, String destFile, String type) {
String imageType = null;
if(type.equals(Image.TYPE_JPG))
imageType = "jpeg";
else if(type.equals(Image.TYPE_GIF))
imageType = "gif";
else if(type.equals(Image.TYPE_PNG))
imageType = "png";
else
throw new RuntimeException("Unsupported image type: " + type);
try {
File fi = new File(srcFile); // src
File fo = new File(destFile); // dest
BufferedImage bis = ImageIO.read(fi);
int w = bis.getWidth();
int h = bis.getHeight();
double scale = (double)w/h;
int nw = IMAGE_SIZE;
int nh = (nw * h) / w;
if( nh>IMAGE_SIZE ) {
nh = IMAGE_SIZE;
nw = (nh * w) / h;
if(nw==0) nw = 1;
}
double sx = (double)nw / w;
double sy = (double)nh / h;
transform.setToScale(sx,sy);
AffineTransformOp ato = new AffineTransformOp(transform, null);
BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis,bid);
ImageIO.write(bid, imageType, fo);
} catch(Exception e) {
e.printStackTrace();
throw new RuntimeException("Failed in create preview image. Error: " + e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -