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做图片压缩的代码,稍微整理精简一下做下分享. 首先,要压缩的图片格式不能说动态图片,你 ...
随机推荐
- 关于div
<article id="a"> <div class="a1"></div> <div class="a2 ...
- shell 查看去掉windons中的换行符
查看 cat -v 1.sh 替换 sed -i 's/\r//g' 1.sh
- Nuke的色彩匹配节点思路
大致思路是首先利用node.sample()自带采样功能对整个画面逐像素采 样,我把采样函数写的比较灵活,可以定义一个采样族,即把整个画面分成若干块,逐块采样,很大程度上减少了采样工作量,但相对精度会 ...
- Python3中Urllib库基本使用
什么是Urllib? Python内置的HTTP请求库 urllib.request 请求模块 urllib.error 异常处理模块 urllib.par ...
- 【问题解决】docker中创建volume时,无访问权限
挂载宿主机已存在目录后,在容器内对其进行操作,报"Permission denied". 可通过两种方式解决: 1> 关闭selinux. 临时关闭:# setenforce ...
- Java学习——多线程例子:银行
package cys; public class Example9_3 { public static void main(String[] args) { // TODO Auto-generat ...
- 开发框架-.Net:Learun(力软敏捷开发)
ylbtech-开发框架-.Net:Learun(力软敏捷开发) 1.返回顶部 2.返回顶部 1. 系统简介:(1)后台采用MVC+EF架构,前台使用Jquery+Bootstrap,界面美观大气 ...
- Vue Admin 后台管理
https://segmentfault.com/a/1190000009188689
- MySQL 设置密码有效时间
mysql> alter user 'xuaiqi'@'%' PASSWORD EXPIRE INTERVAL 30 DAY;
- 第9章 应用层(2)_动态主机配置协议(DHCP)
2. 动态主机配置协议(DHCP) 2.1 静态地址和动态地址的应用场景 (1)静态地址应用场景 ①IP地址不经常更改的设备(如服务器地址) ②使用有规律的IP地址以便于管理(如学校机房为方便教师管理 ...