可按照比例缩放,也可以指定宽高

 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高质量缩放图片的更多相关文章

  1. C#剪切生成高质量缩放图片

    /// <summary> /// 高质量缩放图片 /// </summary> /// <param name="OriginFilePath"&g ...

  2. 移动Web—CSS为Retina屏幕替换更高质量的图片

    来源:互联网 作者:佚名 时间:12-24 10:37:45 [大 中 小] 点评:Retian似乎是屏幕显示的一种趋势,这也是Web设计师面对的一个新挑战;移动应用程序的设计师们已经学会了如何为Re ...

  3. 使用 FFmpeg 处理高质量 GIF 图片

    使用 FFmpeg 处理高质量 GIF 图片 - 为程序员服务  http://ju.outofmemory.cn/entry/169845

  4. C# GDI生成清晰【高质量】图片

    对于GDI+,在正常的操作,Bitmap,Graphcis,DrawImage或者DrawString ,生成图片的话,会产生很多杂点,或者是图片质量不稳定.尤其是在读取图片后,生成缩略图之后,文件会 ...

  5. Python高质量缩放切图,抗锯齿

    最近刚接触Python,以迅雷不及掩耳盗铃之势(只是迫不及待)应用到工作中去了之前用 cmd+photoshop做批量图像处理(缩放切片),在执行效率(速度)上和灵活度上有很大限制,遂转战Python ...

  6. java图片高质量缩放类

    import java.awt.Color;import java.awt.Graphics;import java.awt.Image;import java.awt.image.BufferedI ...

  7. 【Java】图片高质量缩放类

    package com.test; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.image.codec.jpeg. ...

  8. c#无损高质量压缩图片

    这几天在做同城交友网www.niyuewo.com时遇到的一个问题,如何将会员的头像压缩,在网上搜索整理如下:在此也感谢医药精(www.yiyaojing.com)站长的帮忙 /// <summ ...

  9. c# 无损高质量压缩图片代码

    /// <summary> /// 无损压缩图片 /// </summary> /// <param name="sFile">原图片</ ...

随机推荐

  1. 洛谷1019 单词接龙 字符串dfs

    问题描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...

  2. [洛谷P2245]星际导航

    题目大意:有一张n点m边的带权无向图,和一些问题,每次询问两个点之间的路径的最大边权最小是多少. 解题思路:同NOIP2013货车运输,只是数据增大,大变成小,小变成大了而已.所以具体思路见货车运输. ...

  3. opencv——均值/中值滤波器去噪

    实验内容及实验原理: 1.用均值滤波器(即邻域平均法)去除图像中的噪声: 2.用中值滤波器去除图像中的噪声 3.比较两种方法的处理结果 实验步骤: 用原始图像lena.bmp或cameraman.bm ...

  4. Spring学习总结(13)——Spring+Log4j+ActiveMQ实现远程记录日志

    应用场景 随着项目的逐渐扩大,日志的增加也变得更快.Log4j是常用的日志记录工具,在有些时候,我们可能需要将Log4j的日志发送到专门用于记录日志的远程服务器,特别是对于稍微大一点的应用.这么做的优 ...

  5. struts2.x + Tiles2.x读取多个xml 配置文件

    在web.xml中配置如下即可: <context-param> <param-name>org.apache.tiles.impl.BasicTilesContainer.D ...

  6. 查看SQLSERVER当前正在运行的sql信息

    能够使用SQL Profiler捕捉在SQL Server实例上运行的活动.这种活动被称为Profiler跟踪.这个就不多说了,大家都知道,以下是使用代码为实现同样的效果. SET TRANSACTI ...

  7. xcode 4 svn配置(host is unreachable)

    xcode 4 svn配置 先保证你的xcode中已经安装了command line tools xcode -> preferences -> downloads -> comma ...

  8. vue17 $watch 监听数据变化

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Mysql基础部分,针对以后python使用

    #redis 非关系型数据库#mysql 关系型数据库 表与表之间有数据关系 Oracle Mysql SqlServer DB2#多张表组合在一起就是数据库#冗余 存储两倍数据 可以使系统速度更快 ...

  10. [ DB ] [ SQL ] [ SQL Server ] MS SQL 建立暫存表格 temp table - 轉載

    範例 SQL: IF OBJECT_ID(N'tempdb.dbo.#tmp_checkStatusCount', N'U') IS NOT NULL DROP TABLE #tmp_checkSta ...