---
title: "识别图片数字-验证码"
author: "philip01"
date: "2011-04-03T00:09:39Z"
tags: []
categories: ["企业架构"]
slug: "untitled"
draft: false
lastmod: "2011-04-03T01:00:28Z"
id: 985530
url: "https://philip01.iteye.com/blog/985530"
---

![](http://dl.iteye.com/upload/attachment/453633/aac4a113-f938-34bb-80d3-e8692f49adc4.jpg)  
   
 

![](http://dl.iteye.com/upload/attachment/453631/0d8c2b36-f6d6-3847-a06a-d0b36ff523e9.jpg)  
   
   
   
 

 

 

   
   
 

 java 通过 JNI 调用开源的OCR 来识别验证码， 这样不要通过调用exe 生成临时文件来获得验证码

 

 

 //演示程序

 protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
   throws ServletException, IOException {  
    
  String url = req.getParameter("j\_imgurl");  
  InputStream instream = InitEnv.class.getResourceAsStream(InitEnv.CERTPATH);  
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();  
  String protocol = "http";  
  byte\[] b = null;  
  try {  
   if(url.startsWith("https")){  
       protocol = "https";  
   }  
   PicUtil.getPic(protocol,url,InitEnv.PORT, InitEnv.PWD, instream, outputStream);  
   b = outputStream.toByteArray();  
   ImageFilter imageFilter = new ImageFilter(new ByteArrayInputStream(b));  
   outputStream.close();  
   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();   
   ImageIOHelper.createImage(imageFilter.changeGrey(), byteArrayOutputStream);  
   byte\[] imgData = byteArrayOutputStream.toByteArray();  
   byteArrayOutputStream.close();  
     
   if(imgData == null || imgData.length &lt;=0){  
        return ;  
   }

   TesseractEntity tesseractEntity =new TesseractEntity();  
   tesseractEntity.setImgByte(imgData);  
   tesseractEntity.setIdentifyingCode("-1");  
   tesseractEntity.setLength(imgData.length);  
   tesseractEntity.setPath(InitEnv.unZipOcrPath);  
   tesseractEntity.setLang\_option("eng");  
   

   //调用JNI 获得图片文字  
   String result = AnalysePictureEx.analysePicEx(tesseractEntity);  
   String uuid = UUID.randomUUID().toString();  
   ImageData.imags.put(uuid, b );  
   

   //获得验证码  
   req.setAttribute("identifyingCode", tesseractEntity.getIdentifyingCode());  
   req.setAttribute("uuid", uuid);  
         RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/result.jsp");  
         requestDispatcher.forward(req, resp);  
     
  } catch (Exception e) {  
     e.printStackTrace();  
  }  
 }    
    
       
    
 

1、 [iOCR.war](http://dl.iteye.com/topics/download/7303b4df-1549-3adc-9111-fed81d1aa902)放到 tomcat 下。

 

2、访问[http://127.0.0.1:8080/iOCR/](http://127.0.0.1:8080/iOCR/)

 

3、输入图片的URL地址。

 

4、得到图片数字

 

注： 在XP下可以通过