📄 imageutils.java
字号:
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输出
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -