|
@@ -0,0 +1,340 @@
|
|
|
+package com.iamberry.wechat.utils;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import java.util.Iterator;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.imageio.ImageReadParam;
|
|
|
+import javax.imageio.ImageReader;
|
|
|
+import javax.imageio.stream.ImageInputStream;
|
|
|
+import javax.swing.*;
|
|
|
+
|
|
|
+
|
|
|
+import java.awt.Image;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description:图片处理工具
|
|
|
+ * @author:liuyc
|
|
|
+ * @time:2016年5月27日 上午10:18:00
|
|
|
+ */
|
|
|
+public class ImageUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param srcFile源图片、targetFile截好后图片全名、startAcross 开始截取位置横坐标、StartEndlong开始截图位置纵坐标、width截取的长,hight截取的高
|
|
|
+ * @Description:截图
|
|
|
+ * @author:liuyc
|
|
|
+ * @time:2016年5月27日 上午10:18:23
|
|
|
+ */
|
|
|
+ public static void cutImage(String srcFile, String targetFile, int startAcross, int StartEndlong, int width,
|
|
|
+ int hight) throws Exception {
|
|
|
+ // 取得图片读入器
|
|
|
+ Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");
|
|
|
+ ImageReader reader = readers.next();
|
|
|
+ // 取得图片读入流
|
|
|
+ InputStream source = new FileInputStream(srcFile);
|
|
|
+ ImageInputStream iis = ImageIO.createImageInputStream(source);
|
|
|
+ reader.setInput(iis, true);
|
|
|
+ // 图片参数对象
|
|
|
+ ImageReadParam param = reader.getDefaultReadParam();
|
|
|
+ Rectangle rect = new Rectangle(startAcross, StartEndlong, width, hight);
|
|
|
+ param.setSourceRegion(rect);
|
|
|
+ BufferedImage bi = reader.read(0, param);
|
|
|
+ ImageIO.write(bi, targetFile.split("\\.")[1], new File(targetFile));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param files 要拼接的文件列表
|
|
|
+ * @param type1 横向拼接, 2 纵向拼接
|
|
|
+ * @Description:图片拼接 (注意:必须两张图片长宽一致哦)
|
|
|
+ * @author:liuyc
|
|
|
+ * @time:2016年5月27日 下午5:52:24
|
|
|
+ */
|
|
|
+ public static void mergeImage(String[] files, int type, String targetFile) {
|
|
|
+ int len = files.length;
|
|
|
+ if (len < 1) {
|
|
|
+ throw new RuntimeException("图片数量小于1");
|
|
|
+ }
|
|
|
+ File[] src = new File[len];
|
|
|
+ BufferedImage[] images = new BufferedImage[len];
|
|
|
+ int[][] ImageArrays = new int[len][];
|
|
|
+ for (int i = 0; i < len; i++) {
|
|
|
+ try {
|
|
|
+ src[i] = new File(files[i]);
|
|
|
+ images[i] = ImageIO.read(src[i]);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ int width = images[i].getWidth();
|
|
|
+ int height = images[i].getHeight();
|
|
|
+ ImageArrays[i] = new int[width * height];
|
|
|
+ ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
|
|
|
+ }
|
|
|
+ int newHeight = 0;
|
|
|
+ int newWidth = 0;
|
|
|
+ for (int i = 0; i < images.length; i++) {
|
|
|
+ // 横向
|
|
|
+ if (type == 1) {
|
|
|
+ newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();
|
|
|
+ newWidth += images[i].getWidth();
|
|
|
+ } else if (type == 2) {// 纵向
|
|
|
+ newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();
|
|
|
+ newHeight += images[i].getHeight();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (type == 1 && newWidth < 1) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (type == 2 && newHeight < 1) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成新图片
|
|
|
+ try {
|
|
|
+ BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.SCALE_FAST);
|
|
|
+ int height_i = 0;
|
|
|
+ int width_i = 0;
|
|
|
+ for (int i = 0; i < images.length; i++) {
|
|
|
+ if (type == 1) {
|
|
|
+ ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0,
|
|
|
+ images[i].getWidth());
|
|
|
+ width_i += images[i].getWidth();
|
|
|
+ } else if (type == 2) {
|
|
|
+ ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth);
|
|
|
+ height_i += images[i].getHeight();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //输出想要的图片
|
|
|
+ targetFile.replaceAll("\\\\", "/");
|
|
|
+
|
|
|
+ ImageIO.write(ImageNew, "png", new File(targetFile));
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description:小图片贴到大图片形成一张图(合成)
|
|
|
+ * @author:liuyc
|
|
|
+ * @time:2016年5月27日 下午5:51:20
|
|
|
+ */
|
|
|
+ public static final void overlapImage(String bigPath, String smallPath, String outFile) {
|
|
|
+ try {
|
|
|
+ BufferedImage big = ImageIO.read(new File(bigPath));
|
|
|
+ BufferedImage small = ImageIO.read(new File(smallPath));
|
|
|
+ Graphics2D g = big.createGraphics();
|
|
|
+ int x = (big.getWidth() - small.getWidth()) / 2;
|
|
|
+ int y = (big.getHeight() - small.getHeight()) / 2;
|
|
|
+ g.drawImage(small, x, y, small.getWidth(), small.getHeight(), null);
|
|
|
+ g.dispose();
|
|
|
+ ImageIO.write(big, outFile.split("\\.")[1], new File(outFile));
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /* public static void main(String[] args) throws IOException {
|
|
|
+ Font font = new Font("04b_08", Font.PLAIN, 30);//字体
|
|
|
+ *//*String sourceImg="D:\\test\\123.jpg"; //源图片地址
|
|
|
+ String targetImg="D:\\test\\watermark\\123.jpg"; //新存储的地址
|
|
|
+ String watermark="图片出处:www.baidu.com";//水印内容
|
|
|
+ String watermarkUrl="D:\\test\\zy.png";*//*
|
|
|
+ String sourceImg="C:/Users/Administrator/Desktop/人格测试/0309测试题,结果图/0309测试题,结果图/kfx-2.png"; //源图片地址
|
|
|
+ String targetImg="C:/Users/Administrator/Desktop/人格测试/0309测试题,结果图/0309测试题,结果图/kfx1.png"; //新存储的地址
|
|
|
+ String watermark="柳康健";//水印内容
|
|
|
+ String watermarkUrl="C:/Users/Administrator/Desktop/人格测试/0309测试题,结果图/0309测试题,结果图/kfx2.png";
|
|
|
+ Color color=new Color(0);
|
|
|
+ addWatermark(sourceImg, targetImg,watermark, color,font);
|
|
|
+ markImgMark(watermarkUrl,sourceImg,targetImg);
|
|
|
+ }*/
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 为图片添加图片水印
|
|
|
+ * @param watermarkUrl 原图
|
|
|
+ * @param source 水印图片
|
|
|
+ * @param output 制作完成的图片
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static String markImgMark(String watermarkUrl, String source, String output) throws IOException {
|
|
|
+ String result = "添加图片水印出错";
|
|
|
+ File file = new File(source);
|
|
|
+ Image img = ImageIO.read(file);
|
|
|
+ int width = img.getWidth(null);//水印宽度
|
|
|
+ int height = img.getHeight(null);//水印高
|
|
|
+ BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
+ Graphics2D g = bi.createGraphics();
|
|
|
+ g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
|
+ g.drawImage(img.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
|
|
|
+ ImageIcon imgIcon = new ImageIcon(watermarkUrl);
|
|
|
+ Image con = imgIcon.getImage();
|
|
|
+ float clarity = 0.6f;//透明度
|
|
|
+ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, clarity));
|
|
|
+ g.drawImage(con, 10, 10, null);//水印的位置
|
|
|
+ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
|
|
|
+ g.dispose();
|
|
|
+ File sf = new File(output);
|
|
|
+ ImageIO.write(bi, "jpg", sf); // 保存图片
|
|
|
+ System.out.println("添加图片水印成功");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 设置文字水印
|
|
|
+ * @param sourceImg 源图片路径
|
|
|
+ * @param targetImg 保存的图片路径
|
|
|
+ * @param watermark 水印内容
|
|
|
+ * @param color 水印颜色
|
|
|
+ * @param font 水印字体
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void addWatermark(String sourceImg, String targetImg, String watermark,Color color,Font font) throws IOException {
|
|
|
+
|
|
|
+ File srcImgFile = new File(sourceImg);
|
|
|
+ Image srcImg = ImageIO.read(srcImgFile);
|
|
|
+ int srcImgWidth = srcImg.getWidth(null);
|
|
|
+ int srcImgHeight = srcImg.getHeight(null);
|
|
|
+ BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.SCALE_FAST);
|
|
|
+ Graphics2D g = bufImg.createGraphics();
|
|
|
+ g.drawImage(srcImg, 0, 0, null);
|
|
|
+ g.setColor(color);
|
|
|
+ g.setFont(font);
|
|
|
+ g.setBackground(Color.WHITE);
|
|
|
+ //设置水印的坐标
|
|
|
+ /*int x = srcImgWidth - (g.getFontMetrics(g.getFont()).charsWidth(watermark.toCharArray(), 0, watermark.length())+20);
|
|
|
+ int y = srcImgHeight - 25;*/
|
|
|
+ int x = 80;
|
|
|
+ int y = 110;
|
|
|
+ //处理昵称位置
|
|
|
+ switch (watermark.length()){
|
|
|
+ case 1:
|
|
|
+ x += 130;
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ x += 150;
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ x += 140;
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ x += 130;
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ x += 120;
|
|
|
+ break;
|
|
|
+ case 6:
|
|
|
+ x += 110;
|
|
|
+ break;
|
|
|
+ case 7:
|
|
|
+ x += 90;
|
|
|
+ break;
|
|
|
+ case 8:
|
|
|
+ x += 80;
|
|
|
+ break;
|
|
|
+ case 9:
|
|
|
+ x += 70;
|
|
|
+ break;
|
|
|
+ case 10:
|
|
|
+ x += 60;
|
|
|
+ break;
|
|
|
+ case 11:
|
|
|
+ x += 50;
|
|
|
+ break;
|
|
|
+ case 12:
|
|
|
+ x += 35;
|
|
|
+ break;
|
|
|
+ case 13:
|
|
|
+ x += 25;
|
|
|
+ break;
|
|
|
+ case 14:
|
|
|
+ x += 15;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ g.drawString(watermark, x, y); //加水印
|
|
|
+ g.dispose();
|
|
|
+ // 输出图片
|
|
|
+ FileOutputStream outImgStream = new FileOutputStream(targetImg);
|
|
|
+ ImageIO.write(bufImg, "png", outImgStream);
|
|
|
+ System.out.println("添加水印完成");
|
|
|
+ outImgStream.flush();
|
|
|
+ outImgStream.close();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void yPic(){//纵向处理图片
|
|
|
+ try {
|
|
|
+ /* 1 读取第一张图片*/
|
|
|
+ File fileOne = new File("F:/wechatN/wechat-wechat-web/target/wechat-wechat-web-1.0.0/WEB-INF/common/images/send/o3G6nw7DSmcQwSy3LtNhEcPYMqRI2.png");
|
|
|
+ BufferedImage imageFirst = ImageIO.read(fileOne);
|
|
|
+ int width = imageFirst.getWidth();// 图片宽度
|
|
|
+ int height = imageFirst.getHeight();// 图片高度
|
|
|
+ int[] imageArrayFirst = new int[width * height];// 从图片中读取RGB
|
|
|
+ imageArrayFirst = imageFirst.getRGB(0, 0, width, height, imageArrayFirst, 0, width);
|
|
|
+
|
|
|
+ /* 1 对第二张图片做相同的处理 */
|
|
|
+ File fileTwo = new File("F:/wechatN/wechat-wechat-web/target/wechat-wechat-web-1.0.0/WEB-INF/common/images/db2.png");
|
|
|
+ BufferedImage imageSecond = ImageIO.read(fileTwo);
|
|
|
+ int width2 = imageSecond.getWidth();// 图片宽度
|
|
|
+ int height2 = imageSecond.getHeight();// 图片高度
|
|
|
+
|
|
|
+ int[] imageArraySecond = new int[width * height];
|
|
|
+ imageArraySecond = imageSecond.getRGB(0, 0, width2, height2, imageArraySecond, 0, width2);
|
|
|
+
|
|
|
+ // 生成新图片
|
|
|
+ BufferedImage imageResult = new BufferedImage(width, height + height2,BufferedImage.SCALE_FAST);
|
|
|
+ imageResult.setRGB(0, 0, width, height, imageArrayFirst, 0, width);// 设置左半部分的RGB
|
|
|
+ imageResult.setRGB(0, height, width, height2, imageArraySecond, 0, width);// 设置右半部分的RGB
|
|
|
+ File outFile = new File("F:/wechatN/wechat-wechat-web/target/wechat-wechat-web-1.0.0/WEB-INF/common/images/send/o3G6nw7DSmcQwSy3LtNhEcPYMqRI211.png");
|
|
|
+ ImageIO.write(imageResult, "jpg", outFile);// 写图片
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ /*Font font = new Font("04b_08", Font.PLAIN, 25);//字体
|
|
|
+ String sourceImg="C:/Users/Administrator/Desktop/人格测试/0309测试题,结果图/0309测试题,结果图/kfx-2.png"; //源图片地址
|
|
|
+ String targetImg="C:/Users/Administrator/Desktop/人格测试/0309测试题,结果图/0309测试题,结果图/kfx.png"; //新存储的地址
|
|
|
+ String watermark="柳康健";//水印内容
|
|
|
+ Color color=new Color(0,0,0,150);
|
|
|
+ addWatermark(sourceImg, targetImg,watermark, color,font);
|
|
|
+ *//*markImgMark(watermarkUrl,sourceImg,targetImg);*//*
|
|
|
+ String[] files = new String[2];
|
|
|
+ files[0] = "C:/Users/Administrator/Desktop/人格测试/0309测试题,结果图/0309测试题,结果图/kfx-2.png";
|
|
|
+ files[1] = "C:/Users/Administrator/Desktop/人格测试/0309测试题,结果图/0309测试题,结果图/kfx-1.png";
|
|
|
+ mergeImage(files,2,"C:/Users/Administrator/Desktop/人格测试/0309测试题,结果图/0309测试题,结果图/kfx.png"); */
|
|
|
+ Font font = new Font("04b_08", Font.BOLD, 22);//字体
|
|
|
+ String sourceImg="C:/Users/Administrator/Desktop/人格测试/0309测试题,结果图/0309测试题,结果图/kfx-1.png"; //源图片地址
|
|
|
+ String targetImg="C:/Users/Administrator/Desktop/人格测试/0309测试题,结果图/0309测试题,结果图/kfx.png"; //新存储的地址
|
|
|
+ String watermark="柳康健健健健健健健健健健:";//水印内容
|
|
|
+ Color color=new Color(0,0,0,150);
|
|
|
+ /*addWatermark(sourceImg, targetImg,watermark, color,font);*/
|
|
|
+ /*markImgMark(watermarkUrl,sourceImg,targetImg);*/
|
|
|
+ String[] files = new String[2];
|
|
|
+ files[0] = "F:/wechatN/wechat-wechat-web/target/wechat-wechat-web-1.0.0/WEB-INF/common/images/send/o3G6nw7DSmcQwSy3LtNhEcPYMqRI2.png";
|
|
|
+ files[1] = "F:/wechatN/wechat-wechat-web/target/wechat-wechat-web-1.0.0/WEB-INF/common/images/db2.png";
|
|
|
+ /*mergeImage(files,2,"F:/wechatN/wechat-wechat-web/target/wechat-wechat-web-1.0.0/WEB-INF/common/images/send/o3G6nw7DSmcQwSy3LtNhEcPYMqRI211.png");*/
|
|
|
+ /*mergeImage(files,2,"F:/wechatN/wechat-wechat-web/target/wechat-wechat-web-1.0.0/WEB-INF/common/images/send/1.png");*/
|
|
|
+ /*yPic();*/
|
|
|
+ //zipImageFile("C:/Users/Administrator/Desktop/人格测试/0312测试结果图/0312测试结果图/JG.png", 750, 1942, 1f, "x2");
|
|
|
+ //saveMinPhoto("C:/Users/Administrator/Desktop/人格测试/0312测试结果图/0312测试结果图/JG.png", "C:/Users/Administrator/Desktop/人格测试/0312测试结果图/0312测试结果图/JG2.png", 139, 0.9d);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|