在实际项目中,我们经常会遇到处理各种各样的图片问题。
比如:图片的旋转、缩放、图片格式转换、获取图片类型、验证图片大小、写入图片
等。
这里我们使用java.awt.Graphics2D来实现常用图像处理的功能,形成我们的图像处理工具类。
package com.zhangsx.util.image;
import java.util.Iterator;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
/**
* 图像处理工具类。
*
* @version 1.00 2010-1-15
* @since 1.5
* @author ZhangShixi
*/
public class ImageUtil {
/**
* 旋转图像。
* @param bufferedImage 图像。
* @param degree 旋转角度。
* @return 旋转后的图像。
*/
public static BufferedImage rotateImage(
final BufferedImage bufferedImage, final int degree) {
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
int type = bufferedImage.getColorModel().getTransparency();
BufferedImage image = new BufferedImage(width, height, type);
Graphics2D graphics2D = image.createGraphics();
graphics2D.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.rotate(Math.toRadians(degree), width / 2, height / 2);
graphics2D.drawImage(bufferedImage, 0, 0, null);
try {
return image;
} finally {
if (graphics2D != null) {
graphics2D.dispose();
}
}
}
/**
* 将图像按照指定的比例缩放。
* 比如需要将图像放大20%,那么调用时scale参数的值就为20;如果是缩小,则scale值为-20。
* @param bufferedImage 图像。
* @param scale 缩放比例。
* @return 缩放后的图像。
*/
public static BufferedImage resizeImageScale(
final BufferedImage bufferedImage, final int scale) {
if (scale == 0) {
return bufferedImage;
}
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
int newWidth = 0;
int newHeight = 0;
double nowScale = (double) Math.abs(scale) / 100;
if (scale > 0) {
newWidth = (int) (width * (1 + nowScale));
newHeight = (int) (height * (1 + nowScale));
} else if (scale < 0) {
newWidth = (int) (width * (1 - nowScale));
newHeight = (int) (height * (1 - nowScale));
}
return resizeImage(bufferedImage, newWidth, newHeight);
}
/**
* 将图像缩放到指定的宽高大小。
* @param bufferedImage 图像。
* @param width 新的宽度。
* @param height 新的高度。
* @return 缩放后的图像。
*/
public static BufferedImage resizeImage(
final BufferedImage bufferedImage,
final int width, final int height) {
int type = bufferedImage.getColorModel().getTransparency();
BufferedImage image = new BufferedImage(width, height, type);
Graphics2D graphics2D = image.createGraphics();
graphics2D.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(bufferedImage, 0, 0, width, height, 0, 0,
bufferedImage.getWidth(), bufferedImage.getHeight(), null);
try {
return image;
} finally {
if (graphics2D != null) {
graphics2D.dispose();
}
}
}
/**
* 将图像水平翻转。
* @param bufferedImage 图像。
* @return 翻转后的图像。
*/
public static BufferedImage flipImage(
final BufferedImage bufferedImage) {
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
int type = bufferedImage.getColorModel().getTransparency();
BufferedImage image = new BufferedImage(width, height, type);
Graphics2D graphics2D = image.createGraphics();
graphics2D.drawImage(bufferedImage, 0, 0, width, height,
width, 0, 0, height, null);
try {
return image;
} finally {
if (graphics2D != null) {
graphics2D.dispose();
}
}
}
/**
* 获取图片的类型。如果是 gif、jpg、png、bmp 以外的类型则返回null。
* @param imageBytes 图片字节数组。
* @return 图片类型。
* @throws java.io.IOException IO异常。
*/
public static String getImageType(final byte[] imageBytes)
throws IOException {
ByteArrayInputStream input = new ByteArrayInputStream(imageBytes);
ImageInputStream imageInput = ImageIO.createImageInputStream(input);
Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInput);
String type = null;
if (iterator.hasNext()) {
ImageReader reader = iterator.next();
type = reader.getFormatName().toUpperCase();
}
try {
return type;
} finally {
if (imageInput != null) {
imageInput.close();
}
}
}
/**
* 验证图片大小是否超出指定的尺寸。未超出指定大小返回true,超出指定大小则返回false。
* @param imageBytes 图片字节数组。
* @param width 图片宽度。
* @param height 图片高度。
* @return 验证结果。未超出指定大小返回true,超出指定大小则返回false。
* @throws java.io.IOException IO异常。
*/
public static boolean checkImageSize(
final byte[] imageBytes, final int width, final int height)
throws IOException {
BufferedImage image = byteToImage(imageBytes);
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
if (sourceWidth > width || sourceHeight > height) {
return false;
} else {
return true;
}
}
/**
* 将图像字节数组转化为BufferedImage图像实例。
* @param imageBytes 图像字节数组。
* @return BufferedImage图像实例。
* @throws java.io.IOException IO异常。
*/
public static BufferedImage byteToImage(
final byte[] imageBytes) throws IOException {
ByteArrayInputStream input = new ByteArrayInputStream(imageBytes);
BufferedImage image = ImageIO.read(input);
try {
return image;
} finally {
if (input != null) {
input.close();
}
}
}
/**
* 将BufferedImage持有的图像转化为指定图像格式的字节数组。
* @param bufferedImage 图像。
* @param formatName 图像格式名称。
* @return 指定图像格式的字节数组。
* @throws java.io.IOException IO异常。
*/
public static byte[] imageToByte(
final BufferedImage bufferedImage, final String formatName)
throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, formatName, output);
try {
return output.toByteArray();
} finally {
if (output != null) {
output.close();
}
}
}
/**
* 将图像以指定的格式进行输出,调用者需自行关闭输出流。
* @param bufferedImage 图像。
* @param output 输出流。
* @param formatName 图片格式名称。
* @throws java.io.IOException IO异常。
*/
public static void writeImage(final BufferedImage bufferedImage,
final OutputStream output, final String formatName)
throws IOException {
ImageIO.write(bufferedImage, formatName, output);
}
/**
* 将图像以指定的格式进行输出,调用者需自行关闭输出流。
* @param imageBytes 图像的byte数组。
* @param output 输出流。
* @param formatName 图片格式名称。
* @throws java.io.IOException IO异常。
*/
public static void writeImage(final byte[] imageBytes,
final OutputStream output, final String formatName)
throws IOException {
BufferedImage image = byteToImage(imageBytes);
ImageIO.write(image, formatName, output);
}
}
分享到:
相关推荐
图像处理工具类,比较好用的图像处理工具类
C#将图片进行旋转处理,C#图像处理还是方便好用啊.
内容概要:图像处理工具类,图像任意格式互相转换,彩色图像转换为黑白图像,图像按尺寸或按比例放大和缩小,读取本地(文件流或文件名)图像,读取网络图像,截取图像,按指定格式输出,获取指定图像的大小和尺寸,...
里给大家分享了一个java常用的图像处理工具类,包含缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等,有需要的小伙伴参考下。
本软件用于对虚拟视频的再次处理和识别,可接入红外设备进行红外层面的探测,希望可以帮到大家,但是由于有些入门基础
图像处理库常用的工具类,例如图像处理,求图像的积分图,对图像进行距离变换,边缘检测,以及一些矩阵运算常用方法
6 医学图像处理工具箱 7 人工蜂群工具箱 8 MPT3安装包 9 drEEM toolbox 10 DOMFluor Toolbox v1.7 11 Matlab数学建模工具箱 12 马尔可夫决策过程(MDP)工具箱MDPtoolbox 13 国立SVM工具箱 14 模式识别与机器学习工具...
图像融合常见功能,3类共12种图像融合算法,GUI界面,源图像
非常强大的图像处理工具。可重复使用的Ciamge类。可处理三维图像。是国际上最强的图像处理工具
由于在许多实际应用的编程中往往都要涉及到数字图像处理,涉及到其中的一些算法,这也是许多编程爱好者感兴趣的一个内容,我们这个讲座就是讨论如何利用微软的Visual C++开发工具来实现一些常用的数字图像处理算法,...
6 医学图像处理工具箱 7 人工蜂群工具箱 8 MPT3安装包 9 drEEM toolbox 10 DOMFluor Toolbox v1.7 11 Matlab数学建模工具箱 12 马尔可夫决策过程(MDP)工具箱MDPtoolbox 13 国立SVM工具箱 14 模式识别与机器学习工具...
近几年来,随着计算机和各个相关领域研究的迅速发展,科学计算可视化、多媒体技术等研究和应用的兴起,数字图像处理从一个专门领域的学科,变成了一种新型的科学研究和人机界面的工具。. Visual C++是Microsoft公司...
这个包里面包括了Java对数据和日期的处理,以及对图像处理、XML处理和HTML处理等工具类,另外还有其他一些有用类。
介绍了遥感图像处理的基本操作,熟悉遥感图像分类,图像处理等功能。还介绍了envi软件菜单栏上各个工具以及各个工具的使用方法。
计算机图形图像处理软件的种类很多,本书在简单介绍图形图像处理基本概念的基础上,介绍了目前流行的图像处理软件Photoshop、图形绘制软件CorelDRAW和网络图像处理软件Fireworks。其中,以图像处理软件Photoshop6.0...
一个实用的图像处理类,只处理RLE8压缩BMP图,即256色位图。
本书介绍了近百个数字图像处理算法的理论解析、C++源代码和应用实例。书中还说明了多种常用图像格式的规范,提供对它们进行操作和相互转换的C++源代码。所有算法都用面向对象的方法实现为类的独立函数,可以即插即用...
java图片处理工具类,里面提供常用的图片处理类,包括缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等。
8.2.2 数字图像处理的主要内容 8.3 图像文件格式 8.4 图像类型 8.4.1 索引图像 8.4.2 灰度图像 8.4.3 RGB 图像 8.4.4 二值图像 8.4.5 图像序列 8.4.6 图形类型判断 8.5 图像类型转换 8.5.1 dither 函数 ...
6 医学图像处理工具箱 7 人工蜂群工具箱 8 MPT3安装包 9 drEEM toolbox 10 DOMFluor Toolbox v1.7 11 Matlab数学建模工具箱 12 马尔可夫决策过程(MDP)工具箱MDPtoolbox 13 国立SVM工具箱 14 模式识别与机器学习工具...