/*
* Copyright 2017 Baidu, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.chenframework.common.baiduai.ocr;
import com.chenframework.common.baiduai.client.BaseClient;
import com.chenframework.common.baiduai.error.AipError;
import com.chenframework.common.baiduai.http.AipRequest;
import com.chenframework.common.baiduai.util.Base64Util;
import com.chenframework.common.baiduai.util.Util;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
public class AipOcr extends BaseClient {
public AipOcr(String appId, String apiKey, String secretKey) {
super(appId, apiKey, secretKey);
}
/**
* 通用文字识别接口
* 用户向服务请求识别某张图中的所有文字
*
* @param image - 二进制图像数据
* @param options - 可选参数对象,key: value都为string类型
* options - options列表:
* language_type 识别语言类型,默认为CHN_ENG。可选值包括:<br>- CHN_ENG:中英文混合;<br>- ENG:英文;<br>- POR:葡萄牙语;<br>- FRE:法语;<br>- GER:德语;<br>- ITA:意大利语;<br>- SPA:西班牙语;<br>- RUS:俄语;<br>- JAP:日语;<br>- KOR:韩语;
* detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
* detect_language 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
* probability 是否返回识别结果中每一行的置信度
* @return JSONObject
*/
public JSONObject basicGeneral(byte[] image, HashMap<String, String> options) {
AipRequest request = new AipRequest();
preOperation(request);
String base64Content = Base64Util.encode(image);
request.addBody("image", base64Content);
if (options != null) {
request.addBody(options);
}
request.setUri(OcrConsts.GENERAL_BASIC);
postOperation(request);
return requestServer(request);
}
/**
* 通用文字识别接口
* 用户向服务请求识别某张图中的所有文字
*
* @param image - 本地图片路径
* @param options - 可选参数对象,key: value都为string类型
* options - options列表:
* language_type 识别语言类型,默认为CHN_ENG。可选值包括:<br>- CHN_ENG:中英文混合;<br>- ENG:英文;<br>- POR:葡萄牙语;<br>- FRE:法语;<br>- GER:德语;<br>- ITA:意大利语;<br>- SPA:西班牙语;<br>- RUS:俄语;<br>- JAP:日语;<br>- KOR:韩语;
* detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
* detect_language 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
* probability 是否返回识别结果中每一行的置信度
* @return JSONObject
*/
public JSONObject basicGeneral(String image, HashMap<String, String> options) {
try {
byte[] data = Util.readFileByBytes(image);
return basicGeneral(data, options);
} catch (IOException e) {
e.printStackTrace();
return AipError.IMAGE_READ_ERROR.toJsonResult();
}
}
/**
* 通用文字识别接口
* 用户向服务请求识别某张图中的所有文字
*
* @param url - 图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,当image字段存在时url字段失效
* @param options - 可选参数对象,key: value都为string类型
* options - options列表:
* language_type 识别语言类型,默认为CHN_ENG。可选值包括:<br>- CHN_ENG:中英文混合;<br>- ENG:英文;<br>- POR:葡萄牙语;<br>- FRE:法语;<br>- GER:德语;<br>- ITA:意大利语;<br>- SPA:西班牙语;<br>- RUS:俄语;<br>- JAP:日语;<br>- KOR:韩语;
* detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
* detect_language 是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
* probability 是否返回识别结果中每一行的置信度
* @return JSONObject
*/
public JSONObject basicGeneralUrl(String url, HashMap<String, String> options) {
AipRequest request = new AipRequest();
preOperation(request);
request.addBody("url", url);
if (options != null) {
request.addBody(options);
}
request.setUri(OcrConsts.GENERAL_BASIC);
postOperation(request);
return requestServer(request);
}
/**
* 通用文字识别(高精度版)接口
* 用户向服务请求识别某张图中的所有文字,相对于通用文字识别该产品精度更高,但是识别耗时会稍长。
*
* @param image - 二进制图像数据
* @param options - 可选参数对象,key: value都为string类型
* options - options列表:
* detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
* probability 是否返回识别结果中每一行的置信度
* @return JSONObject
*/
public JSONObject basicAccurateGeneral(byte[] image, HashMap<String, String> options) {
AipRequest request = new AipRequest();
preOperation(request);
String base64Content = Base64Util.encode(image);
request.addBody("image", base64Content);
if (options != null) {
request.addBody(options);
}
request.setUri(OcrConsts.ACCURATE_BASIC);
postOperation(request);
return requestServer(request);
}
/**
* 通用文字识别(高精度版)接口
* 用户向服务请求识别某张图中的所有文字,相对于通用文字识别该产品精度更高,但是识别耗时会稍长。
*
* @param image - 本地图片路径
* @param options - 可选参数对象,key: value都为string类型
* options - options列表:
* detect_direction 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:<br>- true:检测朝向;<br>- false:不检测朝向。
* probability 是否返回识别结果中每一行的置信度
* @return JSONObject
*/
public JSONObject basicAccurateGeneral(String image, HashMap<String, String> options) {
try {
byte[] data = Util.readFileByBytes(image);
return basicAccurateGeneral(data, options);
} catch (IOException e) {
e.printStackTrace();
return AipError.IMAGE_READ_ERROR.toJsonResult();
}
}
/**
* 通用文