Contoh -contoh dalam artikel ini berbagi kode sumber pengakuan sidik jari Java dan pengenalan gambar untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
Kategori Utama:
Impor java.awt.image.bufferedImage; import java.util.arraylist; import java.util.list; kelas publik serupa dengan penelitian { / ** * @param args * / public static main (string [] args) {list <string> hashcodes = arraylist baru <string> () {list <string> hashcodes = arraylist baru <string> () () {List <string> hashcodes = new Arraylist <String> () () {List <string> hashcodes = new Arraylist <String> () () {List <string> hashcodes = new Arraylist <String> () () {List <string> hashcodes = new Arraylist <String> () (); String filename = imageHelper.path + "// gambar //"; String hashcode = null; untuk (int i = 0; i <6; i ++) {hashcode = produceFingerPrint (fileName + "example" + (i + 1) + ".jpg"); hashcodes.add (hashCode); } System.out.println ("Sumber Daya:"); System.out.println (HashCodes); System.out.println (); String SourceHashCode = ProduceFingerPrint (FileName + "Source.jpg"); System.out.println ("Sumber:"); System.out.println (SourceHashCode); System.out.println (); untuk (int i = 0; i <hashcodes.size (); i ++) {int perbedaan = hammingdistance (sourceHashCode, hashcodes.get (i)); System.out.print ("jarak hamming:"+perbedaan+""); if (perbedaan == 0) {System.out.println ("Sumber.jpg gambar sama dengan contoh"+(i+1)+". jpg"); } lain jika (perbedaan <= 5) {System.out.println ("source.jpg gambar sangat mirip dengan contoh"+(i+1)+". jpg"); } lain jika (perbedaan <= 10) {System.out.println ("source.jpg gambar agak mirip dengan contoh"+(i+1)+". jpg"); } lain jika (perbedaan> 10) {System.out.println ("source.jpg gambar sama sekali berbeda dari contoh"+(i+1)+". jpg"); }}} /*** Hitung "jarak hamming". * Jika bit data yang berbeda tidak melebihi 5, itu berarti bahwa kedua gambar itu sangat mirip; Jika mereka lebih besar dari 10, itu berarti bahwa ini adalah dua gambar yang berbeda. * @param sourceHashCode Sumber hashCode * @param hashcode perbandingan hashCode */ public static int hammingDistance (string sourceHashCode, string hashcode) {int perbedaan = 0; int len = sourceHashCode.length (); untuk (int i = 0; i <len; i ++) {if (sourceHashCode.charat (i)! = hashcode.charat (i)) {perbedaan ++; }} kembali perbedaan; }/ *** Hasilkan sidik jari gambar* @param file nama file* @return gambar sidik jari*/ string statis public produceFingerprint (string fileName) {bufferedImage source = ImageHelper.readpngimage (nama file); // Baca file int int lebar = 8; tinggi int = 8; // Langkah pertama adalah mengurangi ukurannya. // Kecilkan gambar ke ukuran 8x8, dengan total 64 piksel. Tujuan dari langkah ini adalah untuk menghapus detail gambar, hanya menyimpan informasi dasar seperti struktur, terang dan gelap, dan meninggalkan perbedaan dalam gambar yang disebabkan oleh berbagai ukuran dan proporsi. BufferedImage Thumb = ImageHelper.Thumb (Sumber, Lebar, Tinggi, Salah); // Langkah kedua adalah menyederhanakan warnanya. // Putar gambar yang dikurangi ke level 64 Grayscale. Dengan kata lain, hanya ada 64 warna di semua piksel. int [] piksel = int baru [lebar * tinggi]; untuk (int i = 0; i <width; i ++) {for (int j = 0; j <height; j ++) {piksel [i * height+j] = imageHelper.rgbtogray (thumb.getrgb (i, j)); }} // Langkah ketiga adalah menghitung rata -rata. // Hitung rata -rata skala abu -abu dari semua 64 piksel. int avgpixel = ImageHelper.Aungkan (piksel); // Langkah 4, bandingkan skala abu -abu piksel. // Bandingkan grayscale dari setiap piksel dengan nilai rata -rata. Jika lebih besar dari atau sama dengan nilai rata -rata, itu dilambangkan sebagai 1; Jika kurang dari nilai rata -rata, itu dilambangkan sebagai 0. int [] comps = int int [lebar * tinggi]; untuk (int i = 0; i <comps.length; i ++) {if (piksel [i]> = avgpixel) {comps [i] = 1; } else {comps [i] = 0; }} // Langkah 5, Hitung nilai hash. // Gabungkan hasil perbandingan dari langkah sebelumnya bersama-sama untuk membentuk bilangan bulat 64-bit, yang merupakan sidik jari dari gambar ini. Urutan kombinasi tidak penting, pastikan bahwa semua gambar berada dalam urutan yang sama. StringBuffer hashCode = new StringBuffer (); untuk (int i = 0; i <comps.length; i + = 4) {int result = comps [i] * (int) math.pow (2, 3) + comps [i + 1] * (int) math.pow (2, 2) + comps [i + 2] * (int) math.pow (2, 1) + comps [i + 2]; hashCode.Append (BinaryToHex (Hasil)); } // Setelah mendapatkan sidik jari, Anda dapat membandingkan berbagai gambar untuk melihat berapa banyak bit yang berbeda di 64-bit. return hashcode.tostring (); } / ** * Konversi biner menjadi hex * @param int biner * @return char hex * / private static char binarytoHex (int biner) {char ch = ''; switch (biner) {case 0: ch = '0'; merusak; Kasus 1: ch = '1'; merusak; Kasus 2: ch = '2'; merusak; Kasus 3: ch = '3'; merusak; Kasus 4: ch = '4'; merusak; Kasus 5: ch = '5'; merusak; Kasus 6: ch = '6'; merusak; Kasus 7: ch = '7'; merusak; Kasus 8: ch = '8'; merusak; Kasus 9: ch = '9'; merusak; Kasus 10: ch = 'a'; merusak; Kasus 11: ch = 'b'; merusak; Kasus 12: Ch = 'C'; merusak; Kasus 13: ch = 'd'; merusak; Kasus 14: ch = 'e'; merusak; Kasus 15: ch = 'f'; merusak; Default: ch = ''; } return ch; }} Peralatan:
Impor java.awt.alphacomposite; impor java.awt.color; impor java.awt.font; impor java.awt.graphics2d; java.awt.image; impor java.awt.rendinghints; impor java.awt.geom.affinetransform; java.awt.image.colormodel; impor java.awt.image.writableraster; impor java.io.file; impor java.io.fileinputStream; java.io.filenotfoundexception; impor java.io.fileOutputstream; impor java.io.iooOccepsio; javax.imageio.imageio; import com.sun.image.codec.jpeg.imageformateException; import com.sun.image.codec.jpeg.jpegcodec; impor; etencyc.jpeg.jpegimagecodecoder; impor.sun com.suncodec.jpeg.jpegimagecoderer; impor.sun com.suncrec.jpeg.jpegimageCoder; Kelas Alat Gambar, terutama untuk pemrosesan watermark gambar * * @author 025079 * @Version [Nomor Versi, 2011-11-28] * @See [Kelas Terkait/Metode] * @since [Versi Produk/Modul] */Kelas Publik ImageHelper {// Project Root Path Public Static String Path = System.getProperty ("User.dir"); /** * Hasilkan Thumbnails <br/> * Simpan: Imageo.write (BufferedImage, imgType [jpg/png/...], file); * * @param Sumber * Gambar asli * @param width * lebar thumbnail * Tinggi thumbnail * Tinggi thumbnail * @param b * Apakah itu sama -sama skala * */ public static bufferedimage jempol (Sumber buferedimage, lebar int, tinggi int, boolean b) {// targetw, targeth menunjukkan panjang target dan lebar dengan masing -masing target. Target BufferedImage = NULL; ganda sx = (ganda) lebar / sumber.getWidth (); ganda sy = (ganda) tinggi / sumber.getHeight (); if (b) {if (sx> sy) {sx = sy; width = (int) (sx * source.getWidth ()); } else {sy = sx; tinggi = (int) (sy * source.getHeight ()); }} if (type == bufferedImage.type_custom) {// buatan tangan colormodel cm = source.getColormodel (); Writableraster raster = cm.createCompatibleWritableraster (lebar, tinggi); boolean alphapremultiplied = cm.isalphapremultiplied (); target = BufferedImage baru (cm, raster, alphapremultiplied, null); } else target = BufferedImage baru (lebar, tinggi, jenis); Grafik2d g = target.creategraphics (); // lebih halus dari exlax: g.setrenderinghint (renderinghints.key_rendering, renderinghints.value_render_quality); g.drawrenderedimage (sumber, affinetransform.getscaleInstance (sx, sy)); g.dispose (); target pengembalian; } /** * Image watermark* * @param imgPath * Pending image* @param markPath * Watermark image* @param x * Watermark is in the upper left corner of the image The x coordinate value of the @param y * Watermark is in the upper left corner of the image The y coordinate value of the @param alpha * Watermark transparency 0.1f ~ 1.0f * */ public static void waterMark(String imgPath, String MarkPath, int x, int y, float alpha) {coba {// Muat gambar file gambar yang tertunda img = gambaro.read (file baru (imgpath)); BufferedImage Image = BufferedImage baru (img.getWidth (null), img.getheight (null), bufferedimage.type_int_rgb); Graphics2d G = Image.Creategraphics (); G.DrawImage (IMG, 0, 0, NULL); // Muat gambar gambar gambar watermark src_biao = imageo.read (file baru (markpath)); g.setComposite (alphacomposite.getInstance (alphacomposite.src_atop, alpha)); G.DrawImage (SRC_BIAO, X, Y, NULL); g.dispose (); // simpan file yang diproses fileOutputStream out = FileOutputStream baru (imgpath); Jpegimageencoder encoder = jpegcodec.createJpegencoder (out); encoder.encode (gambar); out.close (); } catch (Exception e) {E.PrintStackTrace (); } } /** * Text watermark* * @param imgPath * Pending image* @param text * Watermark text* @param font * Watermark font information* @param color * Watermark font color* @param x * Watermark x coordinate value at the upper left corner of the picture* @param y * Watermark is in the y coordinate value at 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, warna warna, int x, int y, float alpha) {coba {font dfont = (font == null)? font baru ("宋体", 20, 13): font; Image img = ImageIo.read (file baru (imgpath)); BufferedImage Image = BufferedImage baru (img.getWidth (null), img.getheight (null), bufferedimage.type_int_rgb); Graphics2d G = Image.Creategraphics (); G.DrawImage (IMG, 0, 0, NULL); g.setColor (warna); g.setfont (dfont); g.setComposite (alphacomposite.getInstance (alphacomposite.src_atop, alpha)); g.drawstring (teks, x, y); g.dispose (); FileOutputStream out = FileOutputStream baru (imgpath); Jpegimageencoder encoder = jpegcodec.createJpegencoder (out); encoder.encode (gambar); out.close (); } catch (Exception e) {System.out.println (e); }} / *** Baca gambar JPEG* @param fileName nama file* @return BufferedImage Object* / public static BufferedImage readjpegimage (string fileName) {coba {inputStream imagein = new FileInputStream (file baru (fileName)); // Dapatkan input encoder dan encode stream file dalam format jpg jpegimagedecoder decoder = jpegcodec.createJpegdecoder (gambarin); // Dapatkan objek gambar yang dikodekan BufferedImage SourceImage = decoder.decodeasbufferedImage (); return SourceImage; } catch (FileNotFoundException e) {e.printstacktrace (); } catch (ImageFormatException e) {e.printstacktrace (); } catch (ioException e) {e.printstacktrace (); } return null; } / *** Baca gambar JPEG* @param FileName nama file* @return BufferedImage Object* / Public Static BufferedImage ReadPnGImage (String FileName) {coba {file inputFile = File baru (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; } / *** Perhitungan nilai grayscale* @param piksel piksel* @return int nilai grayscale* / public static int rgbtogray (int piksel) {// int _alpha = (piksel >> 24) & 0xff; int _red = (piksel >> 16) & 0xff; int _green = (piksel >> 8) & 0xff; int _blue = (piksel) & 0xff; return (int) (0,3 * _red + 0,59 * _green + 0,11 * _blue); } / *** Hitung rata -rata array* @param piksel array* @return int rata -rata* / rata -rata int statis public (int [] piksel) {float m = 0; untuk (int i = 0; i <pixels.length; ++ i) {m += piksel [i]; } m = m / piksel.length; return (int) m; }}Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.