欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ocr学习第二篇:java通过实现百度AI提供的接口实现图片文字识别

程序员文章站 2024-01-09 23:38:52
...

简单聊一下:第一次接触百度AI提供的这个平台,在里面简单看了一遍,很喜欢这个平台,提供了许多实用的功能,我们在此用到的就是其中的文字识别这个功能,他使用方式很简单,基本http调用就可实现,

基于文字识别提供了两种调用方式:

1.APi方式调用,这种调用方式类似于我们平常去请求其他接口,通过http发一些简单的验证,传入必要的条件,就ok了。

2.sdk方式调用,这种方式调用加上它提供的示例及文档说明,使用起来更加简单。

参考资料:https://blog.csdn.net/weixin_40165004/article/details/82632229

简单介绍下使用过程,参考资料这个博客里面写的已经很清楚了,此处只是按照AI说明文档进行了简单的实现

a. 首先得先注册一个百度账号,进行登录

b,按照API接入指南进行一步步操作,文档很详细,也简单。

注:sdk方式使用只需要在平台下载相应的sdk包,解压缩后把jar文件拷贝到项目中,就可以按照规范进行开发使用了

ocr学习第二篇:java通过实现百度AI提供的接口实现图片文字识别

 附上此处我做的一个实现代码(api的方式只实现了获取access_token,对图片进行base64处理和进行urlencode处理进行接口访问此处未实现),此处用sdk的方式更简单。

代码结构:

ocr学习第二篇:java通过实现百度AI提供的接口实现图片文字识别

 getToken.java

package com.baidu.api.ocr;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpConnectionParams;

public class GetToken {

	public static void main(String[] args) {
		String aKey = "jNEhoBwPhbUOsVxnQBrcnBZB";
		String skey = "5tXebMWoEdGDnjGGkL3BEDrhmwaNqnRr";
		String url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="+aKey+"&client_secret="+skey;
		HttpClient httpClient = null;
		GetMethod getMethod = null;

		try{
			httpClient = new HttpClient();
			httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 60 * 1000);
			httpClient.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, 60 * 1000);
			
			getMethod = new GetMethod(url);
			httpClient.executeMethod(getMethod);

			if (getMethod != null && getMethod.getStatusCode() == 200) {
				String result = getMethod.getResponseBodyAsString();
				System.out.println(result);
			}
		}catch(Exception e){
			
		}
	}
}

/**
 * 获取到的正常返回结果:
 * 
 * {"refresh_token":"25.646dde7abf76b9bdcd8af7550f0ebe0c.315360000.1897026620.282335-18471643",
 *  "expires_in":2592000,
 *  "session_key":"9mzdDAYsciokc8tFn6yQqnTFoIIafKMPIQMk8zxQXLaX6l2ywhh+mR5q18MnNQuPVvNJotit0AWwajikYLvUuetANro2lw==",
 *  "access_token":"24.7328eb90ba8209337fa851e8e8182c6b.2592000.1584258620.282335-18471643",
 *  "scope":"public vis-ocr_ocr vis-classify_dishes brain_ocr_scope vis-classify_car brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number vis-classify_animal vis-classify_plant brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_solution_iocr brain_ingredient brain_qrcode brain_ocr_handwriting brain_ocr_passport brain_ocr_vat_invoice brain_advanced_general_classify brain_custom_dish brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate brain_poi_recognize vis-ocr_\u53f0\u6e7e\u901a\u884c\u8bc1 vis-ocr_\u6e2f\u6fb3\u901a\u884c\u8bc1 vis-ocr_\u673a\u52a8\u8f66\u68c0\u9a8c\u5408\u683c\u8bc1\u8bc6\u522b vis-ocr_\u8f66\u8f86vin\u7801\u8bc6\u522b vis-ocr_\u5b9a\u989d\u53d1\u7968\u8bc6\u522b brain_vehicle_detect vis-ocr_\u4fdd\u5355\u8bc6\u522b vis-ocr_\u884c\u7a0b\u5355\u8bc6\u522b brain_redwine brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_*_pass brain_ocr_vehicle_certificate brain_currency brain_vehicle_damage brain_ocr_air_ticket brain_ocr_insurance_doc wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\u6743\u9650 vis-classify_flower lpq_\u5f00\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\u5f00\u653eScope vis-ocr_\u865a\u62df\u4eba\u7269\u52a9\u7406 idl-video_\u865a\u62df\u4eba\u7269\u52a9\u7406",
 *  "session_secret":"8b1f811b41eced328bc48c0dabaf279d"}
*/

 sdk完整实现代码,Sample.java:

package com.baidu.sdk.ocr;

import java.util.HashMap;

import org.json.JSONObject;

import com.baidu.aip.ocr.AipOcr;

public class Sample {
    //设置APPID/AK/SK
    public static final String APP_ID = "18471643";
    public static final String API_KEY = "jNEhoBwPhbUOsVxnQBrcnBZB";
    public static final String SECRET_KEY = "5tXebMWoEdGDnjGGkL3BEDrhmwaNqnRr";

    public static void main(String[] args) {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

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

        // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");*/

        // 调用接口
        String path = "C:/Users/edz/Desktop/1.png";
        JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
        System.out.println(res.toString(2));
        
    }
}

 

 

相关标签: ocr