java高质量缩放图片的示例代码

 更新时间:2020年09月11日 15:32:04   作者:TianZe  
这篇文章主要介绍了java高质量缩放图片的示例代码,帮助大家更好的使用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);
		}
        //指定宽高
		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:P7.gif");
		 resize(originalImage, new File("c:P7-0.jpg"),150, 0.7f);
		 resize(originalImage, new File("c:P7-1.jpg"),150, 1f);
	}
}

以上就是java高质量缩放图片的示例代码的详细内容,更多关于Java 缩放图片的资料请关注脚本之家其它相关文章!

相关文章

  • Java启动命令大全(汇总)

    Java启动命令大全(汇总)

    Java启动命令是所有java应用程序的入口,通过它来启动Java运行时环境,并加载相关的class,本文希望做一个Java启动命令的汇总,和各位同道分享,也便于日后作为自己的参考
    2023-09-09
  • JDBC简介_动力节点Java学院整理

    JDBC简介_动力节点Java学院整理

    什么是JDBC?这篇文章就为大家详细介绍了Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • SpringBoot中的ExpiringMap代码实例

    SpringBoot中的ExpiringMap代码实例

    这篇文章主要介绍了SpringBoot中的ExpiringMap代码实例,ExpiringMap是一个可以设置过期策略、可变条目过期、延迟条目加载和过期侦听器的线程安全存储容器,需要的朋友可以参考下
    2023-08-08
  • Spring boot 运用策略模式实现避免多次使用if

    Spring boot 运用策略模式实现避免多次使用if

    这篇文章主要介绍了Spring boot 运用策略模式实现避免多次使用if,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • 一文教你学会搭建SpringBoot分布式项目

    一文教你学会搭建SpringBoot分布式项目

    这篇文章主要为大家详细介绍了搭建SpringBoot分布式项目的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-01-01
  • Java:String.split()特殊字符处理操作

    Java:String.split()特殊字符处理操作

    这篇文章主要介绍了Java:String.split()特殊字符处理操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • java selenium 操作浏览器实例

    java selenium 操作浏览器实例

    本文主要介绍java selenium 操作浏览器,这里整理了相关资料,并附示例代码,有需要的小伙伴可以参考下
    2016-08-08
  • java基于C/S模式实现聊天程序(客户端)

    java基于C/S模式实现聊天程序(客户端)

    这篇文章主要为大家详细介绍了java基于C/S模式实现聊天程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • javabean 中使用@Transient属性处理临时字段

    javabean 中使用@Transient属性处理临时字段

    @Transient表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性,本文给大家介绍javabean 中临时字段的处理:@Transient,感兴趣的朋友跟随小编一起看看吧
    2023-08-08
  • Servlet实现统计页面访问次数功能

    Servlet实现统计页面访问次数功能

    这篇文章主要介绍了Servlet实现统计页面访问次数功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04

最新评论