jar代码,与框架无关,写的很Low,能够达到大图包含小图的目的,无需第三方JAR包:
package com.raiyi.pict;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
/**
* 大图是否包含小图,主要思路是将大图rgb的值转成二维数组,为了方面计算再转成List,
* 同理小图也是,然后确认包含关系
*
* @author Nana
*
*/
public class PictRGBCompare {
public static void main(String[] args) {
long start = System.currentTimeMillis();
int[][] p1 = getImageGRB("D://t1.jpg");// 大图,转二位数组
int[][] p10 = getImageGRB("D://t10.jpg"); // 小图,转二位数组
List<List<String>> al21 = array2List(p1);// 转list
List<List<String>> al210 = array2List(p10);// 转list
System.out.println(al210);
int trueCount = 0; // 大图包含小图rgb值的行数
for (int al210i = 0; al210i < al210.size(); al210i++) {// 遍历小的集合
List<String> al210iLoops = al210.get(al210i);
for (int al21i = 0; al21i < al21.size(); al21i++) {// 遍历大的集合
List<String> al21iLoops = al21.get(al21i);
if (al21iLoops.containsAll(al210iLoops)) {
// System.out.print(al21i + ",");
trueCount++; // FIXME:此处可能存在问题
}
}
// System.out.println(al210i);
}
if (trueCount >= al210.size()) {// 包含行数出现的次数大于等于小图的行数默认包含,此处太暴力了,XXX:美好一点的话判断小图的出现大图的起始位置及结束位置,其他包含的排除
System.out.println("包含");
} else {
System.out.println("不包含");
}
System.out.println("cost time : " + System.currentTimeMillis() - start);
}
@SuppressWarnings("unused")
private static void formatArrays(int[][] num) {
for (int x = 0; x < num.length; x++) { // 定位行
for (int y = 0; y < num[x].length; y++) { // 定位每行的元素个数
System.out.print(addZeroForNum(num[x][y] + "") + ",");
}
System.out.println();
}
}
/**
* 转数组
*
* @param aas
* @return
*/
private static List<List<String>> array2List(int[][] aas) {
List<List<String>> listTest = new ArrayList<List<String>>();
for (int i = 0; i < aas.length; i++) {
List<String> columnList = new ArrayList<String>();
for (int j = 0; j < aas[i].length; j++) {
columnList.add(addZeroForNum(aas[i][j] + ""));
}
listTest.add(columnList);
}
return listTest;
}
/**
* 左补齐8位,输出的rbg集合方便对比,为啥是8位?因为RGB的最大值是FFFFFF=16777215
*
* @param str
* @return
*/
private static String addZeroForNum(String str) {
int strLen = str.length();
if (strLen < 8) {
while (strLen < 8) {
StringBuffer sb = new StringBuffer();
sb.append("0").append(str);// 左补0
str = sb.toString();
strLen = str.length();
}
}
return str;
}
/**
* 根据BufferedImage获取图片RGB数组
*
* @param bfImage
* @return
*/
public static int[][] getImageGRB(String pictPath) {
File file = new File(pictPath);
BufferedImage bi = null;
try {
bi = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
int width = bi.getWidth();
int height = bi.getHeight();
int[][] result = new int[height][width];
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++) {
result[h][w] = bi.getRGB(w, h) & 0xFFFFFF;
}
}
return result;
}
}随便写的