???????????
1 // ?????
2 public static void main(String[] args) throws IOException {
3     // ??????
4     File imageFile = new File("c:/1.jpg");
5     Image image = ImageIO.read(imageFile);
6     // ????????
7     image = toGrayscale(image);
8     // ??С??32x32???????
9     image = scale(image);
10     // ??????????????
11     int[] pixels = getPixels(image);
12     // ????????????
13     int averageColor = getAverageOfPixelArray(pixels);
14     // ????????????????饗???????????У?
15     pixels = getPixelDeviateWeightsArray(pixels?? averageColor);
16     // ????????????????????????????????????沽????????????飩
17     int hammingDistance = getHammingDistance(pixels?? pixels);
18     // ????????????????????????Χ [0.0?? 1.0]
19     double similarity = calSimilarity(hammingDistance);
20 }
21
22 // ??????Image???????????BufferedImage????????????????
23 public static BufferedImage convertToBufferedFrom(Image srcImage) {
24     BufferedImage bufferedImage = new BufferedImage(srcImage.getWidth(null)??
25             srcImage.getHeight(null)?? BufferedImage.TYPE_INT_ARGB);
26     Graphics2D g = bufferedImage.createGraphics();
27     g.drawImage(srcImage?? null?? null);
28     g.dispose();
29     return bufferedImage;
30 }
31
32 // ?????????
33 public static BufferedImage toGrayscale(Image image) {
34     BufferedImage sourceBuffered = convertToBufferedFrom(image);
35     ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
36     ColorConvertOp op = new ColorConvertOp(cs?? null);
37     BufferedImage grayBuffered = op.filter(sourceBuffered?? null);
38     return grayBuffered;
39 }
40
41 // ??????32x32?????????
42 public static Image scale(Image image) {
43     image = image.getScaledInstance(32?? 32?? Image.SCALE_SMOOTH);
44     return image;
45 }
46
47 // ???????????
48 public static int[] getPixels(Image image) {
49     int width = image.getWidth(null);
50     int height = image.getHeight(null);
51     int[] pixels = convertToBufferedFrom(image).getRGB(0?? 0?? width?? height??
52             null?? 0?? width);
53     return pixels;
54 }
55
56 // ????????????????????
57 public static int getAverageOfPixelArray(int[] pixels) {
58     Color color;
59     long sumRed = 0;
60     for (int i = 0; i < pixels.length; i++) {
61         color = new Color(pixels[i]?? true);
62         sumRed += color.getRed();
63     }
64     int averageRed = (int) (sumRed / pixels.length);
65     return averageRed;
66 }
67
68 // ??????????????????饗?????????
69 public static int[] getPixelDeviateWeightsArray(int[] pixels??final int averageColor) {
70     Color color;
71     int[] dest = new int[pixels.length];
72     for (int i = 0; i < pixels.length; i++) {
73         color = new Color(pixels[i]?? true);
74         dest[i] = color.getRed() - averageColor > 0 ? 1 : 0;
75     }
76     return dest;
77 }
78
79 // ????????????????????????????????????????????????
80 public static int getHammingDistance(int[] a?? int[] b) {
81     int sum = 0;
82     for (int i = 0; i < a.length; i++) {
83         sum += a[i] == b[i] ? 0 : 1;
84     }
85     return sum;
86 }
87
88 // ???????????????????
89 public static double calSimilarity(int hammingDistance){
90     int length = 32*32;
91     double similarity = (length - hammingDistance) / (double) length;
92
93     // ????????????????????
94     similarity = java.lang.Math.pow(similarity?? 2);
95     return similarity;
96 }
????UI???????????????????????????£????JDK1.5????
????http://download.csdn.net/detail/u011088871/7711833
?????????? run.bat ????????????????????? :)