package net.longjin.comm.utils; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.Random; /** * 描述:ChangeImgSize * * @author 何志鹏 * @ClassName:ChangeImgSize * @create 2019-11-15 9:38 * Version 1.0 */ public class ChangeImgSize { public static boolean changeSize(int newWidth, int newHeight, String path) { BufferedInputStream in = null; try { in = new BufferedInputStream(new FileInputStream(path)); //字节流转图片对象 Image bi = ImageIO.read(in); //构建图片流 BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); //绘制改变尺寸后的图 tag.getGraphics().drawImage(bi, 0, 0, newWidth, newHeight, null); //输出流 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(path)); //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); //encoder.encode(tag); ImageIO.write(tag, "PNG", out); in.close(); out.close(); return true; } catch (IOException e) { return false; } } public static void main(String[] args) { //生成四位随机数 Random ne=new Random(); int num=ne.nextInt(9999-1000+1)+1000; String random = String.valueOf(num); String fileName = "" + System.currentTimeMillis()+random; String newPdfPath = "D:/photo/"+fileName; // changeSize("D:/photo/cc.png", newPdfPath, 1920); changeSize( 1920, 1080, "D:/photo/cc.jpg"); } }
改变图片的大小工具类
猜你喜欢
转载自blog.csdn.net/weixin_39643007/article/details/103090803
今日推荐
周排行