package com.yqzx.common.util;
|
// @formatter:off
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import javax.imageio.ImageIO;
|
import java.awt.*;
|
import java.awt.image.BufferedImage;
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
import java.security.SecureRandom;
|
import java.util.Base64;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* .-~~~~~~~~~-._ _.-~~~~~~~~~-.
|
* __.' @Author ~. .~ 代码无Bug `.__
|
* .'// liu.q \./ (秘籍) \\`.
|
* .'// [916000612@qq.com] | 欲练神功 引刀自宫 \\`.
|
* .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
|
* .'//.-" 2019-06-24 `-. | .-' 15:43 "-.\\`.
|
* .'//______.============-.. \ | / ..-============.______\\`.
|
* .'______________________________\|/______________________________`.
|
*
|
* @Description :
|
*/
|
// @formatter:on
|
@Slf4j
|
public class VerifyImgUtil {
|
private static int T_WIDTH = 320; //源文件宽度
|
private static int T_HEIGHT = 180; //源文件高度
|
|
|
/*
|
* @Author : liu.q [916000612@qq.com]
|
* @Date : 2019-06-24 15:46
|
* @Description : 随机生成抠图坐标
|
*/
|
public static Map<String, Integer> generateCutoutCoordinates(int w, int h) {
|
Map<String, Integer> result = new HashMap();
|
int x = new SecureRandom().nextInt(T_WIDTH - w);
|
int y = new SecureRandom().nextInt(T_HEIGHT - h);
|
while (x < w + 10) {
|
x = new SecureRandom().nextInt(275);
|
}
|
result.put("x", x);
|
result.put("y", y);
|
return result;
|
}
|
|
/*
|
* @Author : liu.q [916000612@qq.com]
|
* @Date : 2019-06-24 16:22
|
* @Description :生成滑块
|
*/
|
public static Map<String, BufferedImage> initSlidingBlock(BufferedImage sourceImg, int[][] templateImgData, int x, int y, int w, int h) throws Exception {
|
// 支持alpha通道的rgb图像
|
BufferedImage newTemplateImg = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
|
|
for (int i = 0; i < templateImgData.length; i++) {
|
for (int j = 0; j < templateImgData[0].length; j++) {
|
int rgb = templateImgData[i][j];
|
if (rgb == 1) {
|
int rgb_ori = sourceImg.getRGB(x + i, y + j);
|
int r = (0xff & rgb_ori);
|
int g = (0xff & (rgb_ori >> 8));
|
int b = (0xff & (rgb_ori >> 16));
|
rgb_ori = r + (g << 8) + (b << 16) + (255 << 24);
|
newTemplateImg.setRGB(i, j, rgb_ori);
|
|
//创建遮罩层
|
BufferedImage cover = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
|
Graphics2D coverG2 = (Graphics2D) cover.getGraphics();
|
coverG2.setColor(Color.BLACK);
|
coverG2.fillRect(0, 0, 10, 10);
|
coverG2.dispose();
|
Graphics2D bgG2 = (Graphics2D) sourceImg.getGraphics();
|
//开启透明度
|
bgG2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
|
//描绘
|
bgG2.drawImage(cover, i + x, y + j, 1, 1, null);
|
//结束透明度
|
bgG2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
|
bgG2.dispose();
|
}
|
if (rgb == 0) {
|
//不用搭理
|
}
|
if (rgb == 2) {
|
//秒边
|
newTemplateImg.setRGB(i, j, 16777215);
|
sourceImg.setRGB(x + i, y + j, 16777215);
|
}
|
}
|
}
|
Map<String, BufferedImage> result = new HashMap<>();
|
result.put("newTemplateImg", newTemplateImg);
|
result.put("newSourceImg", sourceImg);
|
return result;
|
}
|
|
|
/*
|
* @Author : liu.q [916000612@qq.com]
|
* @Date : 2019-06-21 15:37
|
* @Description : 获取base64
|
*/
|
public static String getBase64(BufferedImage bi) {
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
String res = "";
|
try {
|
ImageIO.write(bi, "png", outputStream);
|
res = Base64.getEncoder().encodeToString(outputStream.toByteArray());
|
res = "data:image/png;base64," + res;
|
outputStream.close();
|
} catch (IOException e) {
|
log.error(e.getMessage());
|
}
|
return res;
|
}
|
|
/*
|
* @Author : liu.q [916000612@qq.com]
|
* @Date : 2019-06-24 17:22
|
* @Description : 生成抠图图形矩阵
|
*/
|
public static int[][] getTemplateData(BufferedImage bi) {
|
int[][] data = new int[bi.getWidth()][bi.getHeight()];
|
for (int i = 0; i < bi.getWidth(); i++) {
|
for (int j = 0; j < bi.getHeight(); j++) {
|
int rgb = bi.getRGB(i, j);
|
int leftRgb = -1, rightRgb = -1, upRgb = -1, downRgb = -1;
|
if (j > 0)
|
leftRgb = bi.getRGB(i, j - 1);
|
if (j < bi.getHeight() - 1)
|
rightRgb = bi.getRGB(i, j + 1);
|
if (i > 0)
|
upRgb = bi.getRGB(i - 1, j);
|
if (i < bi.getWidth() - 1)
|
downRgb = bi.getRGB(i + 1, j);
|
if (rgb == -1 && (i == 0 || i == bi.getWidth() - 1 || j == 0 || j == bi.getHeight() - 1)) {
|
data[i][j] = 2; //边
|
} else if (rgb == -1 && (leftRgb != -1 || rightRgb != -1 || upRgb != -1 || downRgb != -1)) {
|
data[i][j] = 2;//边
|
} else if (rgb == -1) {
|
data[i][j] = 1;//内容区域
|
} else {
|
data[i][j] = 0;//透明区域
|
}
|
}
|
}
|
return data;
|
}
|
}
|