1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| package image;
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO;
public class ImageDemo { int[][] number = { { 7, 11, 13, 8, 4, 4, 4, 8, 13, 11, 7 }, { 4, 4, 4, 15, 15, 15, 2, 2, 2 }, { 5, 6, 7, 7, 7, 7, 8, 9, 8, 6 }, { 4, 6, 6, 6, 6, 10, 14, 11, 7 }, { 3, 4, 4, 4, 5, 5, 4, 15, 15, 15, 2, 2 }, { 9, 9, 6, 6, 7, 8, 10, 9, 6 }, { 7, 11, 13, 8, 6, 6, 6, 8, 11, 10, 5 }, { 2, 4, 6, 8, 7, 7, 6, 6, 4, 3 }, { 4, 9, 13, 12, 7, 7, 7, 13, 12, 9, 4 }, { 4, 10, 11, 8, 6, 6, 7, 8, 13, 11, 7 }, };
public int[][] grayImage() throws IOException { File file = new File( "C:\\Users\\user\\Desktop\\DeCheckCode\\checkCode\\check9.jpg" ); BufferedImage image = ImageIO.read(file);
int width = image.getWidth(); int height = image.getHeight();
int box[][] = new int[height][width]; for (int i = 0; i < width; i++) { int[] sum = new int[width]; for (int j = 0; j < height; j++) { int rgb[][] = new int[height][width];
rgb[j][i] = image.getRGB(i, j); int r = (rgb[j][i] & 16711680) >> 16; int g = (rgb[j][i] & 65280) >> 8; int b = (rgb[j][i] & 255); int black = (0 << 16) | (0 << 8) | 0; int white = (255 << 16) | (255 << 8) | 255; if (r * r + g * g + b * b < 3 * 128 * 128) { rgb[j][i] = black; box[j][i] = 1; } else { rgb[j][i] = white; box[j][i] = 0; } sum[i] += box[j][i]; image.setRGB(i, j, rgb[j][i]); } } return box; }
public int[] delNoise() throws IOException { int box[][] = grayImage(); int box1[][] = box; for (int i = 0; i < box.length; i++) { for (int j = 0; j < box[i].length; j++) { if (j == box[i].length - 1) { System.out.println(box[i][j]); } else System.out.print(box[i][j]); } } int x = box1.length; int y = box1[0].length; int sum[] = new int[y]; for (int m = 0; m < y; m++) { for (int n = 0; n < x; n++) { sum[m] += box1[n][m]; } } return sum; }
public void getDegital() throws IOException { int sum[] = delNoise(); int[] m = new int[4]; int[] n = new int[4]; int i = 0; for (int z = 0; z < sum.length; z++) { if (z != 0 && sum[z - 1] == 0 && sum[z] != 0) { m[i] = z; } if (sum[z] != 0) { n[i] = n[i] + 1; } if (z != 0 && sum[z] == 0 && sum[z - 1] != 0) { i = i + 1; } }
int[][] Degital = new int[4][]; int[] code = new int[4]; for (int b = 0; b < 4; b++) { Degital[b] = new int[n[b]]; System.arraycopy(sum, m[b], Degital[b], 0, n[b]);
for (int a = 0; a < 10; a++) { boolean e = true; if (Degital[b].length == number[a].length) { for (int d = 0; d < Degital[b].length; d++) { if (Degital[b][d] != number[a][d]) { e = e & false; } else e = e & true; } if (e) { code[b] = a; System.out.print(a + " "); } } } } }
public static void main(String[] args) throws IOException { ImageDemo demo = new ImageDemo(); demo.getDegital(); } }
|