Javaは認証コードの識別を実現します。
9519 ワード
Javaでは認証コードが簡単で、使うソフトウェアはtesseractocrです。このソフトウェアは現地にインストールする必要があります。 。
githubダウンロードアドレス https://github.com/tesseract-ocr/tessdata
ブロガーは公式サイトでダウンロードしました。
このソフトウェアのデフォルトの認識は英語です。中国語を識別するためには、中国語のトレーニングテキストをチワンとする必要があります。sim.trineddataはC:\Program Files(x 86)\Tesseract-OCR\tessdataに保存されます。
簡単な検証コードの識別は直接呼び出します。 TesseractのdoOCR方法でいいです。認証コードのノイズが多くて、妨害線がある場合は、画像を処理してください。
画像処理の大まかな考え方:階調をして二値化してから干渉線を除去する。
コードを多く言わない。
コードを実現
githubダウンロードアドレス https://github.com/tesseract-ocr/tessdata
ブロガーは公式サイトでダウンロードしました。
このソフトウェアのデフォルトの認識は英語です。中国語を識別するためには、中国語のトレーニングテキストをチワンとする必要があります。sim.trineddataはC:\Program Files(x 86)\Tesseract-OCR\tessdataに保存されます。
簡単な検証コードの識別は直接呼び出します。 TesseractのdoOCR方法でいいです。認証コードのノイズが多くて、妨害線がある場合は、画像を処理してください。
画像処理の大まかな考え方:階調をして二値化してから干渉線を除去する。
コードを多く言わない。
コードを実現
public static void main(String[] args) {
String url = " ";
//
String path= "C:\\Users\\Administrator\\Desktop\\1.jpg";
//
downloadPicture(url,path);
Demo demo= new Demo();
String code= demo.FindOCR(path,false);
System.out.println(code);
}
検証コードをダウンロードするのは簡単です。HTTPClientで検証画像のリンクを取得してダウンロードすればいいです。私はここにダウンロードコードを一つだけ入れます。接続されているサイトの取得要求も違っています。 private static void downloadPicture(String urlList,String path) {
URL url = null;
try {
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
認証コードを識別するツールクラスpublic String FindVCode(String srcImg, boolean language) {
try {
File imgFile = new File(srcImg);
if (!srcImg.exists()) {
return " ";
}
BufferedImage testImage = ImageIO.read(imgFile);
Tesseract tesseract= new Tesseract();
//
instance.setDatapath("/usr/local/share/tessdata/");
if (language) {
tesseract.setLanguage("chi_sim");
}
String vCode= null;
// vCode =instance.doOCR(testImage) ;
BufferedImage cleanedImg = cleanLinesInImage(testImage);
vCode= tesseract.doOCR(cleanedBufferedImage);
return vCode;
} catch (Exception e) {
e.printStackTrace();
return " ";
}
}
画像処理プロセスprivate BufferedImage cleanLinesInImage(BufferedImage image) throws IOException{
BufferedImage bufferedImage = oriBufferedImage;
int h = bufferedImage.getHeight();
int w = bufferedImage.getWidth();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
boolean c = true;
// , ,
int roundWhiteCount = 0;
if (isBlackColor(bufferedImage , x + 1, y + 1)){
roundWhiteCount++;
}
if (isBlackColor(bufferedImage , x + 1, y - 1)){
roundWhiteCount++;
}
if (isBlackColor(bufferedImage , x - 1, y + 1)){
roundWhiteCount++;
}
if (isBlackColor(bufferedImage , x - 1, y - 1)){
roundWhiteCount++;
}
if (roundWhiteCount >= 4) {
c = false;
}
if (!isBlackColor(bufferedImage , x, y) && c) {
image.setRGB(x, y, 0xFFFFFFFF); //argb:AARRGGBB
}
}
}
// ,
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
//
if ((bufferedImage .getRGB(x, y) & 0xFFFFFF) != (new Color(255, 255, 255).getRGB() & 0xFFFFFF)) {
bufferedImage .setRGB(x, y, 0xFF000000);
}
}
}
//
int threshold = ostu(gray, w, h);
BufferedImage binaryBufferedImage= new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
if (gray[x][y] > threshold)
{
gray[x][y] |= 0x00FFFF;
} else
{
gray[x][y] &= 0xFF0000;
}
binaryBufferedImage.setRGB(x, y, gray[x][y]);
}
}
cleanImage(binaryBufferedImage,h,w );
return binaryBufferedImage;
}
private boolean isBlackColor(BufferedImage image, int x, int y) {
//
if (x < 0 || y < 0 || x >= image.getWidth() || y >= image.getHeight()) {
return false;
}
int pixel = image.getRGB(x, y);
return
// R
(pixel & 0xFF0000) >> 16 < 30
// G
&& (pixel & 0xFF00) >> 8 < 30
// B
&& (pixel & 0xFF) < 30;
}
public void cleanImage(BufferedImage binaryBufferedImage,int h ,int w ){
//
for(int y = 1; y < h-1; y++){
for(int x = 1; x < w-1; x++){
boolean flag = false ;
if(isBlack(binaryBufferedImage.getRGB(x, y))){
// ,
if(isWhite(binaryBufferedImage.getRGB(x-1, y)) && isWhite(binaryBufferedImage.getRGB(x+1, y))){
flag = true;
}
// ,
if(isWhite(binaryBufferedImage.getRGB(x, y+1)) && isWhite(binaryBufferedImage.getRGB(x, y-1))){
flag = true;
}
// ,
if(isWhite(binaryBufferedImage.getRGB(x-1, y+1)) && isWhite(binaryBufferedImage.getRGB(x+1, y-1))){
flag = true;
}
if(isWhite(binaryBufferedImage.getRGB(x+1, y+1)) && isWhite(binaryBufferedImage.getRGB(x-1, y-1))){
flag = true;
}
if(flag){
binaryBufferedImage.setRGB(x,y,-1);
}
}
}
}
}
public Mat bufferedImageToMat(BufferedImage bi) {
Mat mat = new Mat(bi.getHeight(), bi.getWidth(), CvType.CV_8UC1);
byte[] white = new byte[] { (byte) 255 };
byte[] black = new byte[] { (byte) 0 };
for (int x=0; x 300)
{
return true;
}
return false;
}
public int isBlackOrWhite(int colorInt)
{
if (getColorBright(colorInt) < 30 || getColorBright(colorInt) > 730)
{
return 1;
}
return 0;
}
public int getColorBright(int colorInt)
{
Color color = new Color(colorInt);
return color.getRed() + color.getGreen() + color.getBlue();
}
public int ostu(int[][] gray, int w, int h)
{
int[] histData = new int[w * h];
// Calculate histogram
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
int red = 0xFF & gray[x][y];
histData[red]++;
}
}
// Total number of pixels
int total = w * h;
float sum = 0;
for (int t = 0; t < 256; t++)
sum += t * histData[t];
float sumB = 0;
int wB = 0;
int wF = 0;
float varMax = 0;
int threshold = 0;
for (int t = 0; t < 256; t++)
{
wB += histData[t]; // Weight Background
if (wB == 0)
continue;
wF = total - wB; // Weight Foreground
if (wF == 0)
break;
sumB += (float) (t * histData[t]);
float mB = sumB / wB; // Mean Background
float mF = (sum - sumB) / wF; // Mean Foreground
// Calculate Between Class Variance
float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);
// Check if new maximum found
if (varBetween > varMax)
{
varMax = varBetween;
threshold = t;
}
}
return threshold;
}
その中の部分はインターネットのソースコードを参考にしています。もし作成が間違っていたり、修正できるより良い提案があれば指摘してください。