package org.sxj.utils;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.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import javax.imageio.ImageIO;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class imageUtil {/** * 把图片印刷到图片上 * * @param pressImg -- * 水印文件 * @param targetImg -- * 目标文件 * @param x * --x坐标 * @param y * --y坐标 * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) */ public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) { try { //目标文件 File _file = new File(targetImg); Image src = ImageIO.read(_file); 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); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); //水印文件 File _filebiao = new File(pressImg); Image src_biao = ImageIO.read(_filebiao); int wideth_biao = src_biao.getWidth(null); int height_biao = src_biao.getHeight(null); g.drawImage(src_biao,x,y, wideth_biao, height_biao, null); //水印文件结束 g.dispose(); FileOutputStream out = new FileOutputStream(targetImg); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 打印文字水印图片 * * @param pressText * --文字 * @param targetImg -- * 目标图片 * @param fontName -- * 字体名 * @param fontStyle -- * 字体样式 * @param color -- * 字体颜色 * @param fontSize -- * 字体大小 * @param x -- * 偏移量 * @param y */ public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color makeColor, int fontSize, int x,int y) { try { File _file = new File(targetImg); Image src = ImageIO.read(_file); int wideth = src.getWidth(null); int height = src.getHeight(null); BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.createGraphics(); g.drawImage(src, 0, 0, wideth, height, null); g.setColor(makeColor); g.setFont(new Font(fontName, fontStyle, fontSize)); // 添加水印的文字和设置水印文字出现的内容 ----位置 g.drawString(pressText, x, y); g.dispose(); FileOutputStream out = new FileOutputStream(targetImg); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } catch (Exception e) { System.out.println(e); } } /** * * 指定长或者宽的最大值来压缩图片 * * @param srcImgPath * :源图片路径 * @param outImgPath * * :输出的压缩图片的路径 * @param maxLength * :长或者宽的最大值 */ public static void compressImage(String srcImgPath, String outImgPath, int maxLength) { // 得到图片 BufferedImage src = InputImage(srcImgPath); if (null != src) { int old_w = src.getWidth(); // 得到源图宽 int old_h = src.getHeight(); // 得到源图长 int new_w = 0; // 新图的宽 int new_h = 0; // 新图的长 // 根据图片尺寸压缩比得到新图的尺寸 new_w = (int) (old_w/maxLength*1.0); new_h = (int) (old_h/maxLength*1.0); // 结束 disposeImage(src, outImgPath, new_w, new_h); } } /** * 将图片按照指定的图片尺寸压缩 * @param srcImgPath :源图片路径 * @param outImgPath :输出的压缩图片的路径 * @param new_w * :压缩后的图片宽 * @param new_h * :压缩后的图片高 s */ public static void compressImage(String srcImgPath, String outImgPath, int new_w, int new_h) { BufferedImage src = InputImage(srcImgPath); disposeImage(src, outImgPath, new_w, new_h); } /** * 处理图片 * @param src * @param outImgPath * @param new_w * @param new_h */ private synchronized static void disposeImage(BufferedImage src, String outImgPath, int new_w, int new_h) { // 得到图片 int old_w = src.getWidth(); // 得到源图宽 int old_h = src.getHeight(); // 得到源图长 BufferedImage newImg = null; // 判断输入图片的类型 switch (src.getType()) { case 13: // png,gifnewImg = new BufferedImage(new_w, new_h, // BufferedImage.TYPE_4BYTE_ABGR); break; default: newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB); break; } Graphics2D g = newImg.createGraphics(); // 从原图上取颜色绘制新图 g.drawImage(src, 0, 0, old_w, old_h, null); g.dispose(); // 根据图片尺寸压缩比得到新图的尺寸 newImg.getGraphics().drawImage( src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0, 0, null); // 调用方法输出图片文件 OutImage(outImgPath, newImg); } /** 图片文件读取 */ private static BufferedImage InputImage(String srcImgPath) { BufferedImage srcImage = null; try { FileInputStream in = new FileInputStream(srcImgPath); srcImage = javax.imageio.ImageIO.read(in); } catch (IOException e) { System.out.println("读取图片文件出错!" + e.getMessage()); e.printStackTrace(); } return srcImage; } /** * 将图片文件输出到指定的路径,并可设定压缩质量 * @param outImgPath * @param newImg * @param * per */ private static void OutImage(String outImgPath, BufferedImage newImg) { // 判断输出的文件夹路径是否存在,不存在则创建 File file = new File(outImgPath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); }// 输出到文件流 try { ImageIO.write(newImg, outImgPath.substring(outImgPath .lastIndexOf(".") + 1), new File(outImgPath)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }package org.sxj.utils;import java.awt.AlphaC