利用Java怎么将不同的图片剪裁成一尺寸的缩略图?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。源码如下:package platfORM.edu.resource.util
利用Java怎么将不同的图片剪裁成一尺寸的缩略图?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
源码如下:
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;public class ImageUtil {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);}}
--结束END--
本文标题: 利用Java怎么将不同的图片剪裁成一尺寸的缩略图
本文链接: https://www.lsjlt.com/news/223220.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0