直播网站程序源码,Java实现图片压缩

编程入门 行业动态 更新时间:2024-10-18 12:29:43

直播网站程序<a href=https://www.elefans.com/category/jswz/34/1770099.html style=源码,Java实现图片压缩"/>

直播网站程序源码,Java实现图片压缩

直播网站程序源码,Java实现图片压缩
1、Thumbnailator简介
对于图片处理,JDK中也提供了对应的工具类,不过处理起来会很麻烦,而Thumbnailator是一个Google开源的优秀图片处理的第三方Java类库,处理效果远比Java API的好。
Thumbnailator可以使用很少的代码实现图片的压缩功能,当然了,Thumbnailator也提供给图片缩放、旋转与加水印等功能。

2、引入依赖
2.1、github地址


2.2、maven地址

<dependency><groupId>net.coobird</groupId><artifactId>thumbnailator</artifactId><version>0.4.17</version>
</dependency>

3、代码

import net.coobird.thumbnailator.Thumbnails;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;/*** 图片处理工具类.** @author admin* @date 2022-09-23 17:10*/
public class PicUtils {public static void main(String[] args) {PicUtilspressPicForScale("C:\\Users\\Li\\Downloads\\test.jpeg", "C:\\Users\\Li\\Downloads\\test1.jpeg",100, 0.8, 2560, 1440); // 图片小于1000kb}/*** 根据指定大小和指定精度压缩图片** @param srcPath      源图片地址* @param desPath      目标图片地址* @param desFileSize  指定图片大小,单位kb(压缩到多大以内)* @param accuracy     精度,递归压缩的比率,建议小于0.9* @param desMaxWidth  目标最大宽度* @param desMaxHeight 目标最大高度* @return 目标文件路径*/public static String compressPicForScale(String srcPath, String desPath, long desFileSize, double accuracy, int desMaxWidth, int desMaxHeight) {if (!new File(srcPath).exists()) {return null;}try {File srcFile = new File(srcPath);long srcFileSize = srcFile.length();System.out.println("源图片:" + srcPath + ",大小:" + srcFileSize / 1024 + "kb");//获取图片信息BufferedImage bim = ImageIO.read(srcFile);int srcWidth = bim.getWidth();int srcHeight = bim.getHeight();//先转换成jpgThumbnails.Builder<File> builder = Thumbnails.of(srcFile).outputFormat("jpg");// 指定大小(宽或高超出会才会被缩放)if (srcWidth > desMaxWidth || srcHeight > desMaxHeight) {builder.size(desMaxWidth, desMaxHeight);} else {//宽高均小,指定原大小builder.size(srcWidth, srcHeight);}// 写入到内存ByteArrayOutputStream bos = new ByteArrayOutputStream(); //字节输出流(写入到内存)builder.toOutputStream(bos);// 递归压缩,直到目标文件大小小于desFileSizebyte[] bytes = compressPicCycle(bos.toByteArray(), desFileSize, accuracy);// 输出到文件File desFile = new File(desPath);FileOutputStream fos = new FileOutputStream(desFile);fos.write(bytes);fos.close();System.out.println("目标图片:" + desPath + ",大小" + desFile.length() / 1024 + "kb");System.out.println("图片压缩完成!");} catch (Exception e) {e.printStackTrace();return null;}return desPath;}private static byte[] compressPicCycle(byte[] bytes, long desFileSize, double accuracy) throws IOException {// File srcFileJPG = new File(desPath);long srcFileSizeJPG = bytes.length;// 2、判断大小,如果小于500kb,不压缩;如果大于等于500kb,压缩if (srcFileSizeJPG <= desFileSize * 1024) {return bytes;}// 计算宽高BufferedImage bim = ImageIO.read(new ByteArrayInputStream(bytes));int srcWidth = bim.getWidth();int srcHeight = bim.getHeight();int desWidth = new BigDecimal(srcWidth).multiply(new BigDecimal(accuracy)).intValue();int desHeight = new BigDecimal(srcHeight).multiply(new BigDecimal(accuracy)).intValue();ByteArrayOutputStream bos = new ByteArrayOutputStream(); //字节输出流(写入到内存)Thumbnails.of(new ByteArrayInputStream(bytes)).size(desWidth, desHeight).outputQuality(accuracy).toOutputStream(bos);return compressPicCycle(bos.toByteArray(), desFileSize, accuracy);}}

以上就是直播网站程序源码,Java实现图片压缩, 更多内容欢迎关注之后的文章

更多推荐

直播网站程序源码,Java实现图片压缩

本文发布于:2024-02-27 05:37:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1705378.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:源码   程序   图片   网站   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!