![](file:///C:\Users\zhen\AppData\Roaming\Tencent\Users\563448396\QQ\WinTemp\RichOle\SV}]FI5A_~5}0]2(92K(K6C.jpg)
![](file:///C:\Users\zhen\AppData\Roaming\Tencent\Users\563448396\QQ\WinTemp\RichOle\SV}]FI5A_~5}0]2(92K(K6C.jpg)
![](file:///C:\Users\zhen\AppData\Roaming\Tencent\Users\563448396\QQ\WinTemp\RichOle\SV}]FI5A_~5}0]2(92K(K6C.jpg)
![](http://img.blog.csdn.net/20130827201302718?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWVpeW91dG9uZ2d1bw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
packageorg.chinasb.client; importjava.awt.Color;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException; importjavax.imageio.ImageIO; publicclass BinaryTest { publicstatic void main(String[] args) throwsIOException { BufferedImage bufferedImage = ImageIO.read(newFile("D:/passCodeAction.jpg")); inth = bufferedImage.getHeight(); intw = bufferedImage.getWidth(); // 灰度化 int[][] gray = newint[w][h]; for(intx = 0; x < w; x++) { for(inty = 0; y < h; y++) { intargb = bufferedImage.getRGB(x, y); intr = (argb >> 16) & 0xFF; intg = (argb >> 8) & 0xFF; intb = (argb >> 0) & 0xFF; intgrayPixel = (int) ((b * 29+ g * 150+ r * 77+ 128) >> 8); gray[x][y] = grayPixel; } } // 二值化 intthreshold = ostu(gray, w, h); BufferedImage binaryBufferedImage = newBufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY); for(intx = 0; x < w; x++) { for(inty = 0; y < h; y++) { if(gray[x][y] > threshold) { gray[x][y] |= 0x00FFFF; }else{ gray[x][y] &= 0xFF0000; } binaryBufferedImage.setRGB(x, y, gray[x][y]); } } // 矩阵打印 for(inty = 0; y < h; y++) { for(intx = 0; x < w; x++) { if(isBlack(binaryBufferedImage.getRGB(x, y))) { System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(); } ImageIO.write(binaryBufferedImage,"jpg",newFile("D:/code.jpg")); } publicstatic boolean isBlack(intcolorInt) { Color color = newColor(colorInt); if(color.getRed() + color.getGreen() + color.getBlue() <= 300) { returntrue; } returnfalse; } publicstatic boolean isWhite(intcolorInt) { Color color = newColor(colorInt); if(color.getRed() + color.getGreen() + color.getBlue() > 300) { returntrue; } returnfalse; } publicstatic int isBlackOrWhite(intcolorInt) { if(getColorBright(colorInt) < 30|| getColorBright(colorInt) > 730) { return1; } return0; } publicstatic int getColorBright(intcolorInt) { Color color = newColor(colorInt); returncolor.getRed() + color.getGreen() + color.getBlue(); } publicstatic int ostu(int[][] gray, intw, inth) { int[] histData = newint[w * h]; // Calculate histogram for(intx = 0; x < w; x++) { for(inty = 0; y < h; y++) { intred = 0xFF& gray[x][y]; histData[red]++; } } // Total number of pixels inttotal = w * h; floatsum = 0; for(intt = 0; t < 256; t++) sum += t * histData[t]; floatsumB = 0; intwB = 0; intwF = 0; floatvarMax = 0; intthreshold = 0; for(intt = 0; t < 256; t++) { wB += histData[t]; // Weight Background if(wB == 0) continue; wF = total - wB; // Weight Foreground if(wF == 0) break; sumB += (float) (t * histData[t]); floatmB = sumB / wB; // Mean Background floatmF = (sum - sumB) / wF; // Mean Foreground // Calculate Between Class Variance floatvarBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF); // Check if new maximum found if(varBetween > varMax) { varMax = varBetween; threshold = t; } } returnthreshold; }}