一个好用的java图片缩放及质量压缩方法
本文中代码来自:http://blog.csdn.net/liuhuanchao/article/details/50527856
由于网站需要对上传的图片进行宽度判断缩放和质量压缩,以提升整体加载速度,于是我在网上找处理方法,
网上大多数是谷歌图片处理组件Thumbnails的介绍。最开始我用Thumbnails尝试,但不知道什么原因,没有任何效果,也不报错。
由于时间的关系,就没再深入研究,另寻他路。后来找到了下面的方法,这个方法优点在于完全基于Java自带API,调用也非常简单,如果只是缩放和压缩,这个方法足够了。
代码:
1 /**
2 * 缩放图片(压缩图片质量,改变图片尺寸)
3 * 若原图宽度小于新宽度,则宽度不变!
4 * @param newWidth 新的宽度
5 * @param quality 图片质量参数 0.7f 相当于70%质量
6 */
7 public static void imageResize(File originalFile, File resizedFile,
8 int maxWidth,int maxHeight, float quality) throws IOException {
9
10 if (quality > 1) {
11 throw new IllegalArgumentException(
12 "图片质量需设置在0.1-1范围");
13 }
14
15 ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
16 Image i = ii.getImage();
17 Image resizedImage = null;
18
19 int iWidth = i.getWidth(null);
20 int iHeight = i.getHeight(null);
21
22 int newWidth = maxWidth;
23 if(iWidth < maxWidth){
24 newWidth = iWidth;
25 }
26
27
28 if (iWidth >= iHeight) {
29 resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
30 / iWidth, Image.SCALE_SMOOTH);
31 }
32
33
34
35 int newHeight = maxHeight;
36 if(iHeight < maxHeight){
37 newHeight = iHeight;
38 }
39
40 if(resizedImage==null && iHeight >= iWidth){
41 resizedImage = i.getScaledInstance((newHeight * iWidth) / iHeight,
42 newHeight, Image.SCALE_SMOOTH);
43 }
44
45 // This code ensures that all the pixels in the image are loaded.
46 Image temp = new ImageIcon(resizedImage).getImage();
47
48 // Create the buffered image.
49 BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
50 temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
51
52 // Copy image to buffered image.
53 Graphics g = bufferedImage.createGraphics();
54
55 // Clear background and paint the image.
56 g.setColor(Color.white);
57 g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
58 g.drawImage(temp, 0, 0, null);
59 g.dispose();
60
61 // Soften.
62 float softenFactor = 0.05f;
63 float[] softenArray = { 0, softenFactor, 0, softenFactor,
64 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
65 Kernel kernel = new Kernel(3, 3, softenArray);
66 ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
67 bufferedImage = cOp.filter(bufferedImage, null);
68
69 // Write the jpeg to a file.
70 FileOutputStream out = new FileOutputStream(resizedFile);
71
72 // Encodes image as a JPEG data stream
73 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
74
75 JPEGEncodeParam param = encoder
76 .getDefaultJPEGEncodeParam(bufferedImage);
77
78 param.setQuality(quality, true);
79
80 encoder.setJPEGEncodeParam(param);
81 encoder.encode(bufferedImage);
82 } // Example usage
调用:
1 //图片压缩处理(缩放+质量压缩以减小高宽度及数据量大小)
2 imageResize(imgFile,imgFile,1200,3000,0.9f);//宽度大于1200的,缩放为1200,高度大于3000的缩放为3000,按比例缩放,质量压缩掉10%
推荐一个好用的聚合搜索引擎,https://www.tomfind.com/ ,支持谷歌、百度、搜狗、bing、360一键切换,支持网页、音乐、购物、视频搜索、支持抖音、快手、哔哩哔哩、微信、头条、知乎搜索、支持顶部栏、左侧栏两种界面模式。
一个好用的java图片缩放及质量压缩方法的更多相关文章
- java图片缩放
package com.rubekid.springmvc.utils; import java.awt.AlphaComposite; import java.awt.Graphics2D; imp ...
- java图片压缩工具类(指定压缩大小)
1:先导入依赖 <!--thumbnailator图片处理--> <dependency> <groupId>net.coobird</groupId> ...
- java图片缩放与裁剪
import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import java.io ...
- java 图片缩放
使用java自带的图片处理api,也可以使用(GraphicsMagick + im4j) import java.awt.Image; import java.awt.image.BufferedI ...
- Asp.net 实现图片缩放 无水印(方法二)
public static System.Drawing.Image GetImage(string path) { try { if (path.StartsWith("http" ...
- php图片上传旋转压缩方法
用到php的exif扩展,需要开启exif 在php.ini文件中去掉exif组件的注释 extension=php_mbstring.dll //要放在php_exif.dll前面让它先加载 ext ...
- Java 图片处理——如何生成高清晰度而占有磁盘小的缩略图
现在的web项目,图片越来越多,图片大小也越来越大,随便就能达到1M,2M,甚至更大.用户上传的图片,一般是无法直接使用的.一般要生成两三种对应的缩略图,分别适配不同的终端,不同的场景.比如PC,手机 ...
- Android图片压缩方法总结
本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩). 第一:质量压缩方法: ? 1 2 3 ...
- android图片压缩方法
android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...
- Android应用开发中三种常见的图片压缩方法
Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩). 一.质量压缩法 private Bitmap com ...
随机推荐
- vue-devtools 打开 vscode 可能会报错
据说 99% 的人不知道 vue-devtools 还能直接打开对应组件文件?本文原理揭秘 mac 电脑在 VSCode command + shift + p,Windows 则是 ctrl + s ...
- Linux基础——操作系统
1. 操作系统(Operation System,OS) 操作系统作为接口的示意图 如果想在裸机上运行自己所编写的程序,就必须用机器语言书写程序如果计算机上安装了操作系统,就可以在操作系统上安装支持的 ...
- Nginx lavarel框架伪静态配置
location / { try_files $uri $uri/ /index.php$is_args$query_string; }
- DOC命令学习(一)
DOC命令学习(一) 命令 切换目录命令(cd) cd /d E: 查看目录命令(dir) dir #查看# 回显命令(echo) @echo offset d = %date:~0,10%set ...
- element表格样式修改
HTML代码: <el-table :data="tableData" style="width: 100%" border :row-class-nam ...
- IOS开发基础之核心动画 基础动画、关键帧、组动画案例
// // ViewController.m // 30-核心动画 // // #import "ViewController.h" @interface ViewControll ...
- codec
1.codec究竟是什么 音频解码芯片:Audio codec,又叫声卡:本质就是ADC和DAC 2.codec的作用 在移动设备中,codec的作用可以归结为4种,分别如下: playback:对P ...
- leetcode medium 记录 1-50
# Title Solution 二刷 Difficulty 备注 2 Add Two Numbers X Medium 3 Longest Substring Wi ...
- Keil 报错解决方法:Cannot link object xxx.o as its attributes are incompatile with the image attributes
链接其他人的lib库时报错:Cannot link object xxx.o as its attributes are incompatile with the image attributes 解 ...
- University of Toronto Faculty of Arts and Science MAT344– Final Assessment Combinatorics Instructors: Stanislav Balchev and Max Klambauer 19 August 2020
目录 随便找的一份测试题 T7 T9 T6 T5 solution to (a) solution to (b) solution to (c) solution to (d) T1 T2 T3 T4 ...