此程序已是3年多前写的,后来没有在深入写下去,图像识别一个很深的领域,得需要很深的数学功底跟思维能力,这个java的程序效率不高,也不能识别变形的或者拉伸的图片,但在那个年代,已经足够用了,大家如果有更好的开源的图像识别代码,请务必来信交流:)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 图片解析引擎,适合做网站验证码的分析。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 首先必须载入样品,解析器将从左到右横向扫描,发现于样本的就自动记录。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 当然本程序不适合样本不是唯一的,也就是说要识别的图片被缩放或者坐标变动和变形本程序无法进行这样的识别。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 如果图片中的颜色变化非常大,此程序可能会有问题,当然了你可以选择一个标准的值做为转换成0,1矩阵的标准。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 样本的制作:请将样本转换成灰度模式,只含有两色最好,当然了不转换我也帮你转换了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Image;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.image.BufferedImage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.File;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.ArrayList;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Iterator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.List;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.imageio.ImageIO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class ImageParser {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// ------------------------------------------------------------ Private Data
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 样本的矩阵
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static List swatches = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 样本的值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static List swatcheValues = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 图片文件的矩阵化
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private byte[][] targetColors;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// ------------------------------------------------------------ Test main method
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 加入样本与其样本对应的数值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String[] files = new String[10];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String[] values = new String[10];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i = 0; i < files.length; i++){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
files[i] = "D:/workspace/SZXClientAPP/res/" + i + ".jpg";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
values[i] = String.valueOf(i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ImageParser parse = new ImageParser(files, values);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long startime = System.currentTimeMillis();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 解析图片
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(parse.parseValue("D:/workspace/SZXClientAPP/res/ValidateNum"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long sincetime = System.currentTimeMillis();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("所花时间 = " + (sincetime - startime));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// ------------------------------------------------------------ Constructors
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 载入所有样本路径与其样本对应的数值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param files
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public ImageParser(String[] files, String[] values) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 只允许样本创建一次即可
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (swatches == null && swatcheValues == null) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int fileslength = files.length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int valueslength = values.length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(fileslength != valueslength){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("样本文件与样本数值不匹配!请重新设置!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
swatches = new ArrayList(fileslength);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
swatcheValues = new ArrayList(valueslength);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (; i < files.length; i++) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
swatches.add(imageToMatrix(files[i]));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
swatcheValues.add(i, values[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(files[i] + " can not be parsed");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public ImageParser() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (swatches == null || swatcheValues == null) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("您未载入样本,请先载入样本!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 解析图片的值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param parseFilePath
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 给出图片路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 返回字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String parseValue(String parseFilePath) throws Exception {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StringBuffer result = new StringBuffer();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
targetColors = imageToMatrix(parseFilePath);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// printMatrix(targetColors);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int height = targetColors.length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int targetWidth = targetColors[0].length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int width = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Iterator it = swatches.iterator();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
while (it.hasNext()) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[][] bytes = (byte[][]) it.next();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int templen = bytes[0].length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (templen > width)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
width = templen;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println("MaxWidth = " + width);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println("MaxHeight = " + height);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int xTag = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
while ((xTag + width) < targetWidth) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
cout: {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Iterator itx = swatches.iterator();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
while (itx.hasNext()) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[][] bytes = (byte[][]) itx.next();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[][] temp = splitMatrix(targetColors, xTag, 0, width, height);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println(i++);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (isMatrixInBigMatrix(bytes, temp)) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
xTag += width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println("new maxtrix: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// printMatrix(temp);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result.append(swatcheValues.get(i));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break cout;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
xTag++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return result.toString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// ------------------------------------------------------------ Private methods
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 判断一个矩阵是否在另外的矩阵中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param source
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 源矩阵
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param bigMatrix
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 大的矩阵
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 如果存在就返回 true
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
private static final boolean isMatrixInBigMatrix(byte[][] source, byte[][] bigMatrix) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (source == bigMatrix)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (source == null || bigMatrix == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (source.length > bigMatrix.length)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i = 0; i < source.length; i++) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (source[i].length > bigMatrix[i].length)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (ArrayIndexOutOfBoundsException e) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int height = source.length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int width = source[0].length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int x = 0, y = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i = 0, j = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int count = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int comparecount = height * width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (; i < bigMatrix.length - height + 1; i++) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (j = 0; j < bigMatrix[i].length - width + 1; j++) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
cout: {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
count = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int k = i; k < height + i; k++) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
y = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int l = j; l < width + j; l++) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println("bytes[" + x + "][" + y + "]"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// + " = " + source[x][y] + ", " + "other["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// + k + "][" + l + "] = " + bigMatrix[k][l]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((source[x][y] & bigMatrix[k][l]) == source[x][y]) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
count++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
} else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break cout;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
y++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println("count = " + count);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (count == comparecount)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 切割矩阵
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param source
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 源矩阵
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param x
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* X坐标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param y
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Y坐标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param width
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 矩阵的宽度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param height
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 矩阵的高度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 切割后的矩阵
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
private static final byte[][] splitMatrix(byte[][] source, int x, int y, int width, int height) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[][] resultbytes = new byte[height][width];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i = y, k = 0; i < height + y; i++, k++) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int j = x, l = 0; j < width + x; j++, l++) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
resultbytes[k][l] = source[i][j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println("source[" + i + "][" + j + "]" + " = " +
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// source[i][j] + ", " + "resultbytes["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// + k + "][" + l + "] = " + resultbytes[k][l]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return resultbytes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 图片转换成矩阵数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param filePath
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 文件路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 返回矩阵
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 可能会抛出异常
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
private byte[][] imageToMatrix(String filePath) throws Exception {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 读入文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Image image = ImageIO.read(new File(filePath));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int w = image.getWidth(null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int h = image.getHeight(null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BufferedImage src = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
src.getGraphics().drawImage(image, 0, 0, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[][] colors = new byte[h][w];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i = 0; i < h; i++) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int j = 0; j < w; j++) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int rgb = src.getRGB(j, i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 像素进行灰度处理
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String sRed = Integer.toHexString(rgb).substring(2, 4);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String sGreen = Integer.toHexString(rgb).substring(4, 6);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String sBlank = Integer.toHexString(rgb).substring(6, 8);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long ired = Math.round((Integer.parseInt(sRed, 16) * 0.3 + 0.5d));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long igreen = Math.round((Integer.parseInt(sGreen, 16) * 0.59 + 0.5d));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long iblank = Math.round((Integer.parseInt(sBlank, 16) * 0.11 + 0.5d));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long al = ired + igreen + iblank;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// if (al > 127)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.print(" " + " ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.print(" " + "1");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.print(" " + (tempint > = maxint ? 0 : 1));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println("tempInt = " + tempint);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/* 将图像转换成0,1 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 此处的值可以将来修改成你所需要判断的值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
colors[i][j] = (byte) (al > 127 ? 0 : 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return colors;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 打印矩阵
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param matrix
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
private static final void printMatrix(byte[][] matrix) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i = 0; i < matrix.length; i++) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int j = 0; j < matrix[i].length; j++) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (matrix[i][j] == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.print(" ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.print(" 1");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}