Java 后端压缩图片
import java.io.*;
import java.util.Date;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
//import com.sun.image.codec.jpeg.*;
public class ImgCompress {
private Image img;
private int width;
private int height;
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
System.out.println("开始:" + new Date().toLocaleString());
ImgCompress imgCom = new ImgCompress("C:\\Users\\fh123\\Desktop\\TIM图片20180511172046.jpg");
imgCom.resizeFix(600, 600,"C:\\Users\\fh123\\Desktop\\TIM图片20180511172046.jpg");
System.out.println("结束:" + new Date().toLocaleString());
}
public ImgCompress(String fileName) throws IOException {
File file = new File(fileName);// 读入文件
img = ImageIO.read(file); // 构造Image对象
width = img.getWidth(null); // 得到源图宽
height = img.getHeight(null); // 得到源图长
}
/**
* 按照宽度还是高度进行压缩
*
* @param w
* int 最大宽度
* @param h
* int 最大高度
*/
public void resizeFix(int w, int h,String urlPath) throws IOException {
if (width / height > w / h) {
resizeByWidth(w,urlPath);
} else {
resizeByHeight(h,urlPath);
}
}
/**
* 以宽度为基准,等比例放缩图片
*
* @param w
* int 新宽度
*/
public void resizeByWidth(int w,String urlPath) throws IOException {
int h = (int) (height * w / width);
resize(w, h,urlPath);
}
/**
* 以高度为基准,等比例缩放图片
*
* @param h
* int 新高度
*/
public void resizeByHeight(int h,String urlPath) throws IOException {
int w = (int) (width * h / height);
resize(w, h,urlPath);
}
/**
* 强制压缩/放大图片到固定的大小
*
* @param w
* int 新宽度
* @param h
* int 新高度
*/
public void resize(int w, int h,String urlPath) 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(urlPath);
FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
ImageIO.write(image,"jpg", out);
// 可以正常实现bmp、png、gif转jpg
//JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//encoder.encode(image); // JPEG编码
out.close();
}
}
Java 后端压缩图片的更多相关文章
- 使用java来压缩图片
使用java来压缩图片,简单几句,清清爽爽 使用0.3的压缩比得到的结果如下(从2.8M压缩到268K,且图片的清晰度看不出明显差别): package carlspringtest; import ...
- java 上传图片压缩图片
package com.bitspace.flame.util; import java.io.File; import java.awt.Image;import java.awt.image.Bu ...
- Java后端实现图片压缩技术
今天来说说图片压缩技术,为什么要使用图片压缩,图片上传不就完事了吗?对的,这在几年前可以这么说,因为几年前还没有现在这么大的并发,也没有现在这么关注性能. 如今手机很多,很多人都是通过手机访问网络或者 ...
- java流压缩图片
整理文档,搜刮出一个Java做图片压缩的代码,稍微整理精简一下做下分享.首先,要压缩的图片格式不能说动态图片,你可以使用bmp.png.gif等,至于压缩质量,可以通过BufferedImage来指定 ...
- java 上传图片 并压缩图片大小
Thumbnailator 是一个优秀的图片处理的Google开源Java类库.处理效果远比Java API的好.从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生 ...
- java 上传图片 并压缩图片大小(转)
Thumbnailator 是一个优秀的图片处理的Google开源Java类库.处理效果远比Java API的好.从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生 ...
- Java压缩图片的实现类
package com.function; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; ...
- java上传并压缩图片(等比例压缩或者原尺寸压缩)
本文转载自http://www.voidcn.com/article/p-npjxrbxr-kd.html 先看效果: 原图:1.33M 处理后:27.4kb 关键代码; package codeGe ...
- java中判断图片格式并且等比例压缩图片
最近项目中需要判断上传的图片必须是png,jpg,gif三种格式的图片,并且当图片的宽度大于600px时,压缩图片至600px,并且等比例的压缩图片的高度. 具体的实现形式: 大致的思路是: 判断根据 ...
随机推荐
- 【C语言】判断某一正整数是否为完数
什么是完数? 如果一个数等于它的因子之和,则称该数为“完数”(或“完全数”). 例如,6的因子为1.2.3,而 6=1+2+3,因此6是“完数”. 程序框图:m 问题分析 根据完数的定义,解决本题的 ...
- UIDocumentPickerViewController和UIDocumentInteractionController
UIDocumentPickerViewController和UIDocumentInteractionController UIDocumentPickerViewController 补充一下,U ...
- python二维数组的创建
话不多说,代码伺候 m = [[]*]*3 #创建一个3行5列的二维数组 m[][]= print(m) 输出结果为: 分析: m = [[0]*5]*3只是指向三个空列表的引用. 创建一个二维数组的 ...
- Centos610 Oracle 监听文件配置参考
lister.ora配置参考 # listener.ora Network Configuration File: /home/oracle/app/oracle/product//dbhome_1/ ...
- Linux - TLCL
三. 文件系统中跳转 pwd - Print name of current working directory cd - Change directory ls - List directory c ...
- websocket 无法查看Data
websocket 是浏览器新的信息传输协议,记录一些遇到的问题: 调试相关: websocket 连接以后可以在Chrome tools 中的network下看到,如图 要查看通信内容可以选中点击f ...
- linux理论知识点(用于考试)
ps:为其十天左右的linux培训即将结束了,未雨绸缪,为了更好的通过之后的考试,提前多看些考试题和知识点.这是在chinaunix论坛看到的一个帖子,贴来分享. 原文地址:[http://bbs.c ...
- Hibernate学习(二)
持久化对象的声明周期 1.Hibernate管理的持久化对象(PO persistence object )的生命周期有四种状态,分别是transient.persistent.detached和re ...
- 【原】简单shell练习(五)
1.查询一个文件中某个单词出现的次数 # grep -o 'ts' ./test/txt |wc -l 2.统计当前文件夹下文件的个数 ls -l |grep "^-"|wc -l ...
- Math 用法
console.log(Math.abs(-5)) 取绝对值 console.log(Math.round(5.1)) 取四舍五入 5.5 为中间值 取5 console.log(Math.ceil( ...