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">原图片</ ...
随机推荐
- 洛谷 P1368 工艺 后缀自动机 求最小表示
后缀自动机沙茶题 将字符串复制一次,建立后缀自动机. 在后缀自动机上贪心走 $n$ 次即可. Code: #include <cstdio> #include <algorithm& ...
- 用树链剖分来写LCA
当两个点在一条链上,它们的LCA就是深度较小的那个点. 于是这种树链剖分写LCA的思想就是把要求的两个点想办法靠到一条链上. 而且要靠到尽量更优的一条链上(重链). 做法: 预处理出每棵树上的重链(s ...
- 装了ubuntu后笔记本电脑的无线网卡用不了,怎么设置?
百度经验的一篇文章 http://jingyan.baidu.com/article/ca2d939dd4f1b4eb6c31ce09.html 点击右上角的齿轮,选择“系统设置” 点击“软件和更 ...
- iptables指南
在了解iptables之前我们先了解一下 防火墙 的概念防火墙是由Check Point创立者Gil Shwed于1993年发明并引入国际互联网,防火墙也是一种位于内部网络与外部网络之间的网络安全系统 ...
- 【Henu ACM Round#16 E】Paths and Trees
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 感觉很像一道最短路. 然后就试了一发. 结果真的是.. 只要用一个优先队列优化的dijkstra算法求出每个点的最短路上的前一个点是 ...
- 【转】 HTML解析:基于XPath的C#类库HtmlAgiliytyPack
[转] HTML解析:基于XPath的C#类库HtmlAgiliytyPack 最近处于毕业设计开始阶段,前期工作需要去国外的一些专业数据库网站比对一些所需TF家族信息,为了快捷方便,想到用程序去帮助 ...
- [Python] Slice the data with pandas
For example we have dataframe like this: SPY AAPL IBM GOOG GLD 2017-01-03 222.073914 114.311760 160. ...
- Android资源之图像资源(状态图像资源)
在上一篇博文中.我主要解说了XML图像资源中的图层资源,在此图像资源博文中我会给大家陆续解说XMl图像资源的图像状态资源.图像级别资源.淡入淡出资源.嵌入图像资源.剪切图像资源和外形资源. 1.图像状 ...
- UINavigationBar的系统渲染方式
昨天想手工实现一下类知乎日报的Navigation Bar的动态颜色改变,但不管怎么设置Navigation Bar的 backgroundColor barTintColor alpha參数都达不到 ...
- Python爬虫爬取一篇韩寒新浪博客
网上看到大神对Python爬虫爬到非常多实用的信息,认为非常厉害.突然对想学Python爬虫,尽管自己没学过Python.但在网上找了一些资料看了一下,看到爬取韩寒新浪博客的视频.共三集,第一节讲爬取 ...