この記事の例は、参照のためのJavaフィンガープリント認識と画像認識のソースコードを共有しています。特定のコンテンツは次のとおりです
メインカテゴリ:
java.awt.image.bufferedimage; import java.util.arraylist; import java.util.list; public class signimagesearch { / ** * @param args * / public static void main(string [] args){list <string> hashcodes = new Arraylist <string>();文字列filename = imagehelper.path + "// images //";文字列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 dixter = hammingdistance(sourcehashcode、hashcodes.get(i)); System.out.print( "ハミング距離:"+差+""); if(difference == 0){system.out.println( "source.jpg画像は、例"+(i+1)+"。Jpg"); } else if(difference <= 5){system.out.println( "source.jpg画像は、例"+(i+1)+"。JPG"); } else if(difference <= 10){system.out.println( "source.jpgイメージは、例"+(i+1)+"。jpg"); } else if(difference> 10){system.out.println( "source.jpgイメージは、例"+(i+1)+"。jpg"); }}} /***「ハミング距離」を計算します。 *異なるデータビットが5を超えない場合、2つの写真が非常に似ていることを意味します。それらが10を超える場合、これらは2つの異なる写真であることを意味します。 * @param sourcehashcode source hashcode * @param hashcode比較ハッシュコード */ public static int hammingdistance(string sourcehashcode、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差; }/ ***画像指紋を生成* @param filename filename* @return image Fingerprint*/ public static string producefingerprint(string filename){bufferedimage source = imagehelper.readpngimage(filename); //ファイルの読み取りファイルint width = 8; int height = 8; //最初のステップは、サイズを減らすことです。 //画像を8x8サイズに縮小し、合計64ピクセルで縮小します。このステップの目的は、画像の詳細を削除し、構造、明るい、暗いなどの基本情報のみを保持し、さまざまなサイズとプロポーションによって引き起こされる画像の違いを放棄することです。 bufferedimage thumb = imagehelper.thumb(source、width、height、false); // 2番目のステップは、色を簡素化することです。 //縮小画像をレベル64グレースケールに変えます。つまり、すべてのピクセルに64色しかありません。 int [] pixels = new int [width * height]; for(int i = 0; i <width; i ++){for(int j = 0; j <height; j ++){pixels [i * height+j] = imagehelper.rgbtogray(thumb.getrgb(i、j)); }} // 3番目のステップは、平均を計算することです。 // 64ピクセルすべてのグレースケール平均を計算します。 int avgpixel = imagehelper.average(pixels); //ステップ4、ピクセルのグレースケールを比較します。 //各ピクセルのグレースケールを平均値と比較します。平均値以上の場合、1として示されます。平均値よりも少ない場合、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; }} //ステップ5、ハッシュ値を計算します。 //前のステップの比較結果を組み合わせて、この写真の指紋である64ビット整数を形成します。組み合わせの順序は重要ではありません。すべての写真が同じ順序であることを確認してください。 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)math.pow(2、2) + comps [i + 2] *(int)math.pow(2、1) + comps [i + 2]; hashcode.append(binarytohex(result)); } //指紋を取得した後、さまざまな写真を比較して、64ビットでいくつのビットが異なるかを確認できます。 hashcode.toString()を返します。 } / ** *バイナリをhexに変換 * @param int binary * @return char hex * / private static char binarytohex(int binary){char ch = ''; switch(binary){case 0:ch = '0';壊す;ケース1:ch = '1';壊す;ケース2:ch = '2';壊す;ケース3:Ch = '3';壊す;ケース4:ch = '4';壊す;ケース5:Ch = '5';壊す;ケース6:ch = '6';壊す;ケース7:ch = '7';壊す;ケース8:ch = '8';壊す;ケース9:Ch = '9';壊す;ケース10:ch = 'a';壊す;ケース11:ch = 'b';壊す;ケース12:ch = 'c';壊す;ケース13:ch = 'd';壊す;ケース14:ch = 'e';壊す;ケース15:ch = 'f';壊す;デフォルト:ch = ''; } chを返します。 }}ツール:
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; aff.awt.awt.awt.awt.awt.awt.awt.awma. java.awt.image.colormodel; Import java.awt.image.writableraster; Import java.io.file; Import java.io.fileinputStream; Import java.io.fileenotfoundexception; Import java.io.fileOutputStream; Import java.io.io.ioexception; Import java.ioeexception; javax.imageio.imageio; import com.sun.image.codec.jpeg.imageformatexception; Import com.sun.image.codec.jpeg.jpegcodec; Import com.sun.image.codec.jpeg.jpegimageCoder; Import.sun.sun.sun.image.codec.jpeg.jpegmecterクラス、主に画像の透かし処理 * * @author 025079 * @version [バージョン番号、2011-11-28] * @see [関連クラス/メソッド] * @since [製品/モジュールバージョン] */public class imagehelper {//プロジェクトルートディレクトリパブリック最終文字列パス= System.getProperty( "user.dir"); /** *サムネイルを生成<br/> * save:imageio.write(bufferedimage、imgtype [jpg/png/...]、file); * * @param source *オリジナル画像 * @param幅 *サムネイル幅 *サムネイル高さ *サムネイルの高さ * @param b * @param b */ public static bufferedimageサム(bufferedimage source、int width、int height、boolean b){//ターゲット、ターゲットはそれぞれターゲットの長さと幅を指します。 bufferedimageターゲット= null; double sx =(double)width / source.getWidth(); double sy =(double)height / source.getheigh(); if(b){if(sx> sy){sx = sy; width =(int)(sx * source.getWidth()); } else {sy = sx; height =(int)(sy * source.getheigh()); }} if(type == bufferedimage.type_custom){//ハンドメイドColormodel cm = source.getColormodel(); writableraster raster = cm.createcopatiblewritableraster(幅、高さ); boolean alphapremultipired = cm.isalphapremultipired();ターゲット= new BuffereDimage(CM、Raster、Alphapremultipired、Null); } else Target = new BufferedImage(幅、高さ、タイプ); graphics2d g = target.creategraphics(); // exlaxよりスムーズ:G.SetRenderingHint(renderingHints.key_rendering、renderinghints.value_render_quality); G.DrawrenderEdimage(source、affintransform.getScaleinStance(SX、SY)); g.dispose();ターゲットを返します。 } / ** *画像透かし * * @param imgpath *保留イメージ * @param Markpath *透かし * @param x *透かしは画像の左上隅にあります@param y *ウォーターマークのX座標値は画像の左上隅にあります。 x、int y、float alpha){try {//保留中の画像ファイル画像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); //ウォーターマークの画像ファイルのロード画像src_biao = imageio.read(new file(markpath)); g.setComposite(alphacomposite.getInstance(alphacomposite.src_atop、alpha)); G.drawimage(src_biao、x、y、null); g.dispose(); //処理されたファイルを保存fileoutputStream out = new fileoutputStream(imgpath); jpegimageencoder encoder = jpegcodec.createjpegencoder(out); encoder.encode(画像); out.close(); } catch(Exception e){e.printstacktrace(); }} /** *テキスト透かし * * @param imgpath *保留画像 * @paramテキスト *透かしテキスト * @param font * watermark font情報 * @param color * @param x * watermark x座標値の写真の左上隅 * @param y *ウォーターマークは、左上cordinate valuctにあります。 */ public static void textmark(string imgpath、string text、font font、color color、int x、float alpha){try {font dfont =(font == null)?新しいフォント( "宋体"、20、13):フォント; 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(色); 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(画像); out.close(); } catch(Exception e){System.out.println(e); }} / *** jpeg image* @param filename filename* @return bufferedimageイメージオブジェクト* / public static bufferedimage readjpegimage(string filename){try {inputstream imagein = new fileinputStream(filename(filename)); //入力エンコーダーを取得し、jpg形式でファイルストリームをエンコードしますjpegimagedecoder decoder = jpegcodec.createjpegdecoder(imagein); //エンコードされた画像オブジェクトbufferedimage sourceimage = decoder.decodeasbufferedimage()を取得します。 SourceImageを返します。 } catch(filenotfoundexception e){e.printstacktrace(); } catch(imageformatexception e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); } nullを返します。 } / *** jpeg picture* @param filename filename* @return bufferedimage画像オブジェクト* / public static bufferedimage readpngimage(string filename){try {file inputfile = new file(filename); bufferedimage sourceimage = imageio.read(inputFile); SourceImageを返します。 } catch(filenotfoundexception e){e.printstacktrace(); } catch(imageformatexception e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); } nullを返します。 } / ***グレイスケール値計算* @param pixels pixels* @return int grayscale value* / public static int rgbtogray(int pixels){// int _alpha =(pixels >> 24)&0xff; int _red =(ピクセル>> 16)&0xff; int _green =(pixels >> 8)&0xff; int _blue =(ピクセル)&0xff; return(int)(0.3 * _red + 0.59 * _green + 0.11 * _blue); } / ***配列の平均を計算* @param pixels array* @return int平均* / public static int平均(int [] pixels){float m = 0; for(int i = 0; i <pixels.length; ++ i){m += pixels [i]; } m = m / pixels.length; return(int)m; }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。