Java图片压缩
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图片压缩的更多相关文章
- java图片压缩(Thumbnails)
package com.hzxc.groupactivity.server.util; import java.awt.image.BufferedImage; import java.io.*; i ...
- Java 图片压缩
package com.wuyu.util; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.Buff ...
- 在windows和Linux上安装ImageMagick与jmagick,Maven配置、Java图片压缩代码(整理网上、结合自己情况、编写出来的新安装方式)
安装过程(如图所示) .Exceptionin thread "main" java.lang.UnsatisfiedLinkError:C:\WINDOWS\system32\j ...
- java 图片压缩 缩放
废话不多说,直接上代码,静态方法可直接调用,中间用流来处理的 /** * 图片缩放(未考虑多种图片格式和等比例缩放) * @param filePath 图片路径 * @param height 高度 ...
- java,图片压缩,略缩图
在网上找了两个图片的缩放类,在这里分享一下: package manager.util; import java.util.Calendar; import java.io.File; import ...
- java 图片压缩 剪切 水印 转换 黑白 缩放
专注java已6年,欢迎加入java核心技术QQ群:135138817,每周五晚有群主进行技术讲座. import java.awt.AlphaComposite; import java.awt.C ...
- Thumbnailator java图片压缩,加水印,批量生成缩略图
地址:http://code.google.com/p/thumbnailator/ 1.指定大小进行缩放 //size(宽度, 高度) /* * 若图片横比200小,高比300小,不变 * 若图片横 ...
- 纯Java代码 图片压缩
Java图片压缩代码 package com.img; import java.awt.Image; import java.awt.image.BufferedImage; import java. ...
- Java中图片压缩处理
原文http://cuisuqiang.iteye.com/blog/2045855 整理文档,搜刮出一个Java做图片压缩的代码,稍微整理精简一下做下分享. 首先,要压缩的图片格式不能说动态图片,你 ...
随机推荐
- PHP代码实现2 [从变量和数据的角度] 1
PHP代码实现2 [从变量和数据的角度] 1 数据类型 1.静态类型语言,比如:C/Java等,在静态语言类型中,类型的检查是在<编译>(compile-time)确定的, 也就是说在运行 ...
- Speeding Up The Traveling Salesman Using Dynamic Programming
Copied From:https://medium.com/basecs/speeding-up-the-traveling-salesman-using-dynamic-programming-b ...
- django报错总结
问题一: dictionary update sequence element #1 has length 3; 2 is required 解决方法: 检查视图函数的render里传的字典
- vuex状态管理2
在vuex的官网https://vuex.vuejs.org中,提到的核心概念一共有5个,分别是State.Getter.Mutation.Action和Module,在上一篇随笔中,我们主要用到其中 ...
- 学习-工作:GTD
ylbtech-学习-工作:GTD GTD就是Getting Things Done的缩写,翻译过来就是“把事情做完”,是一个管理时间的方法.GTD的核心理念概括就是必须记录下来要做的事,然后整理安排 ...
- HDOJ 2013 蟠桃记
#include<iostream> using namespace std; int main() { int n; while (cin >> n) { ; ;i < ...
- Linux 下 MQ 的安装
在WebSphere MQ 7.1版本以前,同时只能有一个产品的安装实例,在UNIX和Linux系统上,/usr/lib,/usr/bin和/usr/include目录下会增加一些软连接,也指向了这个 ...
- Android 通过联系人姓名查询联系人号码
<!-- 读联系人权限 --><uses-permission android:name="android.permission.READ_CONTACTS" / ...
- python容器数据类型的特色
python容器数据类型的特色 list: 可变数据类型(不可哈希), 有序, 可索引获取, 可修改 Dict: 可变数据类型(不可哈希), 3.6版本有序, 可通 ...
- 10 Skills Every SharePoint Developer Needs
10 Skills Every SharePoint Developer Needs(原文) This blog post guides you through the essential skill ...