(ImageMagick + tesseract - ocr) 를 사용 하여 이미지 인증 코드 인식 인 스 턴 스 를 실현 합 니 다.
인증 코드 식별 율 은 다음 그림 과 같다. (정확 도 는 괜 찮 죠?)
좋 습 니 다. 본론 으로 들 어가 서 빨리 소스 코드 를 올 리 세 요 ~ ~, 부족 한 점 많이 양해 해 주세요.
메모: 인증 코드 인식 류 를 사용 하기 전에 첫째, ImageMagick 을 설치 하 십시오. 둘째, tesseract - 2.04. exe. tar. gz 와 tesseract - 2.00. eng. tar. gz 를 다운로드 하고 내용 을 tesseract 디 렉 터 리 에 압축 을 풀 고 tesseract 디 렉 터 리 를 응용 프로그램 아래 (debug 디 렉 터 리 아래) 로 복사 해 야 합 니 다.
1. 이미지 인식, 식별 유형 검증
/// <summary>
/// ,
/// </summary>
public class Identify
{
/// <summary>
/// : 、 ,
/// </summary>
/// <param name="img_path"> </param>
/// <param name="save_dir"> </param>
/// <param name="scale"> ( :100~1000)-( : )</param>
/// <returns></returns>
public static string StartIdentifyingCaptcha(string img_path, string save_dir,int scale)
{
return StartIdentifyingCaptcha(img_path, save_dir, 4, 0, scale);
}
/// <summary>
/// : 、 ,
/// </summary>
/// <param name="img_path"> </param>
/// <param name="save_dir"> </param>
/// <param name="content_type"> (0: 1: 2: 3: ,4、 )-( : )</param>
/// <param name="content_length"> ( : )</param>
/// <param name="scale"> ( :100~1000)-( : )</param>
/// <returns></returns>
public static string StartIdentifyingCaptcha(string img_path, string save_dir, byte content_type, int content_length, int scale)
{
string captcha = ""; //
//int captcha_length = 6; // ( : )
//int scale = 730; // ( : )
do
{
// ( 、 ImageMagick, 、 tesseract )
Common.StartProcess("cmd.exe", new string[] { "cd tesseract" ,
//
String.Format(@"convert.exe -compress none -depth 8 -alpha off -scale {0}% -colorspace gray {1} {2}\captcha.tif",scale,img_path,save_dir),
//
String.Format(@"tesseract.exe {0}\captcha.tif {0}\result",save_dir),
"exit"});
//
StreamReader reader = new StreamReader(String.Format(@"{0}\result.txt", save_dir));
captcha = reader.ReadLine().Trim();
reader.Close();
//
string pattern = "";
//
if (content_type == 0)
{
pattern = (content_length > 0) ? "^[0-9]{" + content_length + "}$" : "^[0-9]+$";
}
//
else if (content_type == 1)
{
pattern = (content_length > 0) ? "^[a-zA-Z]{" + content_length + "}$" : "^[a-zA-Z]+$";
}
//
else if (content_type == 2)
{
pattern = (content_length > 0) ? "^[a-zA-Z0-9]{" + content_length + "}$" : "^[a-zA-Z0-9]+$";
}
//
else if (content_type == 3)
{
pattern = (content_length > 0) ? "^[\u4e00-\u9fa5]{" + content_length + "}$" : "^[\u4e00-\u9fa5]+$";
}
//
else
{
pattern = (content_length > 0) ? "^[A-Za-z0-9\u4e00-\u9fa5]{" + content_length + "}$" : "^[A-Za-z0-9\u4e00-\u9fa5]+$";
}
// ,
if (pattern != "" && !Regex.IsMatch(captcha, pattern))
{
captcha = "";
scale++;
}
// 9 , , ,
if (scale > 900)
{
captcha = "";
break;
}
} while (captcha == "");
return captcha;
}
}
2. C\# 에서 명령 행 명령 을 수행 하 는 함수
public class Common
{
#region : C#
/// <summary>
/// : C#
/// </summary>
/// <param name="startFileName"> </param>
/// <param name="commands"> </param>
/// <returns></returns>
public static string StartProcess(string startFileName, string[] commands)
{
// Process ,
Process p = new Process();
//Process StartInfo , ProcessStartInfo , ,
// :
//
p.StartInfo.FileName = startFileName;
// Shell
p.StartInfo.UseShellExecute = false;
//
p.StartInfo.RedirectStandardInput = true;
//
p.StartInfo.RedirectStandardOutput = true;
//
p.StartInfo.RedirectStandardError = true;
//
p.StartInfo.CreateNoWindow = true;
// 。
//
p.Start();
//
foreach (string command in commands)
{
p.StandardInput.WriteLine(command);
}
//
return p.StandardOutput.ReadToEnd();
}
#endregion
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Access Request, Session and Application in Struts2If we want to use request, Session and application in JSP, what should we do? We can obtain Map type objects such as Req...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.