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">原图片</ ...
随机推荐
- Example of working with a dump.
Let's say that you are looking at a crash dump, so following the first command from this page you do ...
- 【Git 二】Windows|Mac 安装 Git
Windows 或 Mac 上安装 Git 相对于 Linux 上安装来说步骤是简便一些的.Linux 安装步骤见:[Git 一]Linux安装Git 一.Windows 安装 Git 直接下载对应 ...
- springboot整合redis,并解决乱码问题。
热烈推荐:超多IT资源,尽在798资源网 springboot 版本为 1.5.9 //如果是2.x 修改 pom.xml 也可切换成 1.5.9 <parent> <groupId ...
- BZOJ 2724 [Violet 6]蒲公英(分块)
题意 在线区间众数 思路 预处理出 f[i][j] 即从第 i 块到第 j 块的答案.对于每个询问,中间的整块直接用预处理出的,两端的 sqrtn 级别的数暴力做,用二分查找它们出现的次数.每次询问的 ...
- HDU 2253 Longest Common Subsequence Again
其实这个题我还不会,学长给了一个代码交上去过了,据说用到了一种叫做位压缩的技术,先贴代码吧,以后看懂了再来写 #include <stdio.h> #include <string. ...
- snowflake算法
snowflake算法思考 缘起 为什么会突然谈到分布式唯一id呢?原因是最近在准备使用RocketMQ,看看官网介绍: 一句话,消息可能会重复,所以消费端需要做幂等.为什么消息会重复后续Rocket ...
- Docker之基础篇
小白学Docker之基础篇 系列文章: 小白学Docker之基础篇 小白学Docker之Compose 小白学Docker之Swarm PS: 以下是个人作为新手小白学习docker的笔记总结 1 ...
- dubbo+zookeeper+springMVC +maven
pom: <dependency> <groupId>com.github.sgroschupf</grou ...
- vSphere5安装配置视频教程
vSphere5安装配置视频教程 本文出自 "李晨光原创技术博客" 博客,请务必保留此出处http://chenguang.blog.51cto.com/350944/819550
- ldd---程序所需要的动态链接库
ldd本身不是一个程序,而仅是一个shell脚本:ldd可以列出一个程序所需要得动态链接库(so) [root@xiaolizi ~ ]$ ldd /usr/bin/ls linux-vdso.so. ...