189 8069 5689

利用Java怎么将不同的图片剪裁成一尺寸的缩略图

利用Java怎么将不同的图片剪裁成一尺寸的缩略图?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

成都创新互联公司是专业的榆中网站建设公司,榆中接单;提供成都网站设计、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行榆中网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

源码如下:

package platform.edu.resource.utils;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
 * 图片工具类
 * @author hjn
 * @version 1.0 2013-11-26
 *
 */
public class ImageUtil {
/**
 * 图片等比缩放居中剪裁
 * 不管尺寸不等的图片生成的缩略图都是同一尺寸,方便用于页面展示
 * @param imageSrc图片所在路径
 * @param thumbWidth缩略图宽度
 * @param thumbHeight缩略图长度
 * @param outFilePath缩略图存放路径
 * @throws InterruptedException
 * @throws IOException
 */
public static void createImgThumbnail(String imgSrc, int thumbWidth, int thumbHeight, String outFilePath) throws InterruptedException, IOException {
File imageFile=new File(imgSrc);
BufferedImage image = ImageIO.read(imageFile);
Integer width = image.getWidth();
Integer height = image.getHeight();
double i = (double) width / (double) height;
double j = (double) thumbWidth / (double) thumbHeight;
int[] d = new int[2];
int x = 0;
int y = 0;
if (i > j) {
d[1] = thumbHeight;
d[0] = (int) (thumbHeight * i);
y = 0;
x = (d[0] - thumbWidth) / 2;
} else {
d[0] = thumbWidth;
d[1] = (int) (thumbWidth / i);
x = 0;
y = (d[1] - thumbHeight) / 2;
}
File outFile = new File(outFilePath);
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
/*等比例缩放*/
BufferedImage newImage = new BufferedImage(d[0],d[1],image.getType());
    Graphics g = newImage.getGraphics();
    g.drawImage(image, 0,0,d[0],d[1],null);
    g.dispose();
    /*居中剪裁*/
newImage = newImage.getSubimage(x, y, thumbWidth, thumbHeight);
ImageIO.write(newImage, imageFile.getName().substring(imageFile.getName().lastIndexOf(".") + 1), outFile);
}
}

关于利用Java怎么将不同的图片剪裁成一尺寸的缩略图问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


新闻标题:利用Java怎么将不同的图片剪裁成一尺寸的缩略图
当前地址:http://cdxtjz.cn/article/pgjghg.html

其他资讯