本工具类的功能:缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等
复制代码 代码如下:
package net.kitbox.util;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImagingOpException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageUtils {
private static final int POSITION_UPPERLEFT=0;
private static final int POSITION_UPPERRIGHT=10;
private static final int POSITION_LOWERLEFT=1;
private static final int POSITION_LOWERRIGHT=11;
public static String IMAGE_TYPE_GIF = "gif";// 图形交换格式
public static String IMAGE_TYPE_JPG = "jpg";// 联合照片专家组
public static String IMAGE_TYPE_JPEG = "jpeg";// 联合照片专家组
public static String IMAGE_TYPE_BMP = "bmp";// 英文Bitmap(位图)的简写,它是Windows操作系统中的标准图像文件格式
public static String IMAGE_TYPE_PNG = "png";// 可移植网络图形
private static ImageUtils instance;
private ImageUtils() {
instance = this;
}
public static ImageUtils getInstance() {
if (instance == null) {
instance = new ImageUtils();
}
return instance;
}
public BufferedImage image2BufferedImage(Image image){
System.out.println(image.getWidth(null));
System.out.println(image.getHeight(null));
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, null, null);
g.dispose();
System.out.println(bufferedImage.getWidth());
System.out.println(bufferedImage.getHeight());
return bufferedImage;
}
public static boolean scaleToFile(String srcPath, String destPath, int width, int height,String format) {
boolean flag = false;
try {
File file = new File(srcPath);
File destFile = new File(destPath);
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdir();
}
BufferedImage src = ImageIO.read(file); // 读入文件
Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
flag = ImageIO.write(tag, format, new FileOutputStream(destFile));// 输出到文件流
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
public static BufferedImage scaleByPercentage(BufferedImage inputImage,int percentage){
//允许百分比
if(0>percentage||percentage>10000){
throw new ImagingOpException("Error::不合法的参数:percentage->"+percentage+",percentage应该大于0~小于10000");
}
//获取原始图像透明度类型
int type = inputImage.getColorModel().getTransparency();
//获取目标图像大小
int w=inputImage.getWidth()*percentage;
int h=inputImage.getHeight()*percentage;
//开启抗锯齿
RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_ANTIALIAS_ON);
//使用高质量压缩
renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_RENDER_QUALITY);
BufferedImage img = new BufferedImage(w, h, type);
Graphics2D graphics2d =img.createGraphics();
graphics2d.setRenderingHints(renderingHints);
graphics2d.drawImage(inputImage, 0, 0, w, h, 0, 0, inputImage
.getWidth(), inputImage.getHeight(), null);
graphics2d.dispose();
return img;
}
public static BufferedImage scaleByPixelRate(BufferedImage inputImage, int maxWidth, int maxHeight) throws Exception {
//获取原始图像透明度类型
int type = inputImage.getColorModel().getTransparency();
int width = inputImage.getWidth();
int height = inputImage.getHeight();
int newWidth = maxWidth;
int newHeight =maxHeight;
//如果指定最大宽度超过比例
if(width*maxHeight
}
//如果指定最大高度超过比例
if(width*maxHeight>height*maxWidth){
newHeight=(int)(newWidth*height/width);
}
//开启抗锯齿
RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_ANTIALIAS_ON);
//使用高质量压缩
renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_RENDER_QUALITY);
BufferedImage img = new BufferedImage(newWidth, newHeight, type);
Graphics2D graphics2d =img.createGraphics();
graphics2d.setRenderingHints(renderingHints);
graphics2d.drawImage(inputImage, 0, 0, newWidth, newHeight, 0, 0, width, height, null);
graphics2d.dispose();
return img;
}
public static BufferedImage scaleByPixel(BufferedImage inputImage, int newWidth, int newHeight) throws Exception {
//获取原始图像透明度类型
int type = inputImage.getColorModel().getTransparency();
int width = inputImage.getWidth();
int height = inputImage.getHeight();
//开启抗锯齿
RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//使用高质量压缩
renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
BufferedImage img = new BufferedImage(newWidth, newHeight, type);
Graphics2D graphics2d =img.createGraphics();
graphics2d.setRenderingHints(renderingHints);
graphics2d.drawImage(inputImage, 0, 0, newWidth, newHeight, 0, 0, width, height, null);
graphics2d.dispose();
return img;
}
public static BufferedImage cut(BufferedImage inputImage,int x,int y,int width,int height,boolean fill){
//获取原始图像透明度类型
int type = inputImage.getColorModel().getTransparency();
int w = inputImage.getWidth();
int h = inputImage.getHeight();
int endx=x+width;
int endy=y+height;
if(x>w)
throw new ImagingOpException("起点横坐标超出源图像范围");
if(y>h)
throw new ImagingOpException("起点纵坐标超出源图像范围");
BufferedImage img;
//补白
if(fill){
img = new BufferedImage(width, height, type);
//宽度超出限制
if((w-x)
endx=w;
}
//高度超出限制
if((h-y)
endy=h;
}
//不补
}else{
//宽度超出限制
if((w-x)
endx=w;
}
//高度超出限制
if((h-y)
endy=h;
}
img = new BufferedImage(width, height, type);
}
//开启抗锯齿
RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_ANTIALIAS_ON);
//使用高质量压缩
renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_RENDER_QUALITY);
Graphics2D graphics2d =img.createGraphics();
graphics2d.setRenderingHints(renderingHints);
graphics2d.drawImage(inputImage, 0, 0, width, height, x, y, endx, endy, null);
graphics2d.dispose();
return img;
}
public static BufferedImage cut(BufferedImage inputImage,int startPoint,int width,int height,boolean fill){
//获取原始图像透明度类型
int type = inputImage.getColorModel().getTransparency();
int w = inputImage.getWidth();
int h = inputImage.getHeight();
BufferedImage img;
//补白
if(fill){
img = new BufferedImage(width, height, type);
if(width>w)
width=w;
if(height>h)
height=h;
//不补
}else{
if(width>w)
width=w;
if(height>h)
height=h;
img = new BufferedImage(width, height, type);
}
//开启抗锯齿
RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_ANTIALIAS_ON);
//使用高质量压缩
renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_RENDER_QUALITY);
Graphics2D graphics2d =img.createGraphics();
graphics2d.setRenderingHints(renderingHints);
switch(startPoint){
//右上
case POSITION_UPPERRIGHT:
graphics2d.drawImage(inputImage, w-width, 0, w, height, 0, 0, width, height, null);
break;
//左下
case POSITION_LOWERLEFT:
graphics2d.drawImage(inputImage, 0, h-height, width, h, 0, 0, width, height, null);
break;
//右下
case POSITION_LOWERRIGHT:
graphics2d.drawImage(inputImage, w-width, h-height, w, h, 0, 0, width, height, null);
break;
//默认左上
case POSITION_UPPERLEFT:
default:
graphics2d.drawImage(inputImage, 0, 0, width, height, 0, 0, width, height, null);
}
graphics2d.dispose();
return img;
}
public static BufferedImage rotateImage(final BufferedImage inputImage,
final int degree) {
int w = inputImage.getWidth();
int h = inputImage.getHeight();
int type = inputImage.getColorModel().getTransparency();
BufferedImage img=new BufferedImage(w, h, type);
Graphics2D graphics2d =img.createGraphics();
//开启抗锯齿
RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_ANTIALIAS_ON);
//使用高质量压缩
renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_RENDER_QUALITY);
graphics2d.setRenderingHints(renderingHints);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
graphics2d.drawImage(inputImage, 0, 0, null);
graphics2d.dispose();
return img;
}
public static BufferedImage flipHorizontalImage(final BufferedImage inputImage) {
int w = inputImage.getWidth();
int h = inputImage.getHeight();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, inputImage
.getColorModel().getTransparency())).createGraphics())
.drawImage(inputImage, 0, 0, w, h, w, 0, 0, h, null);
graphics2d.dispose();
return img;
}
public static BufferedImage flipVerticalImage(final BufferedImage inputImage) {
int w = inputImage.getWidth();
int h = inputImage.getHeight();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, inputImage
.getColorModel().getTransparency())).createGraphics())
.drawImage(inputImage, 0, 0, w, h, 0, h, w, 0, null);
graphics2d.dispose();
return img;
}
public static BufferedImage waterMark(BufferedImage inputImage,BufferedImage markImage, int x, int y,
float alpha) {
BufferedImage image = new BufferedImage(inputImage.getWidth(), inputImage
.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.drawImage(inputImage, 0, 0, null);
// 加载水印图像
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
g.drawImage(markImage, x, y, null);
g.dispose();
return image;
}
public static BufferedImage textMark(BufferedImage inputImage, String text, Font font,
Color color, int x, int y, float alpha) {
Font dfont = (font == null) ? new Font("宋体", 20, 13) : font;
BufferedImage image = new BufferedImage(inputImage.getWidth(), inputImage
.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.drawImage(inputImage, 0, 0, null);
g.setColor(color);
g.setFont(dfont);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
g.drawString(text, x, y);
g.dispose();
return image;
}
public final static BufferedImage toGray(BufferedImage inputImage)
{
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
//对源 BufferedImage 进行颜色转换。如果目标图像为 null,
//则根据适当的 ColorModel 创建 BufferedImage。
ColorConvertOp op = new ColorConvertOp(cs, null);
return op.filter(inputImage, null);
}
public final static void toGray(String srcImageFile, String destImageFile,String formatType)
{
try
{
BufferedImage src = ImageIO.read(new File(srcImageFile));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
src = op.filter(src, null);
//如果formatType为null;将默认转换为PNG
if(formatType==null){
formatType="PNG";
}
ImageIO.write(src,formatType,new File(destImageFile));
} catch (IOException e)
{
e.printStackTrace();
}
}
public final static void convert(BufferedImage inputImage, String formatType,String destImageFile)
{
try
{
ImageIO.write(inputImage, formatType, new File(destImageFile));
} catch (Exception e)
{
e.printStackTrace();
}
}
public final static void cut(BufferedImage inputImage, String destDir,
String formatType,int rows, int cols)
{
try
{
if (rows <= 0 || rows > 20)
rows = 2; // 切片行数
if (cols <= 0 || cols > 20)
cols = 2; // 切片列数
// 读取源图像
//BufferedImage bi = ImageIO.read(new File(srcImageFile));
int w = inputImage.getHeight(); // 源图宽度
int h = inputImage.getWidth(); // 源图高度
if (w > 0 && h > 0)
{
Image img;
ImageFilter cropFilter;
Image image = inputImage.getScaledInstance(w, h,
Image.SCALE_DEFAULT);
int destWidth = w; // 每张切片的宽度
int destHeight = h; // 每张切片的高度
// 计算切片的宽度和高度
if (w % cols == 0)
{
destWidth = w / cols;
} else
{
destWidth = (int) Math.floor(w / cols) + 1;
}
if (h % rows == 0)
{
destHeight = h / rows;
} else
{
destHeight = (int) Math.floor(h / rows) + 1;
}
// 循环建立切片
// 改进的想法:是否可用多线程加快切割速度
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
// 四个参数分别为图像起点坐标和宽高
// 即: CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(j * destWidth, i
* destHeight, destWidth, destHeight);
img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(),
cropFilter));
BufferedImage tag = new BufferedImage(destWidth,
destHeight, BufferedImage.TYPE_INT_ARGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 绘制缩小后的图
g.dispose();
// 输出为文件
ImageIO.write(tag, formatType, new File(destDir + "_r" + i
+ "_c" + j + "."+formatType.toLowerCase()));
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
public final static void pressText(String pressText, String srcImageFile,
String destImageFile, String fontName, int fontStyle, Color color,
int fontSize, int x, int y, float alpha,String formatType)
{
try
{
File img = new File(srcImageFile);
Image src = ImageIO.read(img);
int width = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, width, height, null);
g.setColor(color);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
// 在指定坐标绘制水印文字
g.drawString(pressText, (width - (getLength(pressText) * fontSize))
/ 2 + x, (height - fontSize) / 2 + y);
g.dispose();
ImageIO.write((BufferedImage) image, formatType,
new File(destImageFile));// 输出到文件流
} catch (Exception e)
{
e.printStackTrace();
}
}
public final static void pressImage(String pressImg, String srcImageFile,
String destImageFile, int x, int y, float alpha,String formatType)
{
try
{
File img = new File(srcImageFile);
Image src = ImageIO.read(img);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// 水印文件
Image src_biao = ImageIO.read(new File(pressImg));
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
g.drawImage(src_biao, (wideth - wideth_biao) / 2,
(height - height_biao) / 2, wideth_biao, height_biao, null);
// 水印文件结束
g.dispose();
ImageIO.write((BufferedImage) image, formatType,
new File(destImageFile));
} catch (Exception e)
{
e.printStackTrace();
}
}
public final static int getLength(String text)
{
int length = 0;
for (int i = 0; i < text.length(); i++)
{
if (new String(text.charAt(i) + "").getBytes().length > 1)
{
length += 2;
} else
{
length += 1;
}
}
return length / 2;
}
}
非常实用的图像处理功能,希望大家能够喜欢。



