java 使用Thumbnailator图片处理包装类

Thumbnailator是一个非常好的图片开源工具,可以很好的完成图片处理。

从API提供现有的图像文件和图像对象的缩略图中简化了缩略过程,两三行代码就能够从现有图片生成缩略图,且允许微调缩略图生成,同时保持了需要写入到最低限度的代码量。同时还支持根据一个目录批量生成缩略图。

Maven中pom.xml 配置:

<dependency>
	<groupId>net.coobird</groupId>
	<artifactId>thumbnailator</artifactId>
	<version>0.4.8</version>
</dependency>

以下是包装类的代码:

package com.sun4j.app.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Position;

public class ThumbnailatorUtils {

	/**
	 * 指定大小进行缩放
	 * 若图片横比width小,高比height小,不变 若图片横比width小,高比height大,高缩小到height,图片比例不变
	 * 若图片横比width大,高比height小,横缩小到width,图片比例不变
	 * 若图片横比width大,高比height大,图片按比例缩小,横为width或高为height
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 */
	public static void ImgThumb(String source, String output, int width, int height) {
		try {
			Thumbnails.of(source).size(width, height).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 指定大小进行缩放
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 */
	public static void ImgThumb(File source, String output, int width, int height) {
		try {
			Thumbnails.of(source).size(width, height).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 按照比例进行缩放
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 */
	public static void ImgScale(String source, String output, double scale) {
		try {
			Thumbnails.of(source).scale(scale).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void ImgScale(File source, String output, double scale) {
		try {
			Thumbnails.of(source).scale(scale).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 不按照比例,指定大小进行缩放
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param keepAspectRatio
	 *            默认是按照比例缩放的,值为false 时不按比例缩放
	 */
	public static void ImgNoScale(String source, String output, int width, int height, boolean keepAspectRatio) {
		try {
			Thumbnails.of(source).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void ImgNoScale(File source, String output, int width, int height, boolean keepAspectRatio) {
		try {
			Thumbnails.of(source).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 旋转 ,正数:顺时针 负数:逆时针
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param rotate
	 *            角度
	 */
	public static void ImgRotate(String source, String output, int width, int height, double rotate) {
		try {
			Thumbnails.of(source).size(width, height).rotate(rotate).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void ImgRotate(File source, String output, int width, int height, double rotate) {
		try {
			Thumbnails.of(source).size(width, height).rotate(rotate).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 水印
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输入源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param position
	 *            水印位置 Positions.BOTTOM_RIGHT o.5f
	 * @param watermark
	 *            水印图片地址
	 * @param transparency
	 *            透明度 0.5f
	 * @param quality
	 *            图片质量 0.8f
	 */
	public static void ImgWatermark(String source, String output, int width, int height, Position position, String watermark, float transparency, float quality) {
		try {
			Thumbnails.of(source).size(width, height).watermark(position, ImageIO.read(new File(watermark)), transparency).outputQuality(0.8f).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void ImgWatermark(File source, String output, int width, int height, Position position, String watermark, float transparency, float quality) {
		try {
			Thumbnails.of(source).size(width, height).watermark(position, ImageIO.read(new File(watermark)), transparency).outputQuality(0.8f).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 裁剪图片
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param position
	 *            裁剪位置
	 * @param x
	 *            裁剪区域x
	 * @param y
	 *            裁剪区域y
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param keepAspectRatio
	 *            默认是按照比例缩放的,值为false 时不按比例缩放
	 */
	public static void ImgSourceRegion(String source, String output, Position position, int x, int y, int width, int height, boolean keepAspectRatio) {
		try {
			Thumbnails.of(source).sourceRegion(position, x, y).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void ImgSourceRegion(File source, String output, Position position, int x, int y, int width, int height, boolean keepAspectRatio) {
		try {
			Thumbnails.of(source).sourceRegion(position, x, y).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 按坐标裁剪
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param x
	 *            起始x坐标
	 * @param y
	 *            起始y坐标
	 * @param x1
	 *            结束x坐标
	 * @param y1
	 *            结束y坐标
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param keepAspectRatio
	 *            默认是按照比例缩放的,值为false 时不按比例缩放
	 */
	public static void ImgSourceRegion(String source, String output, int x, int y, int x1, int y1, int width, int height, boolean keepAspectRatio) {
		try {
			Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void ImgSourceRegion(File source, String output, int x, int y, int x1, int y1, int width, int height, boolean keepAspectRatio) {
		try {
			Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 转化图像格式
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param format
	 *            图片类型,gif、png、jpg
	 */
	public static void ImgFormat(String source, String output, int width, int height, String format) {
		try {
			Thumbnails.of(source).size(width, height).outputFormat(format).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void ImgFormat(File source, String output, int width, int height, String format) {
		try {
			Thumbnails.of(source).size(width, height).outputFormat(format).toFile(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 输出到OutputStream
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @return toOutputStream(流对象)
	 */
	public static OutputStream ImgOutputStream(String source, String output, int width, int height) {
		OutputStream os = null;
		try {
			os = new FileOutputStream(output);
			Thumbnails.of(source).size(width, height).toOutputStream(os);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return os;
	}

	public static OutputStream ImgOutputStream(File source, String output, int width, int height) {
		OutputStream os = null;
		try {
			os = new FileOutputStream(output);
			Thumbnails.of(source).size(width, height).toOutputStream(os);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return os;
	}

	/**
	 * 输出到BufferedImage
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param format
	 *            图片类型,gif、png、jpg
	 * @return BufferedImage
	 */
	public static BufferedImage ImgBufferedImage(String source, String output, int width, int height, String format) {
		BufferedImage buf = null;
		try {
			buf = Thumbnails.of(source).size(width, height).asBufferedImage();
			ImageIO.write(buf, format, new File(output));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buf;
	}

	public static BufferedImage ImgBufferedImage(File source, String output, int width, int height, String format) {
		BufferedImage buf = null;
		try {
			buf = Thumbnails.of(source).size(width, height).asBufferedImage();
			ImageIO.write(buf, format, new File(output));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buf;
	}
}

使用此类基本可以完成图片的裁剪、缩略图、水印、图片格式转换、输出流对象等操作!

参考地址:http://rensanning.iteye.com/blog/1545708

时间: 2024-08-29 20:39:07

java 使用Thumbnailator图片处理包装类的相关文章

Java部分总结图片版2(已加上原图链接!!!)

Java部分总结图片版2(加上原图链接)

java 获取网络地址图片

收藏一个获取网络图片的方法. 1 //获取网络图片 2 public void ImageRequest(String ImageName,String GifUrl) throws Exception { 3 //new一个URL对象 4 URL url = new URL(GifUrl); 5 //打开链接 6 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 7 //设置请求方式为"GET" 8 c

coco2dx jni 调用 java 相机返回 图片数据

新建 一个项目 名字:testJin  包名:com.TanSon.org  python命令:python create_project.py -project testJin -package com.TanSon.org -language cpp eclipse 导入项目配置 ... 略去,(可以google) 1 c++ 调用 andriod 1.1 包含头文件 #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include <jni.

java blob存取图片

1. 存入图片 Connection con=db.conn; PreparedStatement pstmt; //File file = upload; FileInputStream inputImage = new FileInputStream(upload); byte[] buf = new byte[inputImage.available()]; inputImage.read(buf); con.setAutoCommit(false); System.out.println

Java部分总结图片版(已经加上原图链接下载!!!)

Java基础知识图片版(原图下载链接)

java实现的图片缩放 压缩 裁剪工具!找了很久,市面上再也找不到比它缩放效果还好的代码了

原文:java实现的图片缩放 压缩 裁剪工具!找了很久,市面上再也找不到比它缩放效果还好的代码了 源代码下载地址:http://www.zuidaima.com/share/1550463380458496.htm 纯 java 实现的 图片缩放 压缩 裁剪工具!不依赖任何第三方 jar 包 1. 找了很久,市面上再也找不到比它缩放效果还好的代码了 (再不使用任何第三方组件的前提下) 2. 支持缩放 3. 支持剪切 (例如:用户上传头像后剪切成正方形小图) /* * Copyright 2012

Java中基本数据类型和包装类

参考:深入剖析Java中的装箱和拆箱; Java中基本数据类型和包装类互转中 缓冲机制的使用; java学习笔记:装箱和拆箱,包装器和缓冲池 Java 各 类型数据在内存中分配情况详解 一 java内存分配 这里只是在网上找的一些资料; Java 中的数据类型分为 1. 基本类型(原始数据类型) byte short int long float double char boolean 基本类型的变量持有原始值. 2. 符合数据类型(引用类型),引用类型持有引用值(即对某个对象的引用,而非对象本

Java处理JPEG图片时,需要导入com.sun.image.codec.jpeg.JPEGImageEn,报错处理

Java处理JPEG图片时,需要导入com.sun.image.codec.jpeg.JPEGImageEn,会报错,不能使用相应的方法. 原因:java访问限制级api的时候,默认的eclipse设置会报错,现在更改只警告,不报错,就可以使用了.. 默认把这些访问受限的API当成了错误来处理. 解决办法: 在MyEclipse中点Window-->Preferences-->Java-->Compiler-->Errors/Warnings,展开Deprecated and re

利用java实现简单图片的计数器

利用java实现简单图片的计数器,运行图: 想学习更多关于java的知识,可以点击<Java EE软件工程师>进行学习. <%@ page contentType="text/html;charset=gb2312"%><%@ page language="java" import="java.io.*"%> <html><head><meta http-equiv="C