package ;
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.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
/**
* 图片处理
* @author
*
*/
public final class ImageUtil {
/**
* 图片水印
* @param pressImg 水印图片
* @param targetImg 目标图片
* @param x 修正值 默认在中间
* @param y 修正值 默认在中间
* @param alpha 透明度
*/
public static void pressImage(String pressImg, String targetImg, final int x, final int y,final float alpha) {
try {
File img = new File(targetImg);
Image src = ImageIO.read(img);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
//水印文件
Image srcBiao = ImageIO.read(new File(pressImg));
int widethBiao = srcBiao.getWidth(null);
int heightBiao = srcBiao.getHeight(null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.drawImage(srcBiao, (wideth - widethBiao) / NumberUtils.INT_2, (height - heightBiao) / NumberUtils.INT_2, widethBiao, heightBiao, null);
//水印文件结束
g.dispose();
ImageIO.write((BufferedImage) image, "jpg", img);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文字水印
* @param pressText 水印文字
* @param targetImg 目标图片
* @param fontName 字体名称
* @param fontStyle 字体样式
* @param color 字体颜色
* @param fontSize 字体大小
* @param x 修正值
* @param y 修正值
* @param alpha 透明度
*/
public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha) {
try {
File img = new File(targetImg);
Image src = ImageIO.read(img);
int width = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
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)) / NumberUtils.INT_2 + x, (height - fontSize) /NumberUtils.INT_2 + y);
g.dispose();
ImageIO.write((BufferedImage) image, "jpg", img);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 缩放
* @param filePath 图片路径
* @param height 高度
* @param width 宽度
* @param bb 比例不对时是否需要补白
*/
public static void resize(String filePath, int height, int width, boolean bb) {
try {
double ratio = 0.0; //缩放比例
File f = new File(filePath);
BufferedImage bi = ImageIO.read(f);
Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
//计算比例
if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
if (bi.getHeight() > bi.getWidth()) {
ratio = (new Integer(height)).doubleValue() / bi.getHeight();
} else {
ratio = (new Integer(width)).doubleValue() / bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}
if (bb) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
if (width == itemp.getWidth(null)) {
g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / NumberUtils.INT_2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
}else{
g.drawImage(itemp, (width - itemp.getWidth(null)) / NumberUtils.INT_2, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
}
g.dispose();
itemp = image;
}
ImageIO.write((BufferedImage) itemp, "jpg", f);
} catch (IOException e) {
e.printStackTrace();
}
}
/*
public static void main(String[] args) throws IOException {
//pressImage("G:\\imgtest\\sy.jpg", "G:\\imgtest\\test1.jpg", 0, 0, 0.5f);
//pressText("我是文字水印", "G:\\imgtest\\test1.jpg", "黑体", 36, Color.white, 80, 0, 0, 0.3f);
resize("文件地址",10,1,true);
}
*/
/**
* 获取长度
* @param text 文本
* @return int
*/
public 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 += NumberUtils.INT_2;
} else {
length += 1;
}
}
return length / NumberUtils.INT_2;
}
// -----------------------------验证码--------------------------------
// 验证码图片中可以出现的字符集,可根据需要修改
/**
* * 功能:生成彩色验证码图片 参数width为生成图片的宽度,参数height为生成图片的高度 参数os为页面的输出流
* @param width 宽
* @param height 高
* @param os 流
* @return 文字
*/
public static String getCertPic(int width, int height, OutputStream os) {
final int aw=60;
final int ah=20;
char[] mapTable = { 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E','f', 'F',
'g', 'G', 'h', 'H', 'i', 'J', 'k', 'K', 'L', 'M','N', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z','2', '3', '4', '5', '6', '7', '8', '9' };
if (width <= 0){
width = aw;
}
if (height <= 0){
height = ah;
}
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 设定背景色
final int color=0xdcdcdc;
g.setColor(new Color(color));
g.fillRect(0, 0, width - 1, height - 1);
// 取随机产生的验证码
String strEnsure = "";
// 4代表4位验证码,如果要生成更多位的验证码,则加大数值
final int size=4;
for (int i = 0; i < size; i++) {
strEnsure += mapTable[(int) (mapTable.length * Math.random())];
}
// 将验证码显示到图像中,如果要生成更多位的验证码,增加drawString语句
final int fontSize=18;
g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline", Font.PLAIN, fontSize));
String str = strEnsure.substring(0, 1);
g.drawString(str, new Integer("8"), new Integer("17"));
g.setColor(Color.red);
str = strEnsure.substring(1, NumberUtils.INT_2);
g.drawString(str, new Integer("20"),new Integer("15"));
g.setColor(Color.DARK_GRAY);
str = strEnsure.substring(NumberUtils.INT_2, NumberUtils.INT_3);
g.drawString(str, new Integer("35"), new Integer("18"));
g.setColor(Color.red);
str = strEnsure.substring(new Integer("3"), new Integer("4"));
g.drawString(str, new Integer("45"), new Integer("15"));
Random rand = new Random();
// 随机产生10个干扰点,如果要更多干扰点只需加大i的上限值
g.setColor(Color.black);
final int length=30;
for (int i = 0; i < length; i++) {
int x = rand.nextInt(width);
int y = rand.nextInt(height);
g.drawOval(x, y, 1, 1);
// g.drawLine(x, y, 1, 1);
}
// 释放图形上下文
g.dispose();
try {
// 输出图像到页面
ImageIO.write(image, "JPEG", os);
} catch (IOException e) {
return "";
}
return strEnsure;
}
}
使用:
/**
* 获取验证码
*/
public void getYzm(){
try{
HttpSession session = getSession();
String str = ImageUtil.getCertPic(0, 0, ServletActionContext.getResponse().getOutputStream());
session.setAttribute("loginyzm", str);
}catch(Exception e){
e.printStackTrace();
}
}