如何使用java代码调整图片大小呢?
下文笔者讲述使用java代码调整图片大小的方法分享,如下所示
今天接到一个需求,需要将用户上传的图片调整为指定大小,那么java代码如何实现这一需求呢?
下文笔者将一一道来,如下所示
今天接到一个需求,需要将用户上传的图片调整为指定大小,那么java代码如何实现这一需求呢?
下文笔者将一一道来,如下所示
实现思路: 使用Graphics2D类中的指定方法,即可调整图片的大小 BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null); g.dispose(); BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null); g.dispose(); g.setComposite(AlphaComposite.Src); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);例:调整图片大小的示例分享
import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; /* * @author java265.com * */ public class ImageTest { private static final int IMG_WIDTH = 100; private static final int IMG_HEIGHT = 100; public static void main(String [] args){ try{ BufferedImage originalImage = ImageIO.read(new File("D:\\test\\test20230127.jpg")); int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType(); BufferedImage resizeImageJpg = resizeImage(originalImage, type); ImageIO.write(resizeImageJpg, "jpg", new File("D:\\test\\test20230127_jpg.jpg")); BufferedImage resizeImagePng = resizeImage(originalImage, type); ImageIO.write(resizeImagePng, "png", new File("D:\\test\\test20230127_png.jpg")); BufferedImage resizeImageHintJpg = resizeImageWithHint(originalImage, type); ImageIO.write(resizeImageHintJpg, "jpg", new File("D:\\test\\test20230127_hint_jpg.jpg")); BufferedImage resizeImageHintPng = resizeImageWithHint(originalImage, type); ImageIO.write(resizeImageHintPng, "png", new File("D:\\test\\test20230127_hint_png.jpg")); }catch(IOException e){ System.out.println(e.getMessage()); } } private static BufferedImage resizeImage(BufferedImage originalImage, int type){ BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null); g.dispose(); return resizedImage; } private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type){ BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null); g.dispose(); g.setComposite(AlphaComposite.Src); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); return resizedImage; } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。