图片处理工具类 - ImageUtils.java
纯JAVA实现的图片处理工具类,提供图片的裁剪、压缩、获取尺寸、制作圆角等方法。
源码如下:(点击下载 -ImageUtils.java 、
FolderUtils.java 、
commons-io-2.4.jar
、commons-lang-2.6.jar)
- import java.awt.AlphaComposite;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.RenderingHints;
- import java.awt.geom.RoundRectangle2D;
- import java.awt.image.BufferedImage;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URL;
- import java.util.Iterator;
- import javax.imageio.IIOImage;
- import javax.imageio.ImageIO;
- import javax.imageio.ImageWriteParam;
- import javax.imageio.ImageWriter;
- import javax.imageio.stream.ImageOutputStream;
- import org.apache.commons.io.FilenameUtils;
- import org.apache.commons.io.IOUtils;
- import org.apache.commons.lang.StringUtils;
- /**
- * 纯JAVA实现的图片处理工具类
- *
- */
- public class ImageUtils {
- /**
- * 获取图片尺寸信息
- *
- * @param filePath
- * a {@link java.lang.String} object.
- * @return [width, height]
- */
- public static int[] getSizeInfo(String filePath) throws Exception {
- File file = new File(filePath);
- return getSizeInfo(file);
- }
- /**
- * 获取图片尺寸信息
- *
- * @param url
- * a {@link java.net.URL} object.
- * @return [width,height]
- */
- public static int[] getSizeInfo(URL url) throws Exception {
- InputStream input = null;
- try {
- input = url.openStream();
- return getSizeInfo(input);
- } catch (IOException e) {
- e.printStackTrace();
- throw new Exception(e);
- } finally {
- IOUtils.closeQuietly(input);
- }
- }
- /**
- * 获取图片尺寸信息
- *
- * @param file
- * a {@link java.io.File} object.
- * @return [width,height]
- */
- public static int[] getSizeInfo(File file) throws Exception {
- if (!file.exists()) {
- throw new Exception("file " + file.getAbsolutePath() + " doesn't exist.");
- }
- BufferedInputStream input = null;
- try {
- input = new BufferedInputStream(new FileInputStream(file));
- return getSizeInfo(input);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- throw new Exception(e);
- } finally {
- IOUtils.closeQuietly(input);
- }
- }
- /**
- * 获取图片尺寸
- *
- * @param input
- * a {@link java.io.InputStream} object.
- * @return [width,height]
- */
- public static int[] getSizeInfo(InputStream input) throws Exception {
- try {
- BufferedImage img = ImageIO.read(input);
- int w = img.getWidth(null);
- int h = img.getHeight(null);
- return new int[] { w, h };
- } catch (IOException e) {
- e.printStackTrace();
- throw new Exception(e);
- }
- }
- /**
- * 重调图片尺寸
- *
- * @param srcFilePath
- * 原图路径
- * @param destFile
- * 目标文件
- * @param width
- * 新的宽度,小于1则忽略,按原图比例缩放
- * @param height
- * 新的高度,小于1则忽略,按原图比例缩放
- */
- public static void resize(String srcFilePath, String destFile, int width, int height) throws Exception {
- resize(srcFilePath, destFile, width, height, -1, -1);
- }
- /**
- * 重调图片尺寸
- *
- * @param input
- * a {@link java.io.InputStream} object.
- * @param output
- * a {@link java.io.OutputStream} object.
- * @param width
- * a int.
- * @param height
- * a int.
- */
- public static void resize(InputStream input, OutputStream output, int width, int height) throws Exception {
- resize(input, output, width, height, -1, -1);
- }
- /**
- * 重调图片尺寸
- *
- * @param input
- * a {@link java.io.InputStream} object.
- * @param output
- * a {@link java.io.OutputStream} object.
- * @param width
- * a int.
- * @param height
- * a int.
- * @param maxWidth
- * a int.
- * @param maxHeight
- * a int.
- */
- public static void resize(InputStream input, OutputStream output,
- int width, int height, int maxWidth, int maxHeight) throws Exception {
- if (width < 1 && height < 1 && maxWidth < 1 && maxHeight < 1) {
- try {
- IOUtils.copy(input, output);
- } catch (IOException e) {
- throw new Exception("resize error: ", e);
- }
- }
- try {
- BufferedImage img = ImageIO.read(input);
- boolean hasNotAlpha = !img.getColorModel().hasAlpha();
- double w = img.getWidth(null);
- double h = img.getHeight(null);
- int toWidth;
- int toHeight;
- double rate = w / h;
- if (width > 0 && height > 0) {
- rate = ((double) width) / ((double) height);
- toWidth = width;
- toHeight = height;
- } else if (width > 0) {
- toWidth = width;
- toHeight = (int) (toWidth / rate);
- } else if (height > 0) {
- toHeight = height;
- toWidth = (int) (toHeight * rate);
- } else {
- toWidth = ((Number) w).intValue();
- toHeight = ((Number) h).intValue();
- }
- if (maxWidth > 0 && toWidth > maxWidth) {
- toWidth = maxWidth;
- toHeight = (int) (toWidth / rate);
- }
- if (maxHeight > 0 && toHeight > maxHeight) {
- toHeight = maxHeight;
- toWidth = (int) (toHeight * rate);
- }
- BufferedImage tag = new BufferedImage(toWidth, toHeight, hasNotAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB);
- // Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
- tag.getGraphics().drawImage(img.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH), 0, 0, null);
- ImageIO.write(tag, hasNotAlpha ? "jpg" : "png", output);
- } catch (Exception e) {
- e.printStackTrace();
- throw new Exception(e);
- } finally {
- IOUtils.closeQuietly(input);
- IOUtils.closeQuietly(output);
- }
- }
- /**
- * 重调图片尺寸
- *
- * @param srcFile
- * 原图路径
- * @param destFile
- * 目标文件
- * @param width
- * 新的宽度,小于1则忽略,按原图比例缩放
- * @param height
- * 新的高度,小于1则忽略,按原图比例缩放
- * @param maxWidth
- * 最大宽度,限制目标图片宽度,小于1则忽略此设置
- * @param maxHeight
- * 最大高度,限制目标图片高度,小于1则忽略此设置
- */
- public static void resize(String srcFile, String destFile, int width,
- int height, int maxWidth, int maxHeight) throws Exception {
- resize(new File(srcFile), new File(destFile), width, height, maxWidth, maxHeight);
- }
- /**
- * 重调图片尺寸
- *
- * @param srcFile
- * 原图路径
- * @param destFile
- * 目标文件
- * @param width
- * 新的宽度,小于1则忽略,按原图比例缩放
- * @param height
- * 新的高度,小于1则忽略,按原图比例缩放
- */
- public static void resize(File srcFile, File destFile, int width, int height) throws Exception {
- resize(srcFile, destFile, width, height, -1, -1);
- }
- /**
- * 重调图片尺寸
- *
- * @param srcFile
- * 原图路径
- * @param destFile
- * 目标文件
- * @param width
- * 新的宽度,小于1则忽略,按原图比例缩放
- * @param height
- * 新的高度,小于1则忽略,按原图比例缩放
- * @param maxWidth
- * 最大宽度,限制目标图片宽度,小于1则忽略此设置
- * @param maxHeight
- * 最大高度,限制目标图片高度,小于1则忽略此设置
- */
- public static void resize(File srcFile, File destFile, int width,
- int height, int maxWidth, int maxHeight) throws Exception {
- if (destFile.exists()) {
- destFile.delete();
- } else {
- FolderUtils.mkdirs(destFile.getParent());
- }
- InputStream input = null;
- OutputStream output = null;
- try {
- input = new BufferedInputStream(new FileInputStream(srcFile));
- output = new FileOutputStream(destFile);
- resize(input, output, width, height, maxWidth, maxHeight);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- throw new Exception(e);
- } finally {
- IOUtils.closeQuietly(input);
- IOUtils.closeQuietly(output);
- }
- }
- /**
- * 裁剪图片
- *
- * @param source
- * a {@link java.lang.String} object.
- * @param target
- * a {@link java.lang.String} object.
- * @param x
- * a int.
- * @param y
- * a int.
- * @param w
- * a int.
- * @param h
- * a int.
- */
- public static void crop(String source, String target, int x, int y, int w, int h) throws Exception {
- crop(new File(source), new File(target), x, y, w, h);
- }
- /**
- * 裁剪图片
- *
- * @param source
- * a {@link java.io.File} object.
- * @param target
- * a {@link java.io.File} object.
- * @param x
- * a int.
- * @param y
- * a int.
- * @param w
- * a int.
- * @param h
- * a int.
- */
- public static void crop(File source, File target, int x, int y, int w, int h) throws Exception {
- OutputStream output = null;
- InputStream input = null;
- String ext = FilenameUtils.getExtension(target.getName());
- try {
- input = new BufferedInputStream(new FileInputStream(source));
- if (target.exists()) {
- target.delete();
- } else {
- FolderUtils.mkdirs(target.getParent());
- }
- output = new BufferedOutputStream(new FileOutputStream(target));
- } catch (IOException e) {
- throw new Exception(e);
- }
- crop(input, output, x, y, w, h, StringUtils.equalsIgnoreCase("png", ext));
- }
- /**
- * 裁剪图片
- *
- * @param x
- * a int.
- * @param y
- * a int.
- * @param w
- * a int.
- * @param h
- * a int.
- * @param input
- * a {@link java.io.InputStream} object.
- * @param output
- * a {@link java.io.OutputStream} object.
- * @param isPNG
- * a boolean.
- */
- public static void crop(InputStream input, OutputStream output, int x,
- int y, int w, int h, boolean isPNG) throws Exception {
- try {
- BufferedImage srcImg = ImageIO.read(input);
- int tmpWidth = srcImg.getWidth();
- int tmpHeight = srcImg.getHeight();
- int xx = Math.min(tmpWidth - 1, x);
- int yy = Math.min(tmpHeight - 1, y);
- int ww = w;
- if (xx + w > tmpWidth) {
- ww = Math.max(1, tmpWidth - xx);
- }
- int hh = h;
- if (yy + h > tmpHeight) {
- hh = Math.max(1, tmpHeight - yy);
- }
- BufferedImage dest = srcImg.getSubimage(xx, yy, ww, hh);
- BufferedImage tag = new BufferedImage(w, h, isPNG ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
- tag.getGraphics().drawImage(dest, 0, 0, null);
- ImageIO.write(tag, isPNG ? "png" : "jpg", output);
- } catch (Exception e) {
- e.printStackTrace();
- throw new Exception(e);
- } finally {
- IOUtils.closeQuietly(input);
- IOUtils.closeQuietly(output);
- }
- }
- /**
- * 压缩图片,PNG图片按JPG处理
- *
- * @param input
- * a {@link java.io.InputStream} object.
- * @param output
- * a {@link java.io.OutputStream} object.
- * @param quality
- * 图片质量0-1之间
- */
- public static final void optimize(InputStream input, OutputStream output, float quality) throws Exception {
- // create a BufferedImage as the result of decoding the supplied
- // InputStream
- BufferedImage image;
- ImageOutputStream ios = null;
- ImageWriter writer = null;
- try {
- image = ImageIO.read(input);
- // get all image writers for JPG format
- Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg");
- if (!writers.hasNext())
- throw new IllegalStateException("No writers found");
- writer = (ImageWriter) writers.next();
- ios = ImageIO.createImageOutputStream(output);
- writer.setOutput(ios);
- ImageWriteParam param = writer.getDefaultWriteParam();
- // optimize to a given quality
- param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
- param.setCompressionQuality(quality);
- // appends a complete image stream containing a single image and
- // associated stream and image metadata and thumbnails to the output
- writer.write(null, new IIOImage(image, null, null), param);
- } catch (IOException e) {
- e.printStackTrace();
- throw new Exception(e);
- } finally {
- if (ios != null) {
- try {
- ios.close();
- } catch (IOException e) {
- e.printStackTrace();
- throw new Exception(e);
- }
- }
- writer.dispose();
- }
- }
- /**
- * 压缩图片
- *
- * @param source
- * a {@link java.lang.String} object.
- * @param target
- * a {@link java.lang.String} object.
- * @param quality
- * a float.
- */
- public static final void optimize(String source, String target, float quality) throws Exception {
- File fromFile = new File(source);
- File toFile = new File(target);
- optimize(fromFile, toFile, quality);
- }
- /**
- * 压缩图片
- *
- * @param source
- * a {@link java.io.File} object.
- * @param target
- * a {@link java.io.File} object.
- * @param quality
- * 图片质量0-1之间
- */
- public static final void optimize(File source, File target, float quality) throws Exception {
- if (target.exists()) {
- target.delete();
- } else {
- FolderUtils.mkdirs(target.getParent());
- }
- InputStream is = null;
- OutputStream os = null;
- try {
- is = new BufferedInputStream(new FileInputStream(source));
- os = new BufferedOutputStream(new FileOutputStream(target));
- optimize(is, os, quality);
- } catch (FileNotFoundException e) {
- throw new Exception(e);
- } finally {
- IOUtils.closeQuietly(is);
- IOUtils.closeQuietly(os);
- }
- }
- /**
- * 制作圆角
- *
- * @param srcFile
- * 原文件
- * @param destFile
- * 目标文件
- * @param cornerRadius
- * 角度
- */
- public static void makeRoundedCorner(File srcFile, File destFile, int cornerRadius) throws Exception {
- InputStream in = null;
- OutputStream out = null;
- try {
- in = new BufferedInputStream(new FileInputStream(srcFile));
- FolderUtils.mkdirs(destFile.getParentFile().getAbsolutePath());
- out = new BufferedOutputStream(new FileOutputStream(destFile));
- makeRoundedCorner(in, out, cornerRadius);
- } catch (IOException e) {
- e.printStackTrace();
- throw new Exception(e);
- } finally {
- IOUtils.closeQuietly(out);
- IOUtils.closeQuietly(in);
- }
- }
- /**
- * 制作圆角
- *
- * @param srcFile
- * 原文件
- * @param destFile
- * 目标文件
- * @param cornerRadius
- * 角度
- */
- public static void makeRoundedCorner(String srcFile, String destFile, int cornerRadius) throws Exception {
- makeRoundedCorner(new File(srcFile), new File(destFile), cornerRadius);
- }
- /**
- * 制作圆角
- *
- * @param inputStream
- * 原图输入流
- * @param outputStream
- * 目标输出流
- * @param radius
- * 角度
- */
- public static void makeRoundedCorner(final InputStream inputStream,
- final OutputStream outputStream, final int radius) throws Exception {
- BufferedImage sourceImage = null;
- BufferedImage targetImage = null;
- try {
- sourceImage = ImageIO.read(inputStream);
- int w = sourceImage.getWidth();
- int h = sourceImage.getHeight();
- System.out.println(w);
- int cornerRadius = radius < 1 ? w / 4 : radius;
- targetImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
- Graphics2D g2 = targetImage.createGraphics();
- // This is what we want, but it only does hard-clipping, i.e.
- // aliasing
- // g2.setClip(new RoundRectangle2D ...)
- // so instead fake soft-clipping by first drawing the desired clip
- // shape
- // in fully opaque white with antialiasing enabled...
- g2.setComposite(AlphaComposite.Src);
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g2.setColor(Color.WHITE);
- g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));
- // ... then compositing the image on top,
- // using the white shape from above as alpha source
- g2.setComposite(AlphaComposite.SrcAtop);
- g2.drawImage(sourceImage, 0, 0, null);
- g2.dispose();
- ImageIO.write(targetImage, "png", outputStream);
- } catch (IOException e) {
- e.printStackTrace();
- throw new Exception(e);
- }
- }
- }
图片处理工具类 - ImageUtils.java的更多相关文章
- 验证码图片生成工具类——Captcha.java
验证码图片生成工具,使用JAVA生成的图片验证码,调用getRandcode方法获取图片验证码,以流的方式传输到前端页面. 源码如下:(点击下载 Captcha.java) import java. ...
- Java操作图片的工具类
操作图片的工具类: import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.a ...
- java图片处理工具类
直接上代码: package com.zxd.tool; /** * Created by zhang on 14-3-1. * 图片的常用操作类 */ import java.awt.AlphaCo ...
- Java的图片处理工具类
import Java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- 【转】java缩放图片、java裁剪图片代码工具类
一首先看下效果 二工具类 三测试类 在系统的上传图片功能中,我们无法控制用户上传图片的大小,用户可能会上传大到几十M小到1k的的图片,一方面图片太大占据了太多的空间,另一方面,我们没办法在页面上显示统 ...
- Java常用工具类---image图片处理工具类、Json工具类
package com.jarvis.base.util; import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStre ...
- 图片水印工具类java
关于jar包的导入我就不多说了,我会把引入的jar包显示出来,大家自行Google package com.net.util; import java.awt.AlphaComposite; impo ...
- JavaUtil_03_图片处理工具类
一.源码 功能:缩放图像.切割图像.图像类型转换.彩色转黑白.文字水印.图片水印等 package com.ray.dingtalk.util; import java.awt.AlphaCompos ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
随机推荐
- Hibernate中启用日志
Problem How do you determine what SQL query is being executed by Hibernate? How can you see the Hibe ...
- 1.C#基础篇-->封装、继承和多态
面向对象三要素:封装.继承和多态.正确理解这三个要素,才能在编程中建立面向对象的思想. 1.封装使用篇 作用:好的封装增加代码的可读性,易于维护. 什么情况下使用封装,封装的原则是? 1>功能相 ...
- JS 学习笔记--JS中的事件对象基础
事件:JavaScript中的事件是由访问web页面用户的一系列操作引起的,比如点击鼠标,键盘按键等.当用户执行某些操作的时候再去执行一些代码. 事件模型:内联模型.脚本模型.DOM2模型 内联模型: ...
- SQL Server表分区案例
--学习创建表分区脚本/*SQL SERVER 2005中以上版本,终于引入了表分区,就是说,当一个表里的数据很多时,可以将其分拆到多个的表里,大大提高了性能.下面举例子说明之*/ --------- ...
- 学习NAnt Build .CS+Solution+MSBuild+SVN+NUnit+NUnitReport
NAnt help:http://nant.sourceforge.net/release/latest/help/tasks/NAntContrib help:http://nantcontrib. ...
- js中常用数组方法concat join push pop slice splice shift
javascript给我们很多常用的 数组方法,极大方便了我们做程序.下面我们来介绍下常用的集中数组方法. 比如 concat() join() push() pop() unshift() shif ...
- *args和**kw魔法参数
学Python挺久了,现在才搞懂这个还是有点惭愧 *args:传入元组,无关键字 **kw:传入字典,有关键字 示例: *args **kw 一起使用时args的参数需在前:
- virtualenv 环境下 Nginx + Flask + Gunicorn+ Supervisor 搭建 Python Web
在这篇文章里,我们将搭建一个简单的 Web 应用,在虚拟环境中基于 Flask 框架,用 Gunicorn 做 wsgi 容器,用 Supervisor 管理进程,然后使用 Python 探针来监测应 ...
- post 方式提交XML文件调用接口
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Date; import java. ...
- KMP高质量代码实现详解
KMP算法 对于KMP算法我分为两个部分说明,第一部分是算法部分,介绍KMP算法的算法思想:第二部分是实现部分,介绍一种厉害的实现代码以及代码注释.当然了由于本文主要介绍怎么实现故而先分析实现,对KM ...