|
|
package com.example.jmacro.wjdr.util;
|
|
|
|
|
|
import cn.hutool.core.img.ImgUtil;
|
|
|
import cn.hutool.core.thread.ThreadUtil;
|
|
|
import com.example.jmacro.wjdr.JMainApplication;
|
|
|
import com.example.jmacro.wjdr.base.ViewRect;
|
|
|
|
|
|
import javax.swing.*;
|
|
|
import java.awt.*;
|
|
|
import java.awt.event.KeyAdapter;
|
|
|
import java.awt.event.KeyEvent;
|
|
|
import java.awt.event.WindowAdapter;
|
|
|
import java.awt.event.WindowEvent;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 图片加载工具
|
|
|
*
|
|
|
* @author wangbing
|
|
|
* @version 0.0.1
|
|
|
* @since 1.8
|
|
|
*/
|
|
|
public class ImageUtil {
|
|
|
|
|
|
private final static Map<String, BufferedImage> cache = new HashMap();
|
|
|
|
|
|
/**
|
|
|
* 加载图片
|
|
|
*
|
|
|
* @param pic 图片
|
|
|
* @return 缓冲区图像
|
|
|
*/
|
|
|
public static BufferedImage load(File pic) {
|
|
|
if (cache.containsKey(pic.getAbsolutePath())) {
|
|
|
return cache.get(pic.getAbsolutePath());
|
|
|
}
|
|
|
BufferedImage read = ImgUtil.read(pic);
|
|
|
cache.put(pic.getAbsolutePath(), read);
|
|
|
return read;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加载多个图片
|
|
|
*
|
|
|
* @param pics 图片集
|
|
|
* @return 缓冲区图像集
|
|
|
*/
|
|
|
public static BufferedImage[] load(File[] pics) {
|
|
|
BufferedImage[] images = new BufferedImage[pics.length];
|
|
|
for (int i = 0; i < images.length; i++) {
|
|
|
images[i] = load(pics[i]);
|
|
|
}
|
|
|
return images;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加载多个图片
|
|
|
*
|
|
|
* @param pics 图片集
|
|
|
* @return 缓冲区图像集
|
|
|
*/
|
|
|
public static List<BufferedImage> load(List<File> pics) {
|
|
|
List<BufferedImage> images = new ArrayList();
|
|
|
for (File file : pics) {
|
|
|
images.add(load(file));
|
|
|
}
|
|
|
return images;
|
|
|
}
|
|
|
|
|
|
public static int[][] getImageRGB(BufferedImage bfImage) {
|
|
|
int width = bfImage.getWidth();
|
|
|
int height = bfImage.getHeight();
|
|
|
int[][] result = new int[width][height];
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
for (int x = 0; x < width; x++) {
|
|
|
// 对某个像素点的RGB编码并存入数据库
|
|
|
result[x][y] = bfImage.getRGB(x, y);
|
|
|
// 单独获取每一个像素点的Alpha, Red,Green,和Blue的值。
|
|
|
// int a = rgb>>24 & 0xff;
|
|
|
// int r = rgb>>16 & 0xff;
|
|
|
// int g = rgb>>8 & 0xff;
|
|
|
// int b = rgb & 0xff;
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 计算图片相似度
|
|
|
*
|
|
|
* @param x
|
|
|
* @param y
|
|
|
* @param smallHeight
|
|
|
* @param smallWidth
|
|
|
* @param bigData
|
|
|
* @param smallData
|
|
|
* @return
|
|
|
*/
|
|
|
public static double calcSimilar(int x, int y, int smallHeight, int smallWidth, int[][] bigData, int[][] smallData) {
|
|
|
int similar = 0;
|
|
|
for (int smallY = 0; smallY < smallHeight; smallY++) {
|
|
|
for (int smallX = 0; smallX < smallWidth; smallX++) {
|
|
|
if (ColorUtil.isSimilar(bigData[x + smallX][y + smallY], smallData[smallX][smallY])) {
|
|
|
similar++;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return (double) similar / (smallWidth * smallHeight);
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
try {
|
|
|
Robot robot = new Robot();
|
|
|
BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(0, 0, 1920, 1080));
|
|
|
show(screenCapture);
|
|
|
} catch (AWTException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 展示屏幕指定区域图片
|
|
|
*
|
|
|
* @param rect
|
|
|
*/
|
|
|
public static void show(ViewRect rect) {
|
|
|
try {
|
|
|
Robot robot = new Robot();
|
|
|
BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(rect.getLeft(), rect.getTop(), rect.getWidth(), rect.getHeight()));
|
|
|
show(screenCapture);
|
|
|
} catch (AWTException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 终端程序并展示图片
|
|
|
*
|
|
|
* @param image 图片
|
|
|
*/
|
|
|
public static void show(BufferedImage image) {
|
|
|
int viewSize = 100;
|
|
|
Image showImage = image;
|
|
|
// 图片太大时进行缩放显示
|
|
|
if (image.getWidth() > viewSize || image.getHeight() > viewSize) {
|
|
|
if (image.getWidth() > image.getHeight()) {
|
|
|
showImage = ImgUtil.scale(showImage, 1.0f * viewSize / image.getWidth());
|
|
|
} else {
|
|
|
showImage = ImgUtil.scale(showImage, 1.0f * viewSize / image.getHeight());
|
|
|
}
|
|
|
}
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
ImgUtil.write(showImage,"PNG",outputStream);
|
|
|
javafx.scene.image.Image image1 = new javafx.scene.image.Image(new ByteArrayInputStream(outputStream.toByteArray()));
|
|
|
JMainApplication.mainController.getPreview().setImage(image1);
|
|
|
}
|
|
|
}
|