package com.test;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.junit.Test;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; /**
*图片压缩
*/
public class ImageTest { private Image img;
private int width;
private int height; @Test
public void test(){
try{
//压缩开始
ImageTest imgCom = new ImageTest("D://1.png");
imgCom.resizeFix(400, 400,"D://2.png");
//压缩结束
}catch (Exception e){
e.printStackTrace();
}
} /**
* 构造函数
* @param fileName 图片路径
* @throws IOException
*/
public ImageTest(String fileName) throws IOException {
// 读入文件
File file = new File(fileName);
// 构造Image对象
img = ImageIO.read(file);
// 得到源图宽
width = img.getWidth(null);
// 得到源图长
height = img.getHeight(null);
} /**
* 按照宽度还是高度进行压缩
* @param w int 最大宽度
* @param h int 最大高度
* @param filePath 图片路径
* @throws IOException
*/
public void resizeFix(int w, int h,String filePath) throws IOException {
if (width / height > w / h) {
resizeByWidth(w,filePath);
} else {
resizeByHeight(h,filePath);
}
} /**
* 以宽度为基准,等比例放缩图片
* @param w int 新宽度
* @param filePath 图片路径
* @throws IOException
*/
public void resizeByWidth(int w,String filePath) throws IOException {
int h = (int) (height * w / width);
resize(w, h,filePath);
} /**
* 以高度为基准,等比例缩放图片
* @param h int 新高度
* @param filePath 图片路径
* @throws IOException
*/
public void resizeByHeight(int h,String filePath) throws IOException {
int w = (int) (width * h / height);
resize(w, h,filePath);
} /**
* 强制压缩/放大图片到固定的大小
* @param w int 新宽度
* @param h int 新高度
* @param filePath
* @throws IOException
*/
public void resize(int w, int h,String filePath) throws IOException {
// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );
// 绘制缩小后的图
image.getGraphics().drawImage(img, 0, 0, w, h, null);
File destFile = new File(filePath);
// 输出到文件流
FileOutputStream out = new FileOutputStream(destFile);
// 可以正常实现bmp、png、gif转jpg
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
// JPEG编码
encoder.encode(image);
out.close();
}
}

  

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

  1. java图片压缩(Thumbnails)

    package com.hzxc.groupactivity.server.util; import java.awt.image.BufferedImage; import java.io.*; i ...

  2. Java 图片压缩

    package com.wuyu.util; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.Buff ...

  3. 在windows和Linux上安装ImageMagick与jmagick,Maven配置、Java图片压缩代码(整理网上、结合自己情况、编写出来的新安装方式)

    安装过程(如图所示) .Exceptionin thread "main" java.lang.UnsatisfiedLinkError:C:\WINDOWS\system32\j ...

  4. java 图片压缩 缩放

    废话不多说,直接上代码,静态方法可直接调用,中间用流来处理的 /** * 图片缩放(未考虑多种图片格式和等比例缩放) * @param filePath 图片路径 * @param height 高度 ...

  5. java,图片压缩,略缩图

    在网上找了两个图片的缩放类,在这里分享一下: package manager.util; import java.util.Calendar; import java.io.File; import ...

  6. java 图片压缩 剪切 水印 转换 黑白 缩放

    专注java已6年,欢迎加入java核心技术QQ群:135138817,每周五晚有群主进行技术讲座. import java.awt.AlphaComposite; import java.awt.C ...

  7. Thumbnailator java图片压缩,加水印,批量生成缩略图

    地址:http://code.google.com/p/thumbnailator/ 1.指定大小进行缩放 //size(宽度, 高度) /* * 若图片横比200小,高比300小,不变 * 若图片横 ...

  8. 纯Java代码 图片压缩

    Java图片压缩代码 package com.img; import java.awt.Image; import java.awt.image.BufferedImage; import java. ...

  9. Java中图片压缩处理

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

随机推荐

  1. jQuery自定义扩展写法

    jQuery自定义扩展有两种写法: $.extend({}) ;           调用方法:      $.方法          $.fn.extend({});        调用方法:    ...

  2. Azure PowerShell (16) 并行开关机Azure ARM VM

    <Windows Azure Platform 系列文章目录> 并行开机脚本: https://github.com/leizhang1984/AzureChinaPowerShell/b ...

  3. 在已有Windows系统基础上安装ubuntu后无Windows启动项

    1. 原系统为windows 2. 清出一个不用的分区给linux,使用windows磁盘管理器将该分区重新划分为两个分区,分别用于root和swap 3. 安装过程中ubuntu提示原系统为非UEF ...

  4. 关于Dubbo面试问题

    一.默认使用的是什么通信框架,还有别的选择吗? 默认也推荐使用netty框架,还有mina. 二.服务调用是阻塞的吗? 默认是阻塞的,可以异步调用,没有返回值的可以这么做. 三.一般使用什么注册中心? ...

  5. Osip2和eXosip协议栈的简析

    Osip2是一个开放源代码的sip协议栈,是开源代码中不多使用C语言写的协议栈之一,它具有短小简洁的特点,专注于sip底层解析使得它的效率比较高. eXosip是Osip2的一个扩展协议集,它部分封装 ...

  6. vue中滚动事件绑定的函数无法调用问题

    问题描述: 一个包含下拉加载的页面,刷新当前页然后滚动页面,能够正常触发滚动事件并调用回调函数,但是如果是进了某一个页面然后再进的该页面,滚动事件能够触发, 但是回调函数在滚动的时候只能被调用一次. ...

  7. 显示定位方法,提取中间text 封装成函数的方法

    tager='工作台' element=WebDriverWait(self.dr,15,0.1).until( eval("lambda x: x."+'find_element ...

  8. Flume监听文件目录sink至hdfs配置

    一:flume介绍 Flume是一个分布式.可靠.和高可用的海量日志聚合的系统,支持在系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并写到各种数据接受方(可定制)的能 ...

  9. opengl 入门浅学(一)

    因为要做图形学的实验,又是要以OPENGL为基础,所以就稍微在网上查了一些资料. 我是带着目的去学习的,所以就没有打基础之类的学很深,浅尝. 今天试着搭简单框架,画出一个图形.大神请出门左转. #in ...

  10. 关于微信小程序如何解决多层循环嵌套

    http://www.jianshu.com/p/87cdf985b2b9 附加:小程序里面一些功能示例 http://blog.csdn.net/column/details/13721.html