題記:本身是作java web的,可是本人之前接觸並學習不少圖像的知識,因此對圖像很敏感。下面以百度的一個接口,實現身份證識別案例java
一、須要百度開發者AppID、SecretKey 、API Key。web
先註冊,而後進入https://console.bce.baidu.com/ai/?fromai=1#/ai/ocr/overview/index,這個網址,選擇文字識別,建立應用,獲取AppID、SecretKey 、API Key。apache
二、建立一個maven項目,pom.xml以下:json
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.fx</groupId> <artifactId>demo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>demo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- 百度文字識別SDK --> <dependency> <groupId>com.baidu.aip</groupId> <artifactId>java-sdk</artifactId> <version>4.6.1</version> </dependency> </dependencies> <build> <finalName>demo</finalName> </build> </project>
三、建立BaiduConfig.java、demo.java、Utils.javaapi
package com.fx.config; public class BaiduConfig { public static final String APP_ID = "***";//本身的 public static final String API_KEY = "****";//本身的 public static final String SECRET_KEY = "***";//本身的 }
package com.fx.demo; import java.util.HashMap; import org.json.JSONObject; import com.baidu.aip.ocr.AipOcr; import com.fx.config.BaiduConfig; import com.fx.utils.Utils; public class demo { //設置APPID/AK/SK public static void main(String[] args) { IDCardRecogize(); } public static void IDCardRecogize() { AipOcr client = new AipOcr(BaiduConfig.APP_ID, BaiduConfig.API_KEY, BaiduConfig.SECRET_KEY); // 傳入可選參數調用接口 HashMap<String, String> options = new HashMap<String, String>(); options.put("detect_direction", "true"); options.put("detect_risk", "false"); //背面照 //String idCardSide = "back"; //前面照 String idCardSide = "front"; // 參數爲本地圖片路徑 //String image = "D:\\back.jpg"; //String image = "D:\\front.jpg"; String image = "F:\\picture\\shenfenzheng.jpg";//身份證照片路徑 JSONObject res = client.idcard(image, idCardSide, options); System.out.println(res.toString(2)); // 參數爲本地圖片二進制數組 byte[] file = Utils.readImageFile(image); res = client.idcard(file, idCardSide, options); System.out.println(res.toString(2)); } }
package com.fx.utils; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import javax.imageio.stream.FileImageInputStream; public class Utils { /** 將圖像轉爲二進制數組 * @param path * @return */ public static byte[] readImageFile(String path){ byte[] data = null; FileImageInputStream input = null; try { input = new FileImageInputStream(new File(path)); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int numBytesRead = 0; while ((numBytesRead = input.read(buf)) != -1) { output.write(buf, 0, numBytesRead); } data = output.toByteArray(); output.close(); input.close(); } catch (FileNotFoundException ex1) { ex1.printStackTrace(); } catch (IOException ex1) { ex1.printStackTrace(); } return data; } }
這樣就能夠了,直接運行吧!不懂的或者報錯的,能夠留言........。數組