This example for you to share the Java fingerprint identification and image recognition source code for your reference, the specific contents are as follows
Main class:
Import Java.awt.image.BufferedImage;
Import java.util.ArrayList;
Import java.util.List; public class Similarimagesearch {/** * @param args */public static void main (string[] args) {List<str
ing> hashcodes = new arraylist<string> ();
String filename = imagehelper.path + "\\images\\";
String hashcode = null;
for (int i = 0; i < 6; i++) {hashcode = producefingerprint (filename + "example" + (i + 1) + ". jpg");
Hashcodes.add (hashcode);
} System.out.println ("Resources:");
System.out.println (Hashcodes);
System.out.println ();
String Sourcehashcode = producefingerprint (filename + "source.jpg");
System.out.println ("Source:");
System.out.println (Sourcehashcode);
System.out.println (); for (int i = 0; i < hashcodes.size (); i++) {int difference = hammingdistance (Sourcehashcode, Hashcodes.get (i
));
System.out.print ("Hamming Distance:" "+difference+"); if (difference==0) {System.out.println ("source.jpg picture with example" + (i+1) + ". jpg");
}else if (difference<=5) {System.out.println ("source.jpg picture is similar to Example" + (i+1) + ". jpg");
}else if (difference<=10) {System.out.println ("source.jpg picture with example" + (i+1) + ". jpg somewhat similar");
}else if (difference>10) {System.out.println ("source.jpg picture is completely different from example" + (i+1) + ". jpg");
}}/** * calculates Hamming distance (Hamming distance).
* If the different data bits are not more than 5, it means that two pictures are similar, if more than 10, it is two different pictures. * @param sourcehashcode Source hashcode * @param hashcode hashcode/public static int hammingdistance (String sour
Cehashcode, String hashcode) {int difference = 0;
int len = Sourcehashcode.length ();
for (int i = 0; i < len; i++) {if (Sourcehashcode.charat (i)!= Hashcode.charat (i)) {difference + +;
} return difference; /** * Generate picture fingerprint * @param filename * @return picture fingerprint/public static String Producefingerprint (String filename) {bufferedimage Source = imagehelper.readpngimage (filename);//read file int width = 8;
int height = 8;
The first step is to reduce the size. Reduces the picture to the 8x8 size, which is 64 pixels in total.
The role of this step is to remove the details of the picture, only to retain the structure, light and other basic information, discard the different size, proportion of the picture differences.
BufferedImage thumb = imagehelper.thumb (source, width, height, false);
The second step is to simplify the color. The reduced image will be converted to level 64 grayscale.
That is, all pixels have only 64 colors in total.
int[] pixels = new int[width * height]; for (int i = 0; i < width; i++) {for (int j = 0; j < height; J +) {Pixels[i * height + j] = Imagehel
Per.rgbtogray (Thumb.getrgb (i, j));
}//Step three to calculate the average.
Calculates the grayscale average of all 64 pixels.
int avgpixel = imagehelper.average (pixels);
The fourth step is to compare the pixel grayscale. Compare the grayscale of each pixel with the average.
is greater than or equal to the average, recorded as 1, less than the average, and recorded as 0.
Int[] Comps = new int[width * height];
for (int i = 0; i < comps.length i++) {if (Pixels[i] >= avgpixel) {comps[i] = 1;
else {comps[i] = 0;
}//Step fifth, compute the hash value. The comparison result of the previous step, grouped in theTogether, it constitutes a 64-bit integer, which is the fingerprint of this picture.
The order of the combinations is not important, just make sure all the pictures are in the same order.
StringBuffer hashcode = new StringBuffer (); for (int i = 0; i < Comps.length. i+= 4) {int result = comps[i] * (int) Math.pow (2, 3) + comps[i + 1] * (int) Ma
Th.pow (2, 2) + Comps[i + 2] * (int) Math.pow (2, 1) + Comps[i + 2];
Hashcode.append (Binarytohex (result));
After you get the fingerprint, you can compare the different pictures and see how many of the 64 digits are not the same.
return hashcode.tostring ();
/** * binary to hexadecimal * @param int binary * @return Char hex * * private static char binarytohex (int binary) {
char ch = ';
switch (binary) {case 0:ch = ' 0 ';
Break
Case 1:ch = ' 1 ';
Break
Case 2:ch = ' 2 ';
Break
Case 3:ch = ' 3 ';
Break
Case 4:ch = ' 4 ';
Break
Case 5:ch = ' 5 ';
Break
Case 6:ch = ' 6 ';
Break
Case 7:ch = ' 7 ';
Break
Case 8:ch = ' 8 ';
Break
Case 9:ch = ' 9 ';Break
Case 10:ch = ' a ';
Break
Case 11:ch = ' B ';
Break
Case 12:ch = ' C ';
Break
Case 13:ch = ' d ';
Break
Case 14:ch = ' e ';
Break
Case 15:ch = ' f ';
Break
Default:ch = ';
return ch;
}
}
Tool class:
Import Java.awt.AlphaComposite;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import java.awt.RenderingHints;
Import Java.awt.geom.AffineTransform;
Import Java.awt.image.BufferedImage;
Import Java.awt.image.ColorModel;
Import Java.awt.image.WritableRaster;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Javax.imageio.ImageIO;
Import com.sun.image.codec.jpeg.ImageFormatException;
Import Com.sun.image.codec.jpeg.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGImageDecoder;
Import Com.sun.image.codec.jpeg.JPEGImageEncoder; /** * Picture Tool class, mainly for image watermark processing * * @author 025079 * @version [version number, 2011-11-28] * @see [Related class/method] * @since [Product/Module Version] * * Publi
C class Imagehelper {//Project root directory path public static final String path = System.getproperty ("User.dir"); /** * Generate thumbnails <br/> * Save: Imageio.write (BufferedimAge, imgtype[jpg/png/...], File); * @param source * Original picture * @param width * thumbnail width * @param height * thumbnail height * @param b * is equal to scaling * * /public static BufferedImage thumb (bufferedimage source, int width, int height, Boolean b) {//Targetw,targeth, respectively, representing the target
Long and wide int type = Source.gettype ();
BufferedImage target = null;
Double SX = (double) width/source.getwidth ();
Double sy = (double) height/source.getheight ();
if (b) {if (sx > sy) {sx = SY;
width = (int) (SX * SOURCE.GETWIDTH ());
else {sy = SX;
Height = (int) (SY * source.getheight ());
} if (type = = Bufferedimage.type_custom) {//handmade ColorModel cm = Source.getcolormodel ();
WritableRaster raster = cm.createcompatiblewritableraster (width, height);
Boolean alphapremultiplied = Cm.isalphapremultiplied ();
target = new BufferedImage (cm, raster, alphapremultiplied, null);
else target = new BufferedImage (width, height, type); Graphics2D g = Target.creategraphics ();
Smoother than Exlax:g.setrenderinghint (renderinghints.key_rendering, renderinghints.value_render_quality);
G.drawrenderedimage (source, affinetransform.getscaleinstance (SX, SY));
G.dispose ();
return target;
/** * Image Watermark * * @param imgpath * @param markpath * Watermark Picture * @param x * watermark is located in the upper-left corner of the picture X coordinate value * @param y * watermark is at the y-coordinate of the upper-left corner of the picture * @param alpha * watermark Transparency 0.1f ~ 1.0f */public static void watermark (String i Mgpath, String markpath, int x, int y, float Alpha) {try {//load pending picture file image img = imageio.read (new file (Imgpath)
);
BufferedImage image = New BufferedImage (Img.getwidth (null), img.getheight (null), BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics2D g = image.creategraphics ();
G.drawimage (IMG, 0, 0, NULL);
Load watermark picture file image Src_biao = imageio.read (new file (Markpath));
G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha));
G.drawimage (Src_biao, x, y, null);
G.dispose (); Save the processed files fileOutputStream out = new FileOutputStream (Imgpath);
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (image);
Out.close ();
catch (Exception e) {e.printstacktrace (); }/** * Text watermark * * @param imgpath * @param text * watermark text * @param font * Watermark Font information * @param Color * Watermark Font Colors * @param x * watermark is located in the upper-left corner of the picture x coordinates * @param y * watermark is at the y-coordinate of the upper-left corner of the picture * @param Alpha Watermark Transparency 0.1f ~ 1.0f */public static void Textmark (string imgpath, string text, font font, color color, int x, int y, flo
At Alpha) {try {font dfont = (Font = null)? New Font ("XXFarEastFont-Arial"): Font;
Image img = imageio.read (new File (Imgpath));
BufferedImage image = New BufferedImage (Img.getwidth (null), img.getheight (null), BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics2D g = image.creategraphics ();
G.drawimage (IMG, 0, 0, NULL);
G.setcolor (color);
G.setfont (Dfont);
G.setcomposite (Alphacomposite.getinstance (Alphacomposite.src_atop, Alpha));
g.DrawString (text, x, y);
G.dispose ();
FileOutputStream out = new FileOutputStream (Imgpath);
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (image);
Out.close ();
catch (Exception e) {System.out.println (e); }/** * Read JPEG picture * @param filename filename * @return bufferedimage Picture object/public static BufferedImage Readjpegimag
E (String filename) {try {InputStream ImageIn = new FileInputStream (filename);
Gets the input encoder, encodes the file stream in jpg format Jpegimagedecoder decoder = Jpegcodec.createjpegdecoder (ImageIn);
Get the encoded image object BufferedImage Sourceimage = Decoder.decodeasbufferedimage ();
return sourceimage;
catch (FileNotFoundException e) {e.printstacktrace ();
catch (Imageformatexception e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
return null; /** * Read JPEG picture * @param filename filename * @return bufferedimage Picture object/public static BufferedImage Readpngimage (St Ring FilenaMe) {try {file Inputfile = new file (filename);
BufferedImage sourceimage = Imageio.read (inputfile);
return sourceimage;
catch (FileNotFoundException e) {e.printstacktrace ();
catch (Imageformatexception e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
return null; /** * Gray value calculation * @param pixels pixel * @return int grayscale value/public static int rgbtogray (int pixels) {//int _alpha = (
Pixels >>) & 0xFF;
int _red = (pixels >>) & 0xFF;
int _green = (pixels >> 8) & 0xFF;
int _blue = (pixels) & 0xFF;
return (int) (0.3 * _red + 0.59 * _green + 0.11 * _blue);
/** * Computes the average of the array * @param pixels array * @return INT average * * * public static int average (int[] pixels) {float m = 0;
for (int i = 0; i < pixels.length ++i) {m + = Pixels[i];
} m = M/pixels.length;
return (int) m;
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.