Java后台实现 识别图片文字信息、身份证的文字信息

1.百度AI识别API
http://ai.baidu.com/docs#/OCR-API/top
2.登录创建应用
在这里插入图片描述
创建应用,获取
AppID
API Key
Secret Key

在这里插入图片描述
在这里插入图片描述
3.Java项目开发
pom.xml 加入sdk配置

<!-- 百度文字识别 -->
		<dependency>
			<groupId>com.baidu.aip</groupId>
			<artifactId>java-sdk</artifactId>
			<version>4.10.0</version>
		</dependency>

jsp原生ssm框架请下载jar包

4.新建ApiOrcUtil.java 文字识别工具类
将下文中的APP_ID、API_KEY、SECRET_KEY 替换成你申请的应用

import com.baidu.aip.ocr.AipOcr;
import com.bdxh.framework.commons.util.ConfigMgr;
import org.json.JSONObject;

import java.util.HashMap;

public class ApiOrcUtil {

    //设置APPID/AK/SK
    private static String APP_ID = "APP_ID";
    private static String API_KEY ="API_KEY";
    private static String SECRET_KEY = "SECRET_KEY";

    public static String getPictureString(String photoPath){

        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        // 是否检测朝向
        options.put("detect_direction", "false");
        // 是否检测风险
        options.put("detect_risk", "false");

        // 正反面front /back
        String idCardSide = "front";

        // 参数为本地图片二进制数组
        /*byte[] file = new byte[0];
        try {
            file = Util.readFileByBytes(photoPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject res = client.idcard(file, idCardSide, options);
        System.out.println(res.toString(2));*/


        // 参数为本地图片路径
        try {
            JSONObject res = client.idcard(photoPath, idCardSide, options);
            System.out.println(res.toString(2));
            if (res != null) {
                JSONObject idCard = new JSONObject();
                JSONObject words_result = res.getJSONObject("words_result");

                idCard.put("name", words_result.getJSONObject("姓名").get("words"));
                idCard.put("nation", words_result.getJSONObject("民族").get("words"));
                idCard.put("address", words_result.getJSONObject("住址").get("words"));
                idCard.put("sex", words_result.getJSONObject("性别").get("words"));
                idCard.put("birth", words_result.getJSONObject("出生").get("words"));
                idCard.put("number", words_result.getJSONObject("公民身份号码").get("words"));
                return idCard.toString(2);
            } else {
                return "";
            }
        }catch (JSONException e){
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println("正面" + getPictureString("F:\\zsl\\zsl\\sfz/bbb.jpg"));
    }
}

5.打印结果
在这里插入图片描述
在这里插入图片描述

发布了62 篇原创文章 · 获赞 21 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40618664/article/details/100986890
今日推荐