|
目录- 1:使用Thumbnailator
- pom.xml
- 工具类ThumbnailsUtils
- 2:Graphics2D 自带的方法
- 3:前两种我在使用 ImageCombiner 项目的时候 不生效;
- 总结
1:使用Thumbnailator
- Thumbnailator是Java的开源图像大小调整库,它使用渐进式双线性缩放。它支持JPG,BMP,JPEG,WBMP,PNG和GIF。
复制代码
通过将以下Maven依赖项添加到我们的pom.xml中,将其包括在我们的项目中:
pom.xml
- <dependency>
- <groupId>net.coobird</groupId>
- <artifactId>thumbnailator</artifactId>
- <version>0.4.11</version>
- </dependency>
复制代码
工具类ThumbnailsUtils
- import net.coobird.thumbnailator.Thumbnails;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
-
- public class ThumbnailsUtils{
- private static final Logger logger = LoggerFactory.getLogger(ThumbnailsUtils.class);
-
- /**
- * 通过BufferedImage图片流调整图片大小
- */
- public static BufferedImage resizeImageOne(BufferedImage originalImage, int targetWidth, int targetHeight) throws Exception {
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- Thumbnails.of(originalImage)
- .size(targetWidth, targetHeight)
- .outputFormat("JPEG")
- .outputQuality(1)
- .toOutputStream(outputStream);
- byte[] data = outputStream.toByteArray();
- ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
- return ImageIO.read(inputStream);
- }
-
- /**
- * BufferedImage图片流转byte[]数组
- */
- public static byte[] imageToBytes(BufferedImage bImage) {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- try {
- ImageIO.write(bImage, "jpg", out);
- } catch (IOException e) {
- logger.error("错误信息: ", e);
- }
- return out.toByteArray();
- }
-
- /**
- * byte[]数组转BufferedImage图片流
- */
- private static BufferedImage bytesToBufferedImage(byte[] ImageByte) {
- ByteArrayInputStream in = new ByteArrayInputStream(ImageByte);
- BufferedImage image = null;
- try {
- image = ImageIO.read(in);
- } catch (IOException e) {
- logger.error("错误信息: ", e);
- }
- return image;
- }
- }
复制代码
2:Graphics2D 自带的方法
- public static BufferedImage scaleImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
- int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
- BufferedImage scaledImage = new BufferedImage(targetWidth, targetHeight, type);
- Graphics2D g = scaledImage.createGraphics();
- // Calculate the ratio between the original and scaled image size
- double scaleX = (double) targetWidth / originalImage.getWidth();
- double scaleY = (double) targetHeight / originalImage.getHeight();
- double scale = Math.min(scaleX, scaleY);
- // Now we perform the actual scaling
- int newWidth = (int) (originalImage.getWidth() * scale);
- int newHeight = (int) (originalImage.getHeight() * scale);
- int x = (targetWidth - newWidth) / 2;
- int y = (targetHeight - newHeight) / 2;
- g.drawImage(originalImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), x, y, null);
- g.dispose();
- return scaledImage;
- }
复制代码- public static void main(String[] args) throws Exception {
- // 读取原始图片
- File originalFile = new File("D:\\test1\\schoolLogo.png");
- BufferedImage originalImage = ImageIO.read(originalFile);
-
- // 设定目标宽高
- int targetWidth = 1000; // 两倍放大
- int targetHeight = 1000; // 两倍放大
-
- // 调用 resizeImageOne 方法进行放大
- BufferedImage resizedImage = ThumbnailsUtils.scaleImage(originalImage, targetWidth, targetHeight);
-
- // 将放大后的图片保存到文件
- File outputFile = new File("D:\\test1\\big.png");
- ImageIO.write(resizedImage, "png", outputFile);
- }
复制代码
3:前两种我在使用 ImageCombiner 项目的时候 不生效;
- // 创建新的 BufferedImage,并设置绘制质量
- BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
- Graphics2D g2d = resizedImage.createGraphics();
- g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
-
- // 绘制原始图像到新的 BufferedImage,并进行缩放
- Image scaledImage = image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
- g2d.drawImage(scaledImage, 0, 0, null);
- g2d.dispose();
-
- // 保存新的图片
- String outputImagePath = "C:\\Users\\up1.jpg"; // 替换为实际的输出路径
- ImageIO.write(resizedImage, "jpg", new File(outputImagePath));
-
- System.out.println("图片尺寸调整完成!");
复制代码
总结
到此这篇关于Java调整图片大小的3种方式小结的文章就介绍到这了,更多相关Java调整图片大小内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯! 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:  进行删除处理。
4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
|