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);

}
}

纯Java代码 图片压缩的更多相关文章

  1. Java中图片压缩处理

    原文http://cuisuqiang.iteye.com/blog/2045855 整理文档,搜刮出一个Java做图片压缩的代码,稍微整理精简一下做下分享. 首先,要压缩的图片格式不能说动态图片,你 ...

  2. 帧动画的创建方式 - 纯Java代码方式

    废话不多说,先看东西 帧动画的创建方式主要以下2种: * 用xml创建动画: * 纯Java代码创建动画:   本文内容主要关注 纯java代码创建帧动画 的方式: 用xml创建帧动画:http:// ...

  3. 基于纯Java代码的Spring容器和Web容器零配置的思考和实现(3) - 使用配置

    经过<基于纯Java代码的Spring容器和Web容器零配置的思考和实现(1) - 数据源与事务管理>和<基于纯Java代码的Spring容器和Web容器零配置的思考和实现(2) - ...

  4. Android 使用纯Java代码布局

    java布局 java代码布局和xml布局的区别 1.Java纯布局更加的灵活,比如自定义控件或一些特殊要求时,使用java代码布局 2.常用的xml布局是所见即所得的编写方式,以及xml本身拥有一些 ...

  5. DataX通过纯Java代码启动

    DataX是阿里巴巴团队开发的一个很好开源项目,但是他们对如何使用只提供了python命令启动方式,这种方式对于只是想简单的用下DataX的人来说很是友好,仅仅需要几行代码就可以运行,但是如果你需要在 ...

  6. java实现图片压缩

    java实现图片压缩 package Test; import java.awt.Image; import java.awt.image.BufferedImage; import java.io. ...

  7. 在Android中用纯Java代码布局

    感谢大佬:https://www.jianshu.com/p/7aedea560f16 在Android中用纯Java代码布局 本文的完成了参考了一篇国外的教程,在此表示感谢. Android中的界面 ...

  8. java服务器图片压缩的几种方式及效率比较

    以下是测试了三种图片压缩方式,通过测试发现使用jdk的ImageIO压缩时间更短,使用Google的thumbnailator更简单,但是thumbnailator在GitHub上的源码已经停止维护了 ...

  9. java:图片压缩

    java使用google开源工具实现图片压缩 :http://www.cnblogs.com/linkstar/p/7412012.html

随机推荐

  1. orleans开篇之hello world

    orleans开篇之hello world 什么是orleans Orleans是一个建立在.NET之上的,设计的目标是为了方便程序员开发需要大规模扩展的云服务.Orleans项目基本上被认为是并行计 ...

  2. IntelliJ IDEA —— Android开发的另一个选择

    IntelliJ IDEA —— Android开发的另一个选择 很早就听过Eclipse的大名,但在我初学Java的时候,一位前辈推荐的IDE却是IntelliJ IDEA.因为之前用过JetBra ...

  3. POI导出大量数据的简单解决方案(附源码)-Java-POI导出大量数据,导出Excel文件,压缩ZIP(转载自iteye.com)

    说明:我的电脑 2.0CPU 2G内存 能够十秒钟导出 20W 条数据 ,12.8M的excel内容压缩后2.68M 我们知道在POI导出Excel时,数据量大了,很容易导致内存溢出.由于Excel ...

  4. How to:Installshield判断操作系统是否为64位,并且为操作注册表进行设置

    原文:How to:Installshield判断操作系统是否为64位,并且为操作注册表进行设置 IS脚本操作注册表在64位平台下必须有特殊的设置 if (SYSINFO.bIsWow64) then ...

  5. 纯CSS隔行换色

    原文:纯CSS隔行换色 <head> <meta http-equiv="Content-Type" content="text/html; chars ...

  6. Spring AspectJ的Execution表达式-备忘笔记

    Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式  execu ...

  7. [译]Java 设计模式 之模板方法

    (文章翻译自Java Design Pattern: Template Method) 模板方法设计模式定义了归档特定操作的工作流.它允许子类去修改特定的步奏而不用改变工作流的结构. 下面的例子表示模 ...

  8. List environment variables from Command Prompt

    Request: List the environment variables from Command Promt To list one varibales , the syntax is lik ...

  9. [Dev Blog] KCV插件 —— Provissy Tools 。

    承蒙各位支持! 正式版已推出,请前往http://tieba.baidu.com/p/3398574166 或者前往:http://provissy.com/?p=7 请不要在这里回复,我无法保证回复 ...

  10. DataGridView的使用和批量修改

    DataGridView的属性:AllowUserToAddRows:如果为true允许用户添加行,false不允许用户添加行ReadOnly:true表示只读.不能修改单元格中的值,false可以对 ...