图片像素降低
package com.example.demo.线程池.截屏; import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class 图片像素降低 { public static File run(String in, float 清晰程度, String 图片尾缀) throws IOException { File file=new File(in); BufferedImage src = null; FileOutputStream out = null; ImageWriter imgWrier; ImageWriteParam imgWriteParams; //System.out.println("开始设定压缩图片参数"); // 指定写图片的方式为 jpg imgWrier = ImageIO.getImageWritersByFormatName(图片尾缀).next(); imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam( null); // 要使用压缩,必须指定压缩方式为MODE_EXPLICIT imgWriteParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // 这里指定压缩的程度,参数qality是取值0~1范围内, imgWriteParams.setCompressionQuality(清晰程度); imgWriteParams.setProgressiveMode(ImageWriteParam.MODE_DISABLED); ColorModel colorModel = ImageIO.read(file).getColorModel();// ColorModel.getRGBdefault(); imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier( colorModel, colorModel.createCompatibleSampleModel(32, 32))); //System.out.println("结束设定压缩图片参数"); if (!file.exists()) { System.out.println("Not Found Img File,文件不存在"); throw new FileNotFoundException("Not Found Img File,文件不存在"); } else { System.out.println("图片转换前大小" + file.length() + "字节"); src = ImageIO.read(file); out = new FileOutputStream(file); imgWrier.reset(); // 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何 // OutputStream构造 imgWrier.setOutput(ImageIO.createImageOutputStream(out)); // 调用write方法,就可以向输入流写图片 imgWrier.write(null, new IIOImage(src, null, null), imgWriteParams); out.flush(); out.close(); System.out.println("图片转换后大小" + file.length() + "字节"); return file; } } }
图片分辨率降低
package com.example.demo.线程池.截屏; import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; public class 图片分辨率降低 { public static void run(String 绝对路径, int w, int h) throws Exception { String dest=绝对路径; double wr=0,hr=0; File srcFile = new File(绝对路径); File destFile = new File(dest); BufferedImage bufImg = ImageIO.read(srcFile); //读取图片 Image Itemp = bufImg.getScaledInstance(w, h, bufImg.SCALE_SMOOTH);//设置缩放目标图片模板 wr=w*1.0/bufImg.getWidth(); //获取缩放比例 hr=h*1.0 / bufImg.getHeight(); AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null); Itemp = ato.filter(bufImg, null); try { ImageIO.write((BufferedImage) Itemp,dest.substring(dest.lastIndexOf(".")+1), destFile); //写入缩减后的图片 } catch (Exception ex) { ex.printStackTrace(); } } }
结合截图的使用范例
String 项目根目录 = new File("").getCanonicalPath() ; // ↓这里电脑屏幕缩放比例是150% 就填1.5 截屏 scan=截屏.run(项目根目录+"\src\main\java\com\example\demo\线程池\截屏\","jpg",1.5); //降低分辨率 ↓这里是图片的绝对路径,就是上面第一个run的第一个参数 图片分辨率降低.run(scan.文件地址.getPath(), (int) (scan.图片长*0.7) , (int)(scan.图片宽*0.7) ); //降低画质,压缩 取值0~1范围内 清晰程度 数值越小分辨率越低 图片像素降低.run(scan.文件地址.getPath(),(float) 0.1,scan.图片尾缀);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)