- package com.easystructure.utils.system;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.FontMetrics;
- 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 java.io.InputStream;
- import javax.imageio.ImageIO;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGEncodeParam;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- @SuppressWarnings("all")
- public class ImageUtils {
- /**
- * 把图片印刷到图片上
- *
- * @param pressImg
- * -- 水印文件
- * @param targetImg
- * -- 目标文件
- * @param intAlign
- * --位置类型
- */
- public final static void pressImage(String pressImg, String targetImg, int intAlign) {
- 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);
- // 水印文件
- 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);
- if (intAlign == 1) {
- g.drawImage(src_biao, 5, 10, wideth_biao, height_biao, null);
- } else if (intAlign == 2) {
- g.drawImage(src_biao, (wideth - wideth_biao) / 2 - 5, 10, wideth_biao, height_biao, null);
- } else if (intAlign == 3) {
- g.drawImage(src_biao, wideth - wideth_biao - 5, 10, wideth_biao, height_biao, null);
- } else if (intAlign == 4) {
- g.drawImage(src_biao, 5, (height - height_biao) / 2 - 10, wideth_biao, height_biao, null);
- } else if (intAlign == 5) {
- g.drawImage(src_biao, (wideth - wideth_biao) / 2 - 5, (height - height_biao) / 2 - 10, wideth_biao, height_biao, null);
- } else if (intAlign == 6) {
- g.drawImage(src_biao, wideth - wideth_biao - 5, (height - height_biao) / 2 - 10, wideth_biao, height_biao, null);
- } else if (intAlign == 7) {
- g.drawImage(src_biao, 5, height - height_biao - 10, wideth_biao, height_biao, null);
- } else if (intAlign == 8) {
- g.drawImage(src_biao, (wideth - wideth_biao) / 2 - 5, height - height_biao - 10, wideth_biao, height_biao, null);
- } else if (intAlign == 9) {
- g.drawImage(src_biao, wideth - wideth_biao - 5, height - height_biao - 10, 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 strImageText
- * @param fm
- * @return
- * @author googol Feb 9, 2006
- */
- public static int getStringWidth(String strImageText, FontMetrics fm) {
- int intReturn = 0;
- int intCount = strImageText.length();
- char chrImageText[] = strImageText.toCharArray();
- for (int i = 0; i < intCount; i++) {
- int charWidth = fm.charWidth(chrImageText[i]);
- intReturn += charWidth;
- }
- return intReturn += 10;
- }
- /** */
- /**
- * 打印文字水印图片
- *
- * @param pressText
- * --文字
- * @param targetImg
- * -- 目标图片
- * @param fontName
- * -- 字体名
- * @param fontStyle
- * -- 字体样式
- * @param color
- * -- 字体颜色
- * @param fontSize
- * -- 字体大小
- * @param intAlign
- * --位置类型
- */
- public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int intAlign) {
- 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.setColor(color);
- g.setFont(new Font(fontName, fontStyle, fontSize));
- int intWidth = getStringWidth(pressText, g.getFontMetrics());
- if (wideth > intWidth) {
- if (intAlign == 1) {
- g.drawString(pressText, 5, 20);
- } else if (intAlign == 2) {
- g.drawString(pressText, (wideth - intWidth) / 2 - 5, 20);
- } else if (intAlign == 3) {
- g.drawString(pressText, wideth - intWidth - 5, 20);
- } else if (intAlign == 4) {
- g.drawString(pressText, 5, height / 2 - 10);
- } else if (intAlign == 5) {
- g.drawString(pressText, (wideth - intWidth) / 2 - 5, height / 2 - 10);
- } else if (intAlign == 6) {
- g.drawString(pressText, wideth - intWidth - 5, height / 2 - 10);
- } else if (intAlign == 7) {
- g.drawString(pressText, 5, height - 10);
- } else if (intAlign == 8) {
- g.drawString(pressText, (wideth - intWidth) / 2 - 5, height - 10);
- } else if (intAlign == 9) {
- g.drawString(pressText, wideth - intWidth - 5, height - 10);
- }
- } else {
- g.drawString(pressText, 0, height - 10);
- }
- g.dispose();
- FileOutputStream out = new FileOutputStream(targetImg);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(image);
- out.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- /**
- * 只限制宽度生成缩略图,按比例缩放,如果原图宽度比需要生成的图的宽度还小,则简单拷贝一张图
- *
- * @author Dennis
- *
- * 2009-11-17 下午04:41:55
- */
- public static void makeMiniature(String strPicturePath, String strOutPath, int intMinWidth) {
- File _file = new File(strPicturePath);
- Image src;
- try {
- src = ImageIO.read(_file);
- int width = src.getWidth(null);
- int height = src.getHeight(null);
- if (width <= intMinWidth) {
- // 直接copy
- makeMiniature(strPicturePath, strOutPath, width, height);
- } else {
- // 得到缩放比例
- float scale = (float) intMinWidth / width;
- makeMiniature(strPicturePath, strOutPath, intMinWidth, (int) (height * scale - 1));
- }
- } catch (IOException e) {
- System.out.println(e);
- }
- }
- /**
- * 产生一个缩略
- *
- * @param strPicturePath
- * 原图片位置
- * @param strOutPath
- * 生成图片的位置 2009-11-17 下午04:43:50
- */
- public static void makeMiniature(String strPicturePath, String strOutPath, int intMinWidth, int intMinHeight) {
- try {
- String imageFile = strPicturePath;
- File file = new File(imageFile);
- BufferedImage im = null;
- InputStream imageIn = new FileInputStream(file);
- im = ImageIO.read(imageIn);
- int minh = intMinHeight, minw = intMinWidth;
- BufferedImage imout = new BufferedImage(minw, minh, 1);
- Graphics g = imout.getGraphics();
- g.drawImage(im, 0, 0, minw, minh, null);
- imageIn.close();
- File out = new File(strOutPath);
- if (strOutPath.endsWith(".JPG") || strOutPath.endsWith(".jpg")) {
- ImageIO.write(imout, "jpg", out);
- } else if (strOutPath.endsWith(".GIF") || strOutPath.endsWith(".gif")) {
- ImageIO.write(imout, "gif", out);
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- /**
- * 压缩图片文件<br>
- * 先保存原文件,再压缩、上传
- *
- * @param oldFile
- * 要进行压缩的文件全路径
- * @param width
- * 宽度
- * @param height
- * 高度
- * @param quality
- * 质量
- * @param smallIcon
- * 小图片的后缀
- * @return 返回压缩后的文件的全路径
- */
- public static String zipImageFile(String oldFile, int width, int height, float quality, String smallIcon) {
- if (oldFile == null)
- return null;
- String newImage = null;
- try {
- /** 对服务器上的临时文件进行处理 */
- Image srcFile = ImageIO.read(new File(oldFile));
- double rate1 = ((double) srcFile.getWidth(null)) / (double) width + 0.1;
- double rate2 = ((double) srcFile.getHeight(null)) / (double) width + 0.1;
- double rate = rate1 > rate2 ? rate1 : rate2;
- width = (int) (((double) srcFile.getWidth(null)) / rate);
- width = (int) (((double) srcFile.getHeight(null)) / rate);
- /** 宽,高设定 */
- BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
- String filePrex = oldFile.substring(0, oldFile.indexOf('.'));
- /** 压缩后的文件名 */
- newImage = filePrex + smallIcon + oldFile.substring(filePrex.length());
- /** 压缩之后临时存放位置 */
- FileOutputStream out = new FileOutputStream(newImage);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
- /** 压缩质量 */
- jep.setQuality(quality, true);
- encoder.encode(tag, jep);
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return newImage;
- }
- /**
- * 经过缩略图算法的压缩。存放生成图片的路径同源图片。
- *
- * @param oldFile
- * @param width
- * @param height
- * @param smallIcon
- */
- public static void zipImageFileSameOldPath(String oldFile, int intMinWidth, int intMinHeight, String smallIcon) {
- String strPicturePath = oldFile;
- String strOutPath = CommonUtils.addStrToFileName(strPicturePath, smallIcon);
- zipImageFileDifferPath(strPicturePath, strOutPath, intMinWidth, intMinHeight);
- }
- /**
- * 经过缩略图算法的压缩。存放生成图片的路径为指定路径。
- *
- * @param oldFile
- * @param width
- * @param height
- * @param smallIcon
- */
- public static void zipImageFileDifferPath(String oldFilePath, String newFilePath, int intMinWidth, int intMinHeight) {
- BufferedImage im = null;
- BufferedImage imout = null;
- int minh = intMinHeight;
- int minw = intMinWidth;
- try {
- File file = new File(oldFilePath);
- InputStream imageIn = new FileInputStream(file);
- im = ImageIO.read(imageIn);
- imout = new ImageScale().imageZoomOut(im, minw, minh);
- imageIn.close();
- File out = new File(newFilePath);
- if (newFilePath.endsWith(".JPG") || newFilePath.endsWith(".jpg")) {
- ImageIO.write(imout, "jpg", out);
- } else if (newFilePath.endsWith(".GIF") || newFilePath.endsWith(".gif")) {
- ImageIO.write(imout, "gif", out);
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public static void main(String[] args) {
- // pressImage("F:/bisonte.png", "F:/123.jpg", 9);
- pressText("Dennis", "F:/123.jpg", "宋体", Font.PLAIN, Color.RED, 15, 6);
- // createMark("F:/123.jpg", "mmmmnn", Color.RED, 15);
- }
- }
ImageUtils
最新推荐文章于 2025-08-18 15:23:13 发布