|
@@ -1,19 +1,18 @@
|
|
|
package com.iamberry.wechat.tools;
|
|
|
-import java.awt.AlphaComposite;
|
|
|
-import java.awt.Color;
|
|
|
-import java.awt.Font;
|
|
|
-import java.awt.Graphics2D;
|
|
|
-import java.awt.Image;
|
|
|
+
|
|
|
+import com.mortennobel.imagescaling.ResampleFilters;
|
|
|
+import com.mortennobel.imagescaling.ResampleOp;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
import java.awt.geom.AffineTransform;
|
|
|
import java.awt.image.AffineTransformOp;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
-
|
|
|
-import javax.imageio.ImageIO;
|
|
|
-
|
|
|
-import com.mortennobel.imagescaling.ResampleFilters;
|
|
|
-import com.mortennobel.imagescaling.ResampleOp;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* 图片工具类, 图片水印,文字水印,缩放,补白等 <br>
|
|
@@ -66,6 +65,198 @@ public final class ImageUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+// long l = System.currentTimeMillis();
|
|
|
+// reSize("D://abcdefgj1.jpg", 0.5, 0.5);
|
|
|
+// pressImage("D://Test.jpg", "D://test111.jpg", 350, 500, 1);
|
|
|
+ // 设置水印
|
|
|
+// press("D://Test.jpg", "D://test111.jpg", "深圳爱贝源科技有限公司", 350, 100, 350, 500);
|
|
|
+// System.out.println(System.currentTimeMillis() - l);
|
|
|
+ // resize("C://pic//4.jpg", 1000, 500, true);'
|
|
|
+
|
|
|
+
|
|
|
+ String newImg = "E:\\java-work\\iamberry-watero\\watero-wechat-web\\target\\watero-wechat-web-1.0.0\\common\\images\\movable\\1.jpg";
|
|
|
+ String headImg = "E:\\java-work\\iamberry-watero\\watero-wechat-web\\target\\watero-wechat-web-1.0.0\\common\\images\\movable\\7a7620fb_head.jpg";
|
|
|
+ String codeImg = "E:\\java-work\\iamberry-watero\\watero-wechat-web\\target\\watero-wechat-web-1.0.0\\common\\images\\movable\\7a7620fb_code.jpg";
|
|
|
+ String text = "小茗同学";
|
|
|
+ String alltext = "慈母手中线,\n游子身上衣。\n临行密密缝,\n意恐迟迟归。\n谁言寸草心,\n报得三春晖!";
|
|
|
+// String alltext = "You are the best mom thatsdafsadasfsadsaf a son ever had.\nSometimes it may not seem like it, but I really do love you.God could not be everywhere and therefore he made mothers.";
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+ ImageUtils.pressPost(newImg,
|
|
|
+ headImg,PosterNumUtils.HEAD_X,PosterNumUtils.HEAD_Y,
|
|
|
+ codeImg,PosterNumUtils.CODE_X,PosterNumUtils.CODE_Y,
|
|
|
+ text,PosterNumUtils.TEXT_X,PosterNumUtils.TEXT_Y,new Color(221,106,140),PosterNumUtils.TEXT_FONT_SIZE,
|
|
|
+ alltext,PosterNumUtils.TEXT_CONTENT_X,PosterNumUtils.TEXT_CONTENT_Y,new Color(0,0,0),PosterNumUtils.TEXT_CONTENT_FONT_SIZE
|
|
|
+ );
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+// String ENCODE = "utf-8";
|
|
|
+// String name = java.net.URLDecoder.decode( "%E5%B0%8F%E8%8C%97%E5%90%8C%E5%AD%A6", ENCODE);
|
|
|
+//
|
|
|
+// System.out.println(name);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成海报
|
|
|
+ */
|
|
|
+ public static void pressPost(String targetImg,
|
|
|
+ String srcImg1, int imgx1, int imgy1,
|
|
|
+ String srcImg2, int imgx2, int imgy2,
|
|
|
+ String headText, int x, int y,Color xyColorName,int xySizeNum,
|
|
|
+ String pressText, int m, int n,Color mnColorName,int mnSizeNum
|
|
|
+ ) throws Exception{
|
|
|
+ float alpha = 1;
|
|
|
+
|
|
|
+ File file = new File(targetImg);
|
|
|
+ Image image = ImageIO.read(file);
|
|
|
+
|
|
|
+ int width = image.getWidth(null);
|
|
|
+ int height = image.getHeight(null);
|
|
|
+ BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
+ Graphics2D g = bufferedImage.createGraphics();
|
|
|
+ g.drawImage(image, 0, 0, width, height, null);
|
|
|
+
|
|
|
+ //居中水印文字
|
|
|
+ customContenText(g,width,height,headText,"微软雅黑",xySizeNum,xyColorName,Font.PLAIN,x,y);
|
|
|
+
|
|
|
+ //水印内容
|
|
|
+ List<String> strList = new ArrayList<>();
|
|
|
+ String strs[] = pressText.split("\n");
|
|
|
+ for (String s : strs) {
|
|
|
+ strList.addAll(getStrArr(s));
|
|
|
+ }
|
|
|
+ for (String strCon : strList) { //循环行生成文本
|
|
|
+ customText(g,width,height,strCon,"微软雅黑",mnSizeNum,mnColorName,Font.PLAIN,m,n);
|
|
|
+ n += PosterNumUtils.FONT_LINE_SPACING;
|
|
|
+ }
|
|
|
+
|
|
|
+ Image tarImage = ImageIO.read(new File(srcImg1));
|
|
|
+ g.drawImage(tarImage, imgx1, imgy1, tarImage.getWidth(null), tarImage.getHeight(null), null); // 水印文件结束
|
|
|
+
|
|
|
+ Image tarImage2 = ImageIO.read(new File(srcImg2));
|
|
|
+ g.drawImage(tarImage2, imgx2, imgy2, tarImage2.getWidth(null), tarImage2.getHeight(null), null); // 水印文件结束
|
|
|
+
|
|
|
+ g.dispose();
|
|
|
+ ImageIO.write(bufferedImage, targetImg.substring(targetImg.lastIndexOf(".") + 1), file);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 单行文字转为多行,每行字符数量为 FONT_NUMBER
|
|
|
+ * @param text
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<String> getStrArr(String text){
|
|
|
+ if(text == null){
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ int i = 0,num=0;
|
|
|
+ List<String> strList = new ArrayList<>();
|
|
|
+ StringBuffer temp = new StringBuffer();
|
|
|
+ while (text.length() > num){
|
|
|
+ if(i >= PosterNumUtils.FONT_NUMBER){
|
|
|
+ strList.add(temp.toString());
|
|
|
+ temp = new StringBuffer();
|
|
|
+ i = 0;
|
|
|
+ }
|
|
|
+ char c = text.charAt(num); // 调用字符串的charAt()方法取得第一个字符就是了
|
|
|
+ String x = String.valueOf(c);
|
|
|
+ boolean isWarp = false;
|
|
|
+ try {
|
|
|
+ String newString = new String(x.getBytes("GB2312"), "ISO-8859-1");
|
|
|
+ i+=newString.length();
|
|
|
+ if(32 == c){ //英文状态下,本行字母写不下,换行
|
|
|
+ String later = text.substring(num,text.length());
|
|
|
+ if(later.indexOf(" ") > -1 && later.indexOf(" ",later.indexOf(" ")+1) > 1){
|
|
|
+ String a = later.substring(0,later.indexOf(" ",later.indexOf(" ")+1)); //str.indexOf("a", str.indexOf("a")+1)
|
|
|
+ if((a.length()+i) > PosterNumUtils.FONT_NUMBER){
|
|
|
+ strList.add(temp.toString());
|
|
|
+ temp = new StringBuffer();
|
|
|
+ i = 0;
|
|
|
+ isWarp = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if(!isWarp){
|
|
|
+ temp.append(c);
|
|
|
+ }
|
|
|
+ num++;
|
|
|
+ }
|
|
|
+ strList.add(temp.toString());
|
|
|
+ return strList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加文字水印 -- 居中
|
|
|
+ * @param g
|
|
|
+ * @param width 图片宽
|
|
|
+ * @param height 图片高
|
|
|
+ * @param pressText 大小
|
|
|
+ * @param fontName 字体
|
|
|
+ * @param fontSize 大小
|
|
|
+ * @param color 颜色
|
|
|
+ * @param fontStyle 加粗 Font.PLAIN
|
|
|
+ * @param x -1 为居中 不为-1 就是左边距
|
|
|
+ * @param y -1 为居中 不为-1 就是上边距
|
|
|
+ */
|
|
|
+ public static void customContenText(Graphics2D g, int width, int height,String pressText, String fontName, int fontSize, Color color,int fontStyle, int x, int y) {
|
|
|
+ try {
|
|
|
+ g.setFont(new Font(fontName, fontStyle, fontSize));
|
|
|
+ g.setColor(color);
|
|
|
+ FontMetrics fm = g.getFontMetrics();
|
|
|
+ int stringWidth = fm.stringWidth(pressText);
|
|
|
+ int stringAscent = fm.getAscent();
|
|
|
+ int stringDescent = fm.getDescent ();
|
|
|
+ if(x == -1){
|
|
|
+ x = width / 2 - stringWidth / 2;
|
|
|
+ }
|
|
|
+ if(y == -1){
|
|
|
+ y = height / 2 + (stringAscent - stringDescent) / 2;
|
|
|
+ }
|
|
|
+ g.drawString(pressText, x, y);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加文字水印
|
|
|
+ * 定位
|
|
|
+ */
|
|
|
+ public static void customText(Graphics2D g, int width, int height,String pressText, String fontName, int fontSize, Color color,int fontStyle, int x, int y) {
|
|
|
+ try {
|
|
|
+ float alpha = 1;
|
|
|
+ g.setFont(new Font(fontName, fontStyle, fontSize));
|
|
|
+ g.setColor(color);
|
|
|
+ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
|
|
|
+
|
|
|
+ int width_1 = fontSize * getLength(pressText);
|
|
|
+ int height_1 = fontSize;
|
|
|
+ int widthDiff = width - width_1;
|
|
|
+ int heightDiff = height - height_1;
|
|
|
+ if(x < 0){
|
|
|
+ x = widthDiff / 2;
|
|
|
+ }else if(x > widthDiff){
|
|
|
+ x = widthDiff;
|
|
|
+ }
|
|
|
+ if(y < 0){
|
|
|
+ y = heightDiff / 2;
|
|
|
+ }else if(y > heightDiff){
|
|
|
+ y = heightDiff;
|
|
|
+ }
|
|
|
+ g.drawString(pressText, x, y + height_1);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 添加文字水印
|
|
|
* @param targetImg 目标图片路径,如:C://myPictrue//1.jpg
|
|
@@ -217,13 +408,5 @@ public final class ImageUtils {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
- public static void main(String[] args) throws IOException {
|
|
|
- long l = System.currentTimeMillis();
|
|
|
-// reSize("D://abcdefgj1.jpg", 0.5, 0.5);
|
|
|
-// pressImage("D://Test.jpg", "D://test111.jpg", 350, 500, 1);
|
|
|
- // 设置水印
|
|
|
- press("D://Test.jpg", "D://test111.jpg", "深圳爱贝源科技有限公司", 350, 100, 350, 500);
|
|
|
- System.out.println(System.currentTimeMillis() - l);
|
|
|
- // resize("C://pic//4.jpg", 1000, 500, true);
|
|
|
- }
|
|
|
+
|
|
|
}
|