网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

利用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怎么将不同的图片剪裁成一尺寸的缩略图
标题URL:http://bjjierui.cn/article/pgjghg.html

其他资讯