以下是生成缩略图的代码,生成GIF的缩略图没有动画效果,有知道人帮忙看下!用的是gif4j.jar
public static void createFixedBoundImg(String OriFilePath, String TargetFilePath, int height, int width)
throws Exception {
double Ratio = 0.0;
File f = new File(OriFilePath);
Image src = ImageIO.read(f);
int oriWidth = src.getWidth(null);
int oriHeight = src.getHeight(null);
int tagWidth, tagHeight;
if (oriWidth > width || oriHeight > height) {
if (oriHeight > oriWidth) {
Ratio = (new Integer(height)).doubleValue() / oriHeight;
tagHeight = height;
tagWidth = (int) (oriWidth * Ratio);
}
else {
Ratio = (new Integer(width)).doubleValue() / oriWidth;
tagHeight = (int) (oriHeight * Ratio);
tagWidth = width;
}
}
else {
tagHeight = oriHeight;
tagWidth = oriWidth;
}
BufferedImage target = new BufferedImage(tagWidth, tagHeight, BufferedImage.TYPE_INT_RGB);
target.getGraphics().drawImage(src, 0, 0, tagWidth, tagHeight, null);
FileOutputStream out = new FileOutputStream(TargetFilePath);
//JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//encoder.encode(target);
GifEncoder.encode(target, out);
out.close();
}
public static void createFixedBoundImg(String OriFilePath, String TargetFilePath, int height, int width)
throws Exception {
double Ratio = 0.0;
File f = new File(OriFilePath);
Image src = ImageIO.read(f);
int oriWidth = src.getWidth(null);
int oriHeight = src.getHeight(null);
int tagWidth, tagHeight;
if (oriWidth > width || oriHeight > height) {
if (oriHeight > oriWidth) {
Ratio = (new Integer(height)).doubleValue() / oriHeight;
tagHeight = height;
tagWidth = (int) (oriWidth * Ratio);
}
else {
Ratio = (new Integer(width)).doubleValue() / oriWidth;
tagHeight = (int) (oriHeight * Ratio);
tagWidth = width;
}
}
else {
tagHeight = oriHeight;
tagWidth = oriWidth;
}
BufferedImage target = new BufferedImage(tagWidth, tagHeight, BufferedImage.TYPE_INT_RGB);
target.getGraphics().drawImage(src, 0, 0, tagWidth, tagHeight, null);
FileOutputStream out = new FileOutputStream(TargetFilePath);
//JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//encoder.encode(target);
GifEncoder.encode(target, out);
out.close();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货