java高质量缩放图片
可按照比例缩放,也可以指定宽高
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.Kernel;
import java.awt.image.ConvolveOp; public class ImageUtil {
/**
*
* @param originalFile 原文件
* @param resizedFile 压缩目标文件
* @param newWidth 压缩后的图片宽度
* @param quality 压缩质量(0到1之间,越高质量越好)
* @throws IOException
*/ public static void resize(File originalFile, File resizedFile,
int newWidth, float quality) throws IOException { if (quality > 1) {
throw new IllegalArgumentException(
"Quality has to be between 0 and 1");
} ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null; int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);
//比例缩放
if (iWidth > iHeight) {
resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
/ iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,
newWidth, Image.SCALE_SMOOTH);
}
//指定宽高
//resizedImage = i.getScaledInstance(560,315, Image.SCALE_SMOOTH);
// This code ensures that all the pixels in the image are loaded.
Image temp = new ImageIcon(resizedImage).getImage(); // Create the buffered image.
BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
temp.getHeight(null), BufferedImage.TYPE_INT_RGB); // Copy image to buffered image.
Graphics g = bufferedImage.createGraphics(); // Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose(); // Soften.
float softenFactor = 0.05f;
float[] softenArray = { 0, softenFactor, 0, softenFactor,
1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null); // Write the jpeg to a file.
FileOutputStream out = new FileOutputStream(resizedFile); // Encodes image as a JPEG data stream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(bufferedImage); param.setQuality(quality, true); encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);
} // Example usage public static void main(String[] args) throws IOException {
File originalImage = new File("C:\1207.gif");
resize(originalImage, new File("c:\1207-0.jpg"),150, 0.7f);
resize(originalImage, new File("c:\1207-1.jpg"),150, 1f);
}
}
参考博文:https://www.wanpishe.top/detail?blogId=f80863fd-f8d5-4da0-8f4e-6537ba13bd91
java高质量缩放图片的更多相关文章
- C#剪切生成高质量缩放图片
/// <summary> /// 高质量缩放图片 /// </summary> /// <param name="OriginFilePath"&g ...
- 移动Web—CSS为Retina屏幕替换更高质量的图片
来源:互联网 作者:佚名 时间:12-24 10:37:45 [大 中 小] 点评:Retian似乎是屏幕显示的一种趋势,这也是Web设计师面对的一个新挑战;移动应用程序的设计师们已经学会了如何为Re ...
- 使用 FFmpeg 处理高质量 GIF 图片
使用 FFmpeg 处理高质量 GIF 图片 - 为程序员服务 http://ju.outofmemory.cn/entry/169845
- C# GDI生成清晰【高质量】图片
对于GDI+,在正常的操作,Bitmap,Graphcis,DrawImage或者DrawString ,生成图片的话,会产生很多杂点,或者是图片质量不稳定.尤其是在读取图片后,生成缩略图之后,文件会 ...
- Python高质量缩放切图,抗锯齿
最近刚接触Python,以迅雷不及掩耳盗铃之势(只是迫不及待)应用到工作中去了之前用 cmd+photoshop做批量图像处理(缩放切片),在执行效率(速度)上和灵活度上有很大限制,遂转战Python ...
- java图片高质量缩放类
import java.awt.Color;import java.awt.Graphics;import java.awt.Image;import java.awt.image.BufferedI ...
- 【Java】图片高质量缩放类
package com.test; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.image.codec.jpeg. ...
- c#无损高质量压缩图片
这几天在做同城交友网www.niyuewo.com时遇到的一个问题,如何将会员的头像压缩,在网上搜索整理如下:在此也感谢医药精(www.yiyaojing.com)站长的帮忙 /// <summ ...
- c# 无损高质量压缩图片代码
/// <summary> /// 无损压缩图片 /// </summary> /// <param name="sFile">原图片</ ...
随机推荐
- 题解 CF911D 【Inversion Counting】
这是一道看似复杂其实也不简单的思维题. 其实思路很明显. 因为这道题的数据范围比较大,有1e5的询问,如果暴力(像我考场上那样打平衡树)的话可以做到$mnlogn$. 但那样也是稳T. 经过思考之后我 ...
- java回调方法、钩子方法以及模板方法模式
在面向对象的语言中,回调则是通过接口或抽象类来实现的,我们把实现这种接口的类称为回调类,回调类的对象称为回调对象,其处理事件的方法叫做回调方法.(摘自百度百科) 那么通过上面那句话将百度百科中的&qu ...
- spring定时器中如何获取servletcontext
spring定时器中如何获取servletcontext 学习了:https://zhidao.baidu.com/question/406212574.html @Scheduled(cron = ...
- Bitmap缓存机制
Bitmap缓存机制 载入一个bitmap到UI里面比較简单直接.可是,假设我们一次载入大量的bitmap数据的时候就变得复杂了.很多情况下(比方这些组件:ListVIew,GridView或者Vie ...
- [Java][log4j]支持同一时候按日期和文件大小切割日志
依据DailyRollingFileAppender和RollingFileAppender改编,支持按日期和文件大小切割日志. 源文件: package com.bao.logging; impo ...
- BZOJ5204: [CodePlus 2018 3 月赛]投票统计
[传送门:BZOJ5204] 简要题意: 有n个选手,每个选手会选择一道题投票,求出投票最多的题目个数和这些题目的编号,如果所有题目的投票数相同,则输出-1 题解: 直接搞 离散化,然后判断就可以了 ...
- Dom4j 查找节点或属性
Dom4j 查找节点或属性 例如 1 查找下面xml中的student节点的age属性, xpathstr="/students/student/@age"; 2 查找下面xml ...
- 使用java自带的xml解析器解析xml
使用java自带的xml解析器解析xml,其实我不推荐,可以用Dom解析,或其他的方式,因为Java自带的解析器使用不但麻烦,且还有bug出现. 它要求,针对不同的xml(结构不同),必须写对应的ha ...
- JS对象继承与原型链
1.以复制方式实现的继承 1.1浅拷贝 基本类型的复制 var parent = { lanage: "chinese" } var child = { name: "x ...
- js002---- 标准内置对象
1. js全局的对象(全局作用域里的对象,而不是全局对象), 或者叫标准内置对象 2, 全局对象 是一个Global类的对象. 标准内置对象的分类: 1. 值属性 infinity Na ...