Java 图片压缩实现思路及代码

 更新时间:2013年07月02日 15:07:38   作者:  
本文为大家详细介绍下图片压缩的具体实现思路及java代码,想学习的各位可以参考下哈,希望对大家有所帮助
Java图片压缩代码
复制代码 代码如下:

package com.img;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
*
* @author 可乐加糖
*/
public class CompressPicDemo {
private File file = null; // 文件对象
private String inputDir; // 输入图路径
private String outputDir; // 输出图路径
private String inputFileName; // 输入图文件名
private String outputFileName; // 输出图文件名
private int outputWidth = 100; // 默认输出图片宽
private int outputHeight = 100; // 默认输出图片高
private boolean proportion = true; // 是否等比缩放标记(默认为等比缩放)
public CompressPicDemo() { // 初始化变量
inputDir = "";
outputDir = "";
inputFileName = "";
outputFileName = "";
outputWidth = 100;
outputHeight = 100;
}
public void setInputDir(String inputDir) {
this.inputDir = inputDir;
}
public void setOutputDir(String outputDir) {
this.outputDir = outputDir;
}
public void setInputFileName(String inputFileName) {
this.inputFileName = inputFileName;
}
public void setOutputFileName(String outputFileName) {
this.outputFileName = outputFileName;
}
public void setOutputWidth(int outputWidth) {
this.outputWidth = outputWidth;
}
public void setOutputHeight(int outputHeight) {
this.outputHeight = outputHeight;
}
public void setWidthAndHeight(int width, int height) {
this.outputWidth = width;
this.outputHeight = height;
}
/*
* 获得图片大小
* 传入参数 String path :图片路径
*/
public long getPicSize(String path) {
file = new File(path);
return file.length();
}
// 图片处理
public String compressPic() {
try {
//获得源文件
file = new File(inputDir + inputFileName);
if (!file.exists()) {
return "";
}
Image img = ImageIO.read(file);
// 判断图片格式是否正确
if (img.getWidth(null) == -1) {
System.out.println(" can't read,retry!" + "<BR>");
return "no";
} else {
int newWidth; int newHeight;
// 判断是否是等比缩放
if (this.proportion == true) {
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
// 根据缩放比率大的进行缩放控制
double rate = rate1 > rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
} else {
newWidth = img.getWidth(null); // 输出的图片宽度
newHeight = img.getHeight(null); // 输出的图片高度
}
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
/*
* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的
* 优先级比速度高 生成的图片质量比较好 但速度慢
*/
tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
// JPEGImageEncoder可适用于其他图片类型的转换
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return "ok";
}
public String compressPic (String inputDir, String outputDir, String inputFileName, String outputFileName) {
// 输入图路径
this.inputDir = inputDir;
// 输出图路径
this.outputDir = outputDir;
// 输入图文件名
this.inputFileName = inputFileName;
// 输出图文件名
this.outputFileName = outputFileName;
return compressPic();
}
public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName, int width, int height, boolean gp) {
// 输入图路径
this.inputDir = inputDir;
// 输出图路径
this.outputDir = outputDir;
// 输入图文件名
this.inputFileName = inputFileName;
// 输出图文件名
this.outputFileName = outputFileName;
// 设置图片长宽
setWidthAndHeight(width, height);
// 是否是等比缩放 标记
this.proportion = gp;
return compressPic();
}
// main测试
// compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
public static void main(String[] arg) {
CompressPicDemo mypic = new CompressPicDemo();
System.out.println("输入的图片大小:" + mypic.getPicSize("e:\\1.jpg")/1024 + "KB");
mypic.compressPic("e:\\", "e:\\test\\", "1.jpg", "r1.jpg", 120, 120, false);
}
}

相关文章

  • SpringBoot中的异常处理与参数校验的方法实现

    SpringBoot中的异常处理与参数校验的方法实现

    这篇文章主要介绍了SpringBoot中的异常处理与参数校验的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • WebSocket实现系统后台消息实时通知功能

    WebSocket实现系统后台消息实时通知功能

    在现代Web应用中,提供实时通知对于改善用户体验至关重要,WebSocket技术允许建立双向通信通道,从系统后台将消息实时传送给系统用户,下面我们就来深入探讨一下如何使用WebSocket来实现这一功能吧
    2023-10-10
  • Spring中@EnableScheduling注解的工作原理详解

    Spring中@EnableScheduling注解的工作原理详解

    这篇文章主要介绍了Spring中@EnableScheduling注解的工作原理详解,@EnableScheduling是 Spring Framework 提供的一个注解,用于启用Spring的定时任务(Scheduling)功能,需要的朋友可以参考下
    2024-01-01
  • Java使用贪心算法解决电台覆盖问题(示例详解)

    Java使用贪心算法解决电台覆盖问题(示例详解)

    贪心算法是指在对问题进行求解时,在每一步选择中都采取最好或最优的选择,从而导致结果理想化,下面通过本文介绍下Java使用贪心算法解决电台覆盖问题,需要的朋友可以参考下
    2022-04-04
  • 详解Spring通过@Value注解注入属性的几种方式

    详解Spring通过@Value注解注入属性的几种方式

    本篇文章主要介绍了详解Spring通过@Value注解注入属性的几种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-03-03
  • 注入jar包里的对象,用@autowired的实例

    注入jar包里的对象,用@autowired的实例

    这篇文章主要介绍了注入jar包里的对象,用@autowired的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • java中多态概念、实现原理详解

    java中多态概念、实现原理详解

    JAVA中多态性是对象多种表现形式的体现。在面向对象中,最常见的多态发生在使用父类的引用来引用子类的对象。下面这篇文章主要给大家介绍一下,需要的朋友可以参考下
    2017-04-04
  • SpringBoot数据库恢复的两种方法mysqldump和mysqlbinlog

    SpringBoot数据库恢复的两种方法mysqldump和mysqlbinlog

    binlog用来实现主从复制,也常用来误删数据库找回丢失的记录,本文主要介绍了SpringBoot数据库恢复的两种方法mysqldump和mysqlbinlog,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • java使用EasyExcel实现Sheet的复制与填充

    java使用EasyExcel实现Sheet的复制与填充

    EasyExcel是一个非常有用的工具,它提供了强大的模板填充功能,可以轻松解决各种业务需求,本文主要为大家介绍了如何使用EasyExcel实现模板Sheet复制与填充,需要的可以参考下
    2023-10-10
  • 详解Java对象序列化为什么要使用SerialversionUID

    详解Java对象序列化为什么要使用SerialversionUID

    这篇文章主要介绍了详解Java对象序列化为什么要使用SerialversionUID,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11

最新评论