package com.trulytech.mantis.util;
import com.jhlabs.image.ExposureFilter;
import com.jhlabs.image.GainFilter;
import com.jhlabs.image.GammaFilter;
import com.jhlabs.image.GaussianFilter;
import com.jhlabs.image.HSBAdjustFilter;
import com.jhlabs.image.RGBAdjustFilter;
import com.jhlabs.image.SharpenFilter;
import com.sun.image.codec.jpeg.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import com.trulytech.mantis.system.logWriter;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;
import javax.imageio.ImageIO;
import magick.ImageInfo;
import magick.MagickImage;
import com.anotherbigidea.flash.movie.Shape;
import com.anotherbigidea.flash.movie.ImageUtil;
import com.anotherbigidea.flash.movie.Frame;
import com.anotherbigidea.flash.movie.Movie;
import java.awt.geom.AffineTransform;
/**
*
* <p>
* Title: Mantis
* </p>
*
* <p>
* Description: 文件图片处理
* </p>
*
* <p>
* Copyright: Copyright (c) 2002
* </p>
*
* <p>
* Company:
* </p>
*
* @author Wang Xian
* @version 1.0
*/
public class ImageUtils {
/**
* 产生缩略图
*
* @param Src_file
* String 源文件 必须是JPG,PNG,GIF文件
* @param width
* int 长度
* @param height
* int 高度
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Thumbnail(String Src_file, int width, int height)
throws Exception {
logWriter.Info("产生缩略图,源文件:" + Src_file);
Image image = Toolkit.getDefaultToolkit().getImage(Src_file);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
BufferedImage thumbImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, width, height, null);
graphics2D.dispose();
return thumbImage;
}
/**
* 产生缩略图
*
* @param image
* BufferedImage 源文件
* @param width
* int 长度
* @param height
* int 高度
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Thumbnail(BufferedImage image, int width,
int height) throws Exception {
logWriter.Info("产生缩略图");
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
BufferedImage thumbImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, width, height, null);
graphics2D.dispose();
return thumbImage;
}
/**
* 产生缩略图
*
* @param Src_file
* String 源文件 必须是JPG,PNG,GIF文件
* @param Rate
* int 缩小比例(百分比)
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Thumbnail(String Src_file, int Rate)
throws Exception {
logWriter.Info("产生缩略图,源文件:" + Src_file);
Image image = Toolkit.getDefaultToolkit().getImage(Src_file);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
BufferedImage thumbImage = new BufferedImage(imageWidth * Rate / 100,
imageHeight * Rate / 100, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, imageWidth * Rate / 100, imageHeight
* Rate / 100, null);
graphics2D.dispose();
return thumbImage;
}
/**
* 产生缩略图
*
* @param image
* BufferedImage 源文件
* @param Rate
* int 缩小比例(百分比)
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Thumbnail(BufferedImage image, int Rate)
throws Exception {
logWriter.Info("产生缩略图");
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
BufferedImage thumbImage = new BufferedImage(imageWidth * Rate / 100,
imageHeight * Rate / 100, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, imageWidth * Rate / 100, imageHeight
* Rate / 100, null);
graphics2D.dispose();
return thumbImage;
}
/**
* 生成大图的截取图
*
* @param Src_file
* String 源文件
* @param src_x
* int 源文件x位置
* @param src_y
* int 源文件y位置
* @param desc_width
* int 目标文件宽度
* @param desc_height
* int 目标文件高度
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage DrawClip(String Src_file, int src_x, int src_y,
int desc_width, int desc_height) throws Exception {
logWriter.Info("产生图片分割,源文件:" + Src_file);
Image image = Toolkit.getDefaultToolkit().getImage(Src_file);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
BufferedImage thumbImage = new BufferedImage(desc_width, desc_height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, desc_width, desc_height, src_x,
src_y, image.getWidth(null), image.getHeight(null), null);
graphics2D.dispose();
return thumbImage;
}
/**
* 生成大图的截取图
*
* @param image
* BufferedImage 源文件
* @param src_x
* int 源文件x位置
* @param src_y
* int 源文件y位置
* @param desc_width
* int 目标文件宽度
* @param desc_height
* int 目标文件高度
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage DrawClip(BufferedImage image, int src_x,
int src_y, int desc_width, int desc_height) throws Exception {
logWriter.Info("产生图片分割");
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
BufferedImage thumbImage = new BufferedImage(desc_width, desc_height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, desc_width, desc_height, src_x,
src_y, image.getWidth(null), image.getHeight(null), null);
graphics2D.dispose();
return thumbImage;
}
/**
* 合并多个图片(请注意数组的顺序是按照图片大小的顺序排列,大的在前,小的在后,否则大图将覆盖小图)
*
* @param Src_file
* String[] 资源文件
* @param x
* int[] x位置
* @param y
* int[] y位置
* @param Totalwidth
* int 生成图片的宽度
* @param Totalheight
* int 生成图片的高度
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Coalition(String[] Src_file, int[] x, int[] y,
int Totalwidth, int Totalheight) throws Exception {
logWriter.Info("产生合并图");
Image[] image = new Image[Src_file.length];
MediaTracker[] mediaTracker = new MediaTracker[Src_file.length];
for (int i = 0; i < Src_file.length; i++) {
image[i] = Toolkit.getDefaultToolkit().getImage(Src_file[i]);
mediaTracker[i] = new MediaTracker(new Container());
mediaTracker[i].addImage(image[i], 0);
mediaTracker[i].waitForID(0);
}
BufferedImage thumbImage = new BufferedImage(Totalwidth, Totalheight,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
for (int i = 0; i < Src_file.length; i++) {
graphics2D.drawImage(image[i], x[i], y[i], image[i].getWidth(null),
image[i].getHeight(null), null);
}
graphics2D.dispose();
return thumbImage;
}
/**
* 合并多个图片 (请注意数组的顺序是按照图片大小的顺序排列,大的在前,小的在后,否则大图将覆盖小图)
*
* @param image
* BufferedImage[] 资源文件
* @param x
* int[] x位置
* @param y
* int[] y位置
* @param Totalwidth
* int 生成图片的宽度
* @param Totalheight
* int 生成图片的高度
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Coalition(BufferedImage[] image, int[] x,
int[] y, int Totalwidth, int Totalheight) throws Exception {
logWriter.Info("产生合并图");
BufferedImage thumbImage = new BufferedImage(Totalwidth, Totalheight,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
for (int i = 0; i < image.length; i++) {
graphics2D.drawImage(image[i], x[i], y[i], image[i].getWidth(null),
image[i].getHeight(null), null);
}
graphics2D.dispose();
return thumbImage;
}
/**
* 产生图片字符串
*
* @param Src
* String 字符串
* @param BGColor
* Color 背景色
* @param FontColor
* Color 文字颜色
* @param font
* Font 字体
* @param x
* int x坐标
* @param y
* int y坐标
* @param width
* int 图片宽度
* @param height
* int 图片高度
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage DrawStringImg(String Src, Color BGColor,
Color FontColor, Font font, int x, int y, int width, int height)
throws Exception {
logWriter.Info("产生文字图");
BufferedImage thumbImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setBackground(BGColor);
graphics2D.setColor(BGColor);
graphics2D.fillRect(0, 0, width, height);
graphics2D.setFont(font);
graphics2D.setColor(FontColor);
graphics2D.drawString(Src, x, y);
graphics2D.dispose();
return thumbImage;
}
/**
* 添加字符串到图片
*
* @param Src
* String 字符串
* @param Src_file
* 背景图
* @param FontColor
* Color 文字颜色
* @param font
* Font 字体
* @param x
* int x坐标
* @param y
* int y坐标
* @param width
* int 图片宽度
* @param height
* int 图片高度
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage DrawString(String Src, String Src_file,
Color FontColor, Font font, int x, int y, int width, int height)
throws Exception {
logWriter.Info("产生文字图");
Image image = Toolkit.getDefaultToolkit().getImage(Src_file);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
BufferedImage thumbImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, image.getWidth(null), image
.getHeight(null), null);
graphics2D.setFont(font);
graphics2D.setColor(FontColor);
graphics2D.drawString(Src, x, y);
graphics2D.dispose();
return thumbImage;
}
/**
* 添加字符串到图片
*
* @param Src
* String 字符串
* @param image
* BufferedImage image 背景图
* @param FontColor
* Color 文字颜色
* @param font
* Font 字体
* @param x
* int x坐标
* @param y
* int y坐标
* @param width
* int 图片宽度
* @param height
* int 图片高度
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage DrawString(String Src, BufferedImage image,
Color FontColor, Font font, int x, int y, int width, int height)
throws Exception {
logWriter.Info("产生文字图");
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
BufferedImage thumbImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, image.getWidth(null), image
.getHeight(null), null);
graphics2D.setFont(font);
graphics2D.setColor(FontColor);
graphics2D.drawString(Src, x, y);
graphics2D.dispose();
return thumbImage;
}
/**
* 写JPG图片
*
* @param Desc
* String 目标文件
* @param buffimg
* BufferedImage 图片
* @throws Exception
*/
public static void GenerateJPG(String Desc, BufferedImage buffimg)
throws Exception {
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream((String) Desc));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(buffimg);
out.flush();
out.close();
}
/**
* 写JPG图片
*
* @param Desc
* HttpServletResponse
* @param buffimg
* BufferedImage 图片
* @throws Exception
*/
public static void GenerateJPG(HttpServletResponse Desc,
BufferedImage buffimg) throws Exception {
((HttpServletResponse) Desc).setContentType("image/jpeg");
((HttpServletResponse) Desc).setHeader("Content-Disposition",
" filename=image.jpg");
ServletOutputStream out = ((HttpServletResponse) Desc)
.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(buffimg);
out.flush();
out.close();
}
/**
* 以GIF格式输出
*
* @param Desc
* String 目标文件
* @param buffimg
* BufferedImage
* @throws Exception
*/
public static void GenerateGIF(String Desc, BufferedImage buffimg)
throws Exception {
// 如果是String,则存成文件
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream((String) Desc));
net.jgif.GIFImage image = new net.jgif.GIFImage(buffimg);
/*
* 指定版本号,如果指定为 GIF87A,则注解扩展及透明色实效。 默认为 GIF89A。
*/
// image.setVersion(net.jgif.GIFImage.GIF87A);
/*
* 指定其透明色,也可不指定。 无论是否指定,在 BufferedImage 中为透明色的颜色一律透明。 如果指定了透明色,则该颜色也将透明。
*/
// image.setTransparenceColor(java.awt.Color.BLACK);
image.isInterlace = true; // 指定是否进行交错处理(默认为false)。
image.save(out);
out.flush();
out.close();
}
/**
* 以GIF格式输出
*
* @param Desc
* HttpServletResponse HTTPResponse
* @param buffimg
* BufferedImage
* @throws Exception
*/
public static void GenerateGIF(HttpServletResponse Desc,
BufferedImage buffimg) throws Exception {
// 如果是String,则存成文件
((HttpServletResponse) Desc).setContentType("image/gif");
((HttpServletResponse) Desc).setHeader("Content-Disposition",
" filename=image.gif");
ServletOutputStream out = ((HttpServletResponse) Desc)
.getOutputStream();
net.jgif.GIFImage image = new net.jgif.GIFImage(buffimg);
image.isInterlace = true; // 指定是否进行交错处理(默认为false)。
image.save(out);
out.flush();
out.close();
}
/**
* 产生PNG图片
*
* @param Desc
* String 目标文件
* @param buffimg
* BufferedImage
* @throws Exception
*/
public static void GeneratePNG(String Desc, BufferedImage buffimg)
throws Exception {
// 如果是String,则存成文件
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream((String) Desc));
ImageIO.write(buffimg, "png", out);
out.flush();
out.close();
}
/**
* 产生PNG图片
*
* @param Desc
* HttpServletResponse 目标文件
* @param buffimg
* BufferedImage
* @throws Exception
*/
public static void GeneratePNG(HttpServletResponse Desc,
BufferedImage buffimg) throws Exception {
((HttpServletResponse) Desc).setContentType("image/png");
((HttpServletResponse) Desc).setHeader("Content-Disposition",
" filename=image.png");
ServletOutputStream out = ((HttpServletResponse) Desc)
.getOutputStream();
ImageIO.write(buffimg, "png", out);
out.flush();
out.close();
}
/**
* 产生GIF89动画
*
* @param Src_File
* String[] 源文件
* @param DelayTime
* int[] 延时时间(ms)
* @param Desc
* String
* @throws Exception
*/
public static void DrawGifAnimate(String[] Src_File, int[] DelayTime,
String Desc) throws Exception {
if (Src_File != null && DelayTime != null) {
logWriter.Info("产生GIF动画");
net.jgif.GIFAnimator image = new net.jgif.GIFAnimator();
// 设置循环次数,0 为无限循环,大于 0 为指定次数,默认为 0。
image.setLoopCount(0);
for (int i = 0; i < Src_File.length; i++) {
logWriter.Debug("产生GIF动画,源文件:" + Src_File[i]);
net.jgif.GIFFrame frame = new net.jgif.GIFFrame(ImageIO
.read(new File(Src_File[i])));
// 指定显示方案,默认为 DM_LEAVE。
// frame.setDisposalMethod(net.jgif.GIFFrame.DM_BGCOLOR);
// 设置该帧的显示时间,单位为毫秒,如果不指定则为默认值 500,即 0.5 秒。
frame.setDelayTime(DelayTime[i]);
// 指定frame在整幅图片中的位置。
// frame.imageLeft = 10;
// frame.imageTop = 10;
/*
* 指定其透明色,也可不指定。 无论是否指定,在 BufferedImage 中为透明色的颜色一律透明。
* 如果指定了透明色,则该颜色也将透明。
*/
// frame.setTransparenceColor(java.awt.Color.BLACK);
image.addFrame(frame);
}
// 文件输出
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream((String) Desc));
image.save(out);
out.flush();
out.close();
}
}
/**
* 产生GIF89动画
*
* @param img
* BufferedImage[]
* @param DelayTime
* int[]
* @param Desc
* String
* @throws Exception
*/
public static void DrawGifAnimate(BufferedImage[] img, int[] DelayTime,
String Desc) throws Exception {
if (img != null && DelayTime != null) {
logWriter.Info("产生GIF动画");
net.jgif.GIFAnimator image = new net.jgif.GIFAnimator();
// 设置循环次数,0 为无限循环,大于 0 为指定次数,默认为 0。
image.setLoopCount(0);
for (int i = 0; i < img.length; i++) {
net.jgif.GIFFrame frame = new net.jgif.GIFFrame(img[i]);
// 指定显示方案,默认为 DM_LEAVE。
// frame.setDisposalMethod(net.jgif.GIFFrame.DM_BGCOLOR);
// 设置该帧的显示时间,单位为毫秒,如果不指定则为默认值 500,即 0.5 秒。
frame.setDelayTime(DelayTime[i]);
// 指定frame在整幅图片中的位置。
// frame.imageLeft = 10;
// frame.imageTop = 10;
/*
* 指定其透明色,也可不指定。 无论是否指定,在 BufferedImage 中为透明色的颜色一律透明。
* 如果指定了透明色,则该颜色也将透明。
*/
// frame.setTransparenceColor(java.awt.Color.BLACK);
image.addFrame(frame);
}
// 文件输出
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream((String) Desc));
image.save(out);
out.flush();
out.close();
}
}
/**
* 产生GIF89动画
*
* @param Src_File
* String[] 源文件
* @param DelayTime
* int[] 延时时间(ms)
* @param Desc
* HttpServletResponse
* @throws Exception
*/
public static void DrawGifAnimate(String[] Src_File, int[] DelayTime,
HttpServletResponse Desc) throws Exception {
if (Src_File != null && DelayTime != null) {
logWriter.Info("产生GIF动画");
net.jgif.GIFAnimator image = new net.jgif.GIFAnimator();
// 设置循环次数,0 为无限循环,大于 0 为指定次数,默认为 0。
image.setLoopCount(0);
for (int i = 0; i < Src_File.length; i++) {
logWriter.Debug("产生GIF动画,源文件:" + Src_File[i]);
net.jgif.GIFFrame frame = new net.jgif.GIFFrame(ImageIO
.read(new File(Src_File[i])));
// 指定显示方案,默认为 DM_LEAVE。
// frame.setDisposalMethod(net.jgif.GIFFrame.DM_BGCOLOR);
// 设置该帧的显示时间,单位为毫秒,如果不指定则为默认值 500,即 0.5 秒。
frame.setDelayTime(DelayTime[i]);
// 指定frame在整幅图片中的位置。
// frame.imageLeft = 10;
// frame.imageTop = 10;
/*
* 指定其透明色,也可不指定。 无论是否指定,在 BufferedImage 中为透明色的颜色一律透明。
* 如果指定了透明色,则该颜色也将透明。
*/
// frame.setTransparenceColor(java.awt.Color.BLACK);
image.addFrame(frame);
}
// Http输出
((HttpServletResponse) Desc).setContentType("image/gif");
((HttpServletResponse) Desc).setHeader("Content-Disposition",
" filename=animate.gif");
ServletOutputStream out = ((HttpServletResponse) Desc)
.getOutputStream();
image.save(out);
out.flush();
out.close();
}
}
/**
* 产生GIF89动画
*
* @param img
* BufferedImage[]
* @param DelayTime
* int[]
* @param Desc
* HttpServletResponse
* @throws Exception
*/
public static void DrawGifAnimate(BufferedImage[] img, int[] DelayTime,
HttpServletResponse Desc) throws Exception {
if (img != null && DelayTime != null) {
logWriter.Info("产生GIF动画");
net.jgif.GIFAnimator image = new net.jgif.GIFAnimator();
// 设置循环次数,0 为无限循环,大于 0 为指定次数,默认为 0。
image.setLoopCount(0);
for (int i = 0; i < img.length; i++) {
net.jgif.GIFFrame frame = new net.jgif.GIFFrame(img[i]);
// 指定显示方案,默认为 DM_LEAVE。
// frame.setDisposalMethod(net.jgif.GIFFrame.DM_BGCOLOR);
// 设置该帧的显示时间,单位为毫秒,如果不指定则为默认值 500,即 0.5 秒。
frame.setDelayTime(DelayTime[i]);
// 指定frame在整幅图片中的位置。
// frame.imageLeft = 10;
// frame.imageTop = 10;
/*
* 指定其透明色,也可不指定。 无论是否指定,在 BufferedImage 中为透明色的颜色一律透明。
* 如果指定了透明色,则该颜色也将透明。
*/
// frame.setTransparenceColor(java.awt.Color.BLACK);
image.addFrame(frame);
}
// Http输出
((HttpServletResponse) Desc).setContentType("image/gif");
((HttpServletResponse) Desc).setHeader("Content-Disposition",
" filename=animate.gif");
ServletOutputStream out = ((HttpServletResponse) Desc)
.getOutputStream();
image.save(out);
out.flush();
out.close();
}
}
/**
* 产生flash动画
*
* @param Src_File
* String[] 源文件必须是jpg格式
* @param DelayTime
* int[] 单位为ms,最小单位不得小于1s
* @param Desc
* String 目标文件
* @throws Exception
*/
public static void DrawFlashAnimate(String[] Src_File, int[] DelayTime,
String Desc) throws Exception {
if (Src_File != null && DelayTime != null) {
logWriter.Info("产生Flash动画");
int width = 0;
int height = 0;
Shape[] image = new Shape[Src_File.length];
for (int i = 0; i < Src_File.length; i++) {
FileInputStream jpegIn = new FileInputStream(Src_File[i]);
int[] size = new int[2];
image[i] = ImageUtil.shapeForImage(jpegIn, size);
if (width < size[0])
width = size[0];
if (height < size[1])
height = size[1];
jpegIn.close();
}
Movie movie = new Movie(width, height, 1, 5, null);
for (int i = 0; i < image.length; i++) {
if (DelayTime[i] > 1000) {
for (int j = 0; j < (DelayTime[i]) / 1000; j++) {
Frame frame = movie.appendFrame();
frame.placeSymbol(image[i], 0, 0);
}
} else {
Frame frame = movie.appendFrame();
frame.placeSymbol(image[i], 0, 0);
}
}
// 文件输出
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream((String) Desc));
movie.write(out);
out.flush();
out.close();
}
}
/**
* 产生flash动画
*
* @param Src_File
* String[] 源文件必须是jpg格式
* @param DelayTime
* int[] 单位为ms,最小单位不得小于1s
* @param Desc
* HttpServletResponse
* @throws Exception
*/
public static void DrawFlashAnimate(String[] Src_File, int[] DelayTime,
HttpServletResponse Desc) throws Exception {
if (Src_File != null && DelayTime != null) {
logWriter.Info("产生Flash动画");
int width = 0;
int height = 0;
Shape[] image = new Shape[Src_File.length];
for (int i = 0; i < Src_File.length; i++) {
FileInputStream jpegIn = new FileInputStream(Src_File[i]);
int[] size = new int[2];
image[i] = ImageUtil.shapeForImage(jpegIn, size);
if (width < size[0])
width = size[0];
if (height < size[1])
height = size[1];
jpegIn.close();
}
Movie movie = new Movie(width, height, 1, 5, null);
for (int i = 0; i < image.length; i++) {
if (DelayTime[i] > 1000) {
for (int j = 0; j < (DelayTime[i]) / 1000; j++) {
Frame frame = movie.appendFrame();
frame.placeSymbol(image[i], 0, 0);
}
} else {
Frame frame = movie.appendFrame();
frame.placeSymbol(image[i], 0, 0);
}
}
// Http输出
((HttpServletResponse) Desc)
.setContentType("application/x-shockwave-flash");
((HttpServletResponse) Desc).setHeader("Content-Disposition",
" filename=animate.swf");
ServletOutputStream out = ((HttpServletResponse) Desc)
.getOutputStream();
movie.write(out);
out.flush();
out.close();
}
}
/**
* 向右旋转图片
*
* @param Src_file
* String 图片文件名
* @param angdeg
* double 角度
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Rotation(String Src_file, double angdeg)
throws Exception {
logWriter.Info("产生旋转图");
BufferedImage thumbImage = ImageIO.read(new File(Src_file));
AffineTransform transform = AffineTransform.getRotateInstance(Math
.toRadians(angdeg), thumbImage.getWidth() / 2, thumbImage
.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(transform,
AffineTransformOp.TYPE_BILINEAR);
BufferedImage filteredImage = new BufferedImage(thumbImage.getWidth(),
thumbImage.getHeight(), thumbImage.getType());
op.filter(thumbImage, filteredImage);
op = null;
transform = null;
thumbImage = null;
return filteredImage;
}
/**
* 向右旋转图片
*
* @param thumbImage
* BufferedImage 图片
* @param angdeg
* double 旋转角度
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Rotation(BufferedImage thumbImage, double angdeg)
throws Exception {
logWriter.Info("产生旋转图");
AffineTransform transform = AffineTransform.getRotateInstance(Math
.toRadians(angdeg), thumbImage.getWidth() / 2, thumbImage
.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(transform,
AffineTransformOp.TYPE_BILINEAR);
BufferedImage filteredImage = new BufferedImage(thumbImage.getWidth(),
thumbImage.getHeight(), thumbImage.getType());
op.filter(thumbImage, filteredImage);
op = null;
transform = null;
return filteredImage;
}
/**
* 产生图片边缘
*
* @param Src_file
* String 图片文件
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Edge(String Src_file) throws Exception {
logWriter.Info("产生图片边缘");
BufferedImage thumbImage = ImageIO.read(new File(Src_file));
float[] elements = { 0.0f, -1.0f, 0.0f, -1.0f, 4.f, -1.0f, 0.0f, -1.0f,
0.0f };
Kernel kernel = new Kernel(3, 3, elements);
ConvolveOp op = new ConvolveOp(kernel);
BufferedImage filteredImage = new BufferedImage(thumbImage.getWidth(),
thumbImage.getHeight(), thumbImage.getType());
op.filter(thumbImage, filteredImage);
thumbImage = null;
op = null;
kernel = null;
elements = null;
return filteredImage;
}
/**
* 产生图片边缘
*
* @param thumbImage
* BufferedImage 图片
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Edge(BufferedImage thumbImage) throws Exception {
logWriter.Info("产生图片边缘");
float[] elements = { 0.0f, -1.0f, 0.0f, -1.0f, 4.f, -1.0f, 0.0f, -1.0f,
0.0f };
Kernel kernel = new Kernel(3, 3, elements);
ConvolveOp op = new ConvolveOp(kernel);
BufferedImage filteredImage = new BufferedImage(thumbImage.getWidth(),
thumbImage.getHeight(), thumbImage.getType());
op.filter(thumbImage, filteredImage);
op = null;
kernel = null;
elements = null;
return filteredImage;
}
/**
* 产生胶片效果图
*
* @param Src_file
* String 图片文件
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Negative(String Src_file) throws Exception {
logWriter.Info("产生胶片效果图");
BufferedImage thumbImage = ImageIO.read(new File(Src_file));
byte negative[] = new byte[256];
for (int i = 0; i < 256; i++)
negative[i] = (byte) (255 - i);
ByteLookupTable table = new ByteLookupTable(0, negative);
LookupOp op = new LookupOp(table, null);
BufferedImage filteredImage = new BufferedImage(thumbImage.getWidth(),
thumbImage.getHeight(), thumbImage.getType());
op.filter(thumbImage, filteredImage);
thumbImage = null;
op = null;
table = null;
return filteredImage;
}
/**
* 产生胶片效果图
*
* @param thumbImage
* BufferedImage 图片
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Negative(BufferedImage thumbImage)
throws Exception {
logWriter.Info("产生胶片效果图");
byte negative[] = new byte[256];
for (int i = 0; i < 256; i++)
negative[i] = (byte) (255 - i);
ByteLookupTable table = new ByteLookupTable(0, negative);
LookupOp op = new LookupOp(table, null);
BufferedImage filteredImage = new BufferedImage(thumbImage.getWidth(),
thumbImage.getHeight(), thumbImage.getType());
op.filter(thumbImage, filteredImage);
op = null;
table = null;
return filteredImage;
}
/**
* 变亮/暗效果
*
* @param Src_file
* String 图片文件
* @param weight
* 设置变换参数 越小越暗 例如 1.0f/9.0f
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Brighten(String Src_file, float weight)
throws Exception {
logWriter.Info("产生变亮/变暗效果图");
BufferedImage thumbImage = ImageIO.read(new File(Src_file));
float[] elements = new float[9];
for (int i = 0; i < 9; i++)
elements[i] = weight;
Kernel kernel = new Kernel(3, 3, elements);
ConvolveOp op = new ConvolveOp(kernel);
BufferedImage filteredImage = new BufferedImage(thumbImage.getWidth(),
thumbImage.getHeight(), thumbImage.getType());
op.filter(thumbImage, filteredImage);
thumbImage = null;
op = null;
kernel = null;
elements = null;
return filteredImage;
}
/**
* 变亮/暗效果
*
* @param thumbImage
* BufferedImage 图片
* @param weight
* float 设置变换参数 越小越暗 例如 1.0f/9.0f
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage Brighten(BufferedImage thumbImage, float weight)
throws Exception {
logWriter.Info("产生变亮/变暗效果图");
float[] elements = new float[9];
for (int i = 0; i < 9; i++)
elements[i] = weight;
Kernel kernel = new Kernel(3, 3, elements);
ConvolveOp op = new ConvolveOp(kernel);
BufferedImage filteredImage = new BufferedImage(thumbImage.getWidth(),
thumbImage.getHeight(), thumbImage.getType());
op.filter(thumbImage, filteredImage);
op = null;
kernel = null;
elements = null;
return filteredImage;
}
// 锐化
public static BufferedImage Sharpen(BufferedImage src, int times) {
logWriter.Debug("Sharpen Image");
BufferedImage desc = null;
SharpenFilter filter = new SharpenFilter();
filter.setPremultiplyAlpha(true);
filter.setUseAlpha(true);
desc = filter.filter(src, desc);
for (int i = 0; i < times - 1; i++) {
desc = filter.filter(desc, desc);
}
filter = null;
return desc;
}
// 增加红色,r=0,不变
public static BufferedImage RGBAdjustFilter(BufferedImage src, float r,
float g, float b) {
logWriter.Debug("RGBAdjust Image");
BufferedImage desc = null;
RGBAdjustFilter filter = new RGBAdjustFilter();
filter.setGFactor(g);
filter.setBFactor(b);
filter.setRFactor(r);
desc = filter.filter(src, null);
filter = null;
return desc;
}
// 增加亮度, gammer=1不变,<1变暗 >1变亮
public static BufferedImage GammaFilter(BufferedImage src, float gamma) {
logWriter.Debug("Gamma Image");
BufferedImage desc = null;
GammaFilter filter = new GammaFilter();
filter.setGamma(gamma);
desc = filter.filter(src, null);
filter = null;
return desc;
}
// 增加鲜艳度,饱和度,hue=0,不变
public static BufferedImage HSBAdjustFilter(BufferedImage src, float hue,
float saturation, float brightness) {
logWriter.Debug("HSBAdjust Image");
BufferedImage desc = null;
HSBAdjustFilter filter = new HSBAdjustFilter();
filter.setHFactor(hue);
filter.setBFactor(brightness);
filter.setSFactor(saturation);
desc = filter.filter(src, null);
filter = null;
return desc;
}
// gauss blur radius=0 不变
public static BufferedImage GaussianFilter(BufferedImage src, float radius) {
logWriter.Debug("Gaussian Image");
BufferedImage desc = null;
GaussianFilter filter = new GaussianFilter();
filter.setRadius(radius);
desc = filter.filter(src, null);
filter = null;
return desc;
}
// 瀑光度
public static BufferedImage ExposureFilter(BufferedImage src, float exposure) {
BufferedImage desc = null;
ExposureFilter filter = new ExposureFilter();
filter.setExposure(exposure);
desc = filter.filter(src, null);
filter = null;
return desc;
}
// 对比度 bias-色彩亮度(0.5) gain-色彩对比度(0.5)
public static BufferedImage GainFilter(BufferedImage src, float bias,
float gain) {
BufferedImage desc = null;
GainFilter filter = new GainFilter();
filter.setBias(bias);
filter.setGain(gain);
desc = filter.filter(src, null);
filter = null;
return desc;
}
//通过magickImage生成高质量缩略图
public static void Thumbnail(String Src_file, String desc_file, int maxSize,int quantity)
throws Exception {
logWriter.Info("产生缩略图,源文件:" + Src_file);
System.setProperty("jmagick.systemclassloader", "no");
ImageInfo info = new ImageInfo(Src_file);
MagickImage image = new MagickImage(info);
Dimension dim = image.getDimension();
double rateWidth = 1;
double rateHeight = 1;
double imageWidth = dim.getWidth();
double imageHeight = dim.getHeight();
if (imageWidth <= maxSize && imageHeight <= maxSize) {
} else {
int Width = maxSize;
int Height = maxSize;
if (imageWidth > imageHeight) {
rateHeight = (Double.valueOf(Width) / Double
.valueOf(imageWidth));
rateWidth = rateHeight;
} else {
rateWidth = (Double.valueOf(Height) / Double
.valueOf(imageHeight));
rateHeight = rateWidth;
}
}
info.setQuality(quantity);
MagickImage scaled = image.scaleImage((int) (imageWidth * rateWidth),
(int) (imageHeight * rateHeight)); // 小图片文件的大小.
scaled.setFileName(desc_file);
scaled.writeImage(info);
}
/**
* 生成带水印的缩略图
* @param Src_file
* @param descFile
* @param maxSize
* @param maskString
* @param hasMask
* @throws Exception
*/
public static void ThumbnailFile(String Src_file, String descFile,
int maxSize, String maskString, boolean hasMask) throws Exception {
BufferedImage img = Thumbnail(Src_file, maxSize, maskString, hasMask);
GenerateJPG(descFile, img);
img = null;
}
/**
* 生成缩略图
* @param Src_file
* @param maxSize
* @param maskString
* @param hasMask
* @return
* @throws Exception
*/
public static BufferedImage Thumbnail(String Src_file, int maxSize,
String maskString, boolean hasMask) throws Exception {
logWriter.Info("产生缩略图,源文件:" + Src_file);
Image image = Toolkit.getDefaultToolkit().getImage(Src_file);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
double rateWidth = 1;
double rateHeight = 1;
double imageWidth = image.getWidth(null);
double imageHeight = image.getHeight(null);
if (imageWidth <= maxSize && imageHeight <= maxSize) {
} else {
int Width = maxSize;
int Height = maxSize;
if (imageWidth > imageHeight) {
rateHeight = (Double.valueOf(Width) / Double
.valueOf(imageWidth));
rateWidth = rateHeight;
} else {
rateWidth = (Double.valueOf(Height) / Double
.valueOf(imageHeight));
rateHeight = rateWidth;
}
}
BufferedImage thumbImage = new BufferedImage(
(int) (imageWidth * rateWidth),
(int) (imageHeight * rateHeight), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, (int) (imageWidth * rateWidth),
(int) (imageHeight * rateHeight), null);
if (hasMask && maskString != null && maskString.length() > 0) {
int size = ((int) (imageWidth * rateWidth) * 2)
/ (3 * maskString.length());
graphics2D.setFont(new Font("Arial", 1, size));
//graphics2D.setFont(new Font("宋体", 1, size));
Color col = Color.WHITE;
if (maskString != null) {
for (int i = 0; i < maskString.length(); i++) {
char t = maskString.charAt(i);
col = rotateColor(col);
col = rotateBrightness(col);
graphics2D.setColor(col);
graphics2D.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_ATOP, 0.5F));
graphics2D.drawString(String.valueOf(t),
((int) (imageWidth * rateWidth) - maskString
.length()
* size)
/ 2 + size * i,
(int) (imageHeight * rateHeight) / 2);
}
}
}
graphics2D.dispose();
return thumbImage;
}
/**
* 产生斜体的水印
* @param Src_file
* @param maxSize
* @param maskString
* @param hasMask
* @return
* @throws Exception
*/
public static BufferedImage ThumbnailEx(String Src_file, int maxSize,
String maskString, boolean hasMask) throws Exception {
logWriter.Info("产生缩略图,源文件:" + Src_file);
Image image = Toolkit.getDefaultToolkit().getImage(Src_file);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
double rateWidth = 1;
double rateHeight = 1;
double imageWidth = image.getWidth(null);
double imageHeight = image.getHeight(null);
if (imageWidth <= maxSize && imageHeight <= maxSize) {
} else {
int Width = maxSize;
int Height = maxSize;
if (imageWidth > imageHeight) {
rateHeight = (Double.valueOf(Width) / Double
.valueOf(imageWidth));
rateWidth = rateHeight;
} else {
rateWidth = (Double.valueOf(Height) / Double
.valueOf(imageHeight));
rateHeight = rateWidth;
}
}
BufferedImage thumbImage = new BufferedImage(
(int) (imageWidth * rateWidth),
(int) (imageHeight * rateHeight), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, (int) (imageWidth * rateWidth),
(int) (imageHeight * rateHeight), null);
if (hasMask && maskString != null && maskString.length() > 0) {
int size = ((int) (imageWidth * rateWidth) * 2)
/ (3 * maskString.length());
String fontName = "Arial";
graphics2D.setFont(new Font(fontName, 0, 15));
Color col = Color.WHITE;
if (maskString != null) {
for (int i = 0; i < 1; i++) {
graphics2D.setColor(col);
graphics2D.rotate(-1);
graphics2D.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_ATOP, 0.5F));
for (int m=-10;m<10;m++)
{
for (int n=-10;n<10;n++)
{
graphics2D.drawString(maskString,
200*m+maskString.length()*30,
200*n+maskString.length()*10);
}
}
}
}
}
graphics2D.dispose();
return thumbImage;
}
private static Color rotateColor(Color c) throws Exception {
float[] hsb = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(),
null);
hsb[0] += 0.085;
if (hsb[0] > 1.0)
hsb[0] -= 1.0;
return new Color(Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]));
}
/**
* 转换饱和度
*
* @param c
* 颜色
* @return Color
* @throws Exception
*/
private static Color rotateBrightness(Color c) throws Exception {
float[] hsb = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(),
null);
hsb[2] -= 0.095;
if (hsb[2] > 1.0)
hsb[2] -= 1.0;
return new Color(Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]));
}
}