0. Alibaba Cloud OCR에는 온라인 인식 인터페이스가 있으며 httpPOST를 호출하여 직접 구현할 수 있으며 개발 속도가 매우 빠릅니다. 인식률이 상당히 좋고, 비스듬히 촬영해도 카메라를 인식할 수 있습니다. 측정된 인식 시간은 시간당 약 2초로 일반적인 사용 요구 사항을 충족할 수 있습니다.
1. 먼저 알리바바 클라우드 페이지에서 등록 후 무료 체험판을 신청한 후 제품을 구매해 사용 횟수를 늘릴 수 있습니다.
[일반 OCR 문자 인식] - 일반 문자 인식/문자 인식 OCR/이미지 인식/그림 인식 문자/OCR 인식 [최신버전]_OCR_인공지능_API-클라우드마켓-알리바바클라우드
2. 신청이 완료되면 서비스 페이지에서 AppCode를 확인할 수 있으며, 이 코드를 구성하고 사용하는 것은 매우 중요합니다.
3. 업로드된 사진은 비트맵의 base64 인코딩 기능을 사용하여 BASE64로 인코딩되어야 합니다.
public static String bitmapToBase64(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
4. OCR 호출
public static void ocrOnline(String strPictureBase64) {
String strRet = "";
String host = "https://tysbgpu.market.alicloudapi.com";
String path = "/api/predict/ocr_general";
String method = "POST";
String appcode = "自己的code";
String bodys = "{\"image\":\"" + strPictureBase64 + "\",\"configure\":{\"output_prob\":true,\"output_keypoints\":false,\"skip_detection\":false,\"without_predicting_direction\":false}}";
//Log.i("OCR", "bodys:" + bodys);
String strURL = host + path; //请求地址
Log.i("OCR", "strURL:" + strURL);
try {
// 创建URL对象
URL url = new URL(strURL);
// 创建HttpURLConnection对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置请求方法为POST
conn.setRequestMethod(method);
// 设置请求属性
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Authorization", "APPCODE " + appcode);
// 发送请求
OutputStream os = conn.getOutputStream();
os.write(bodys.getBytes(StandardCharsets.UTF_8));
os.flush();
os.close();
// 处理服务器响应
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
strRet += line;
}
in.close();
/*
{
"request_id": "7879F05E-28F7-427B-8147-BB6A3957E9B4",
"ret": [{
"prob": 0.99,
"rect": {
"angle": 0,
"height": 121,
"left": 756,
"top": 1830,
"width": 1295
},
"word": "QZ729202308300001"
}],
"success": true
}
}
*/
Log.i("OCR", "ret :" + strRet);
JSONObject jsonObject = JSON.parseObject(strRet);
if(jsonObject.getBooleanValue("success"))
{
JSONArray jsonArray = jsonObject.getJSONArray("ret");
String str = jsonArray.getJSONObject(0).getString("word");
Log.i("OCR", "str:" + str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
5. 버튼 응답 기능은 ocrOnline을 직접 호출할 수 없으며, 호출하려면 스레드를 추가해야 하므로 직접 호출할 수 있는 인터페이스는 다음과 같습니다.
public static void callOCROnline(Bitmap bitmap)
{
new Thread(new Runnable() {
@Override
public void run() {
ocrOnline(bitmapToBase64(bitmap));
}
}).start();
}
5. 휴대폰 후면 카메라와 결합하여 인식 효과 호출
카메라 호출에 대해서는 다음 글을 참고해주세요.
업계 초보분들의 경험을 공유해주시고, 틀린 부분이 있으면 지적해주세요~
저작권은 심천 Qizhi 기술 유한 회사 - Huahua에 속합니다.