Java图片批量压缩像素的实现方法是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
为了防止用户流量的丢失,即使在5g 即将来临的情况下,压缩算法依旧是很有必要的,额跑题了,不好意思,今天介绍的不是压缩算法,讲啥呢?主要讲讲如何通过 java 将图片进行压缩,尽可能的控制压缩损比,不仅仅是为了减少存储,其目的是快速呈现给用户,只有良好的体验,才会在当今这个急躁的年代减少流量的损失。
最近因为公司要需要xxx认证上传测试用例功能的具体截图、发现有大小限制、所以就进行了图片压缩,简单记录一下。
main方法测试:
public static void main(String[] args) throws IOException {
String modpath = "C:\\Users\\Administrator\\Desktop\\鲲鹏认证\\test\\";
getFiles("C:\\Users\\Administrator\\Desktop\\鲲鹏认证\\测试用例清单", modpath, 160);//将图片压缩至100宽
}
文件大小处理
/**
* @param srcPath 原图片路径
* @param desPath 转换大小后图片路径
* @param width 转换后图片宽度
* @param height 转换后图片高度
*/
public static void resizeImage(String srcPath, String desPath, int width, int height) throws IOException {
File srcFile = new File(srcPath);
Image srcImg = ImageIO.read(srcFile);
BufferedImage buffImg = null;
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
//使用TYPE_INT_RGB修改的图片会变色
buffImg.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
String filePath="";
if (srcFile.getName().contains("#")) {
filePath = srcFile.getName().replace("#", "");
}else{
filePath=srcFile.getName();
}
ImageIO.write(buffImg, "PNG", new File(desPath + filePath));
}
获取目录文件信息
/**
* @param scaleSize 图片的修改比例,目标宽度
*/
public static void getFiles(String path, String modPath, int scaleSize) throws IOException {
ArrayList<String> files = new ArrayList<String>();
File file = new File(path);
File[] tempList = file.listFiles();
//循环读取目录下图片
for (int i = 0; i < tempList.length; i++) {
String filePath = tempList[i].getName();
if (tempList[i].isFile()) {
System.out.println("文件:" + filePath + "-" + tempList[i].getAbsolutePath().replaceAll("\\\\", "/"));
String[] imagePath = tempList[i].getAbsolutePath().replaceAll("\\\\", "/").split("/");
String imageNumber = null;
FileUtil.resizeImage(tempList[i].getAbsolutePath().replaceAll("\\\\", "/"), modPath, 160, 160);
files.add(tempList[i].toString());
}
if (tempList[i].isDirectory()) {
System.out.println("文件夹:" + tempList[i]);
}
}
System.out.println(path + "下文件数量:" + files.size());
}
控制台目录压缩成功保存到盘符:
Graphics类提供基本绘图方法,Graphics类提供基本的几何图形绘制方法,主要有:画线段、画矩形、画圆、画带颜色的图形、画椭圆、画圆弧、画多边形、画字符串等。 这里不做一一赘述, 进重点介绍一下,利用Graphics类如何进行压缩图像。不多说直接上代码。
/**
* compressImage
*
* @param imageByte
* Image source array
* @param ppi
* @return
*/
public static byte[] compressImage(byte[] imageByte, int ppi) {
byte[] smallImage = null;
int width = 0, height = 0;
if (imageByte == null)
return null;
ByteArrayInputStream byteInput = new ByteArrayInputStream(imageByte);
try {
Image image = ImageIO.read(byteInput);
int w = image.getWidth(null);
int h = image.getHeight(null);
// adjust weight and height to avoid image distortion
double scale = 0;
scale = Math.min((float) ppi / w, (float) ppi / h);
width = (int) (w * scale);
width -= width % 4;
height = (int) (h * scale);
if (scale >= (double) 1)
return imageByte;
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
buffImg.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(buffImg, "png", out);
smallImage = out.toByteArray();
return smallImage;
} catch (IOException e) {
log.error(e.getMessage());
throw new RSServerInternalException("");
}
}
其实,关键点就两处
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
buffImg.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。