/// <summary> /// 感知哈希算法 /// </summary> /// <param name="bm">分割後的小圖</param> /// <returns>字元</returns> public static char OcrHash(Bitmap bm) { Dictionary <string, Bitmap> samples = LoadSamples(); double diff = .0; string mayBe = null; foreach (string key in samples.Keys) { double diffRate = SampleOcr.HashComp(samples[key], bm); if (diffRate == 1) { LastSimilar = 1; return(key[0]); } if (diffRate > diff) { mayBe = key; diff = diffRate; } } if (mayBe == null) { throw new ApplicationException(); } LastSimilar = (float)diff; return(mayBe[0]); }
private void SplitCaptcha(Captcha obj, int want_char = 0, int mode = 0) { // Step2 其他處理 /*if (comboBox1.SelectedIndex == 1) * obj.RemoteNoiseLineByPixels(); * else if (comboBox1.SelectedIndex == 2) * { * obj.ClearPictureBorder(2); * obj.RemoteNoisePointByPixels(); * }*/ // Step2 改變圖片範圍 /*obj.ConvertBmpValidRange(4, 128); * pic_Captcha_Cut.Image = obj.BmpSource;*/ // Step3 切割圖片範圍 Bitmap[] bitmap = null; if (mode == 0) { bitmap = SampleOcr.SplitBitmaps(obj.BmpSource); if (bitmap.Length != want_char) { bitmap = null; bitmap = obj.GetSplitPicChars(want_char, 1); } } else if (mode == 1) { bitmap = SampleOcr.SplitBitmaps(obj.BmpSource); } else if (mode == 2) { bitmap = obj.GetSplitPicChars(want_char, 1); } CaptchaAlgorithm(bitmap, cb_algorithm.SelectedIndex); }
private void CaptchaAlgorithm(Bitmap[] bitmap, int mode = 0) { try { for (int i = 0; i < bitmap.Length; i++) { switch (i) { case 0: pic_p1.Image = SampleOcr.Trim(bitmap[0]); switch (mode) { case 0: txt_p1.Text = SampleOcr.OcrCharEx(SampleOcr.Trim(bitmap[i])).ToString(); break; case 1: txt_p1.Text = SampleOcr.OcrHash(SampleOcr.Trim(bitmap[i])).ToString(); break; case 2: txt_p1.Text = SampleOcr.OcrChar(SampleOcr.Trim(bitmap[i])).ToString(); break; } txt_s1.Text = txt_p1.Text; txt_n1.Text = (SampleOcr.LastSimilar * 100).ToString() + "%"; break; case 1: pic_p2.Image = SampleOcr.Trim(bitmap[1]); switch (mode) { case 0: txt_p2.Text = SampleOcr.OcrCharEx(SampleOcr.Trim(bitmap[i])).ToString(); break; case 1: txt_p2.Text = SampleOcr.OcrHash(SampleOcr.Trim(bitmap[i])).ToString(); break; case 2: txt_p2.Text = SampleOcr.OcrChar(SampleOcr.Trim(bitmap[i])).ToString(); break; } txt_s2.Text = txt_p2.Text; txt_n2.Text = (SampleOcr.LastSimilar * 100).ToString() + "%"; break; case 2: pic_p3.Image = SampleOcr.Trim(bitmap[2]); switch (mode) { case 0: txt_p3.Text = SampleOcr.OcrCharEx(SampleOcr.Trim(bitmap[i])).ToString(); break; case 1: txt_p3.Text = SampleOcr.OcrHash(SampleOcr.Trim(bitmap[i])).ToString(); break; case 2: txt_p3.Text = SampleOcr.OcrChar(SampleOcr.Trim(bitmap[i])).ToString(); break; } txt_s3.Text = txt_p3.Text; txt_n3.Text = (SampleOcr.LastSimilar * 100).ToString() + "%"; break; case 3: pic_p4.Image = SampleOcr.Trim(bitmap[3]); switch (mode) { case 0: txt_p4.Text = SampleOcr.OcrCharEx(SampleOcr.Trim(bitmap[i])).ToString(); break; case 1: txt_p4.Text = SampleOcr.OcrHash(SampleOcr.Trim(bitmap[i])).ToString(); break; case 2: txt_p4.Text = SampleOcr.OcrChar(SampleOcr.Trim(bitmap[i])).ToString(); break; } txt_s4.Text = txt_p4.Text; txt_n4.Text = (SampleOcr.LastSimilar * 100).ToString() + "%"; break; case 4: pic_p5.Image = SampleOcr.Trim(bitmap[4]); switch (mode) { case 0: txt_p5.Text = SampleOcr.OcrCharEx(SampleOcr.Trim(bitmap[i])).ToString(); break; case 1: txt_p5.Text = SampleOcr.OcrHash(SampleOcr.Trim(bitmap[i])).ToString(); break; case 2: txt_p5.Text = SampleOcr.OcrChar(SampleOcr.Trim(bitmap[i])).ToString(); break; } txt_s5.Text = txt_p5.Text; txt_n5.Text = (SampleOcr.LastSimilar * 100).ToString() + "%"; break; } } } catch { } }