请求URL:http(s)://ocr-api.shiliuai.com/api/bank_card_ocr/v1
请求方式:POST
返回类型:JSON
参数 | 类型 | 说明 |
---|---|---|
Authorization | string | 'APPCODE %s' % appcode |
Content-type | string | application/json |
参数 | 类型 | 说明 |
---|---|---|
x-ca-key | string | app_key |
x-ca-timestamp | string | 毫秒时间戳 |
x-ca-signature | string | sign |
Content-type | string | application/json |
参数 | 是否必填 | 类型 | 说明 |
---|---|---|---|
image_base64 | 必填 | string | base64编码的图片文件 |
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.commons.codec.binary.Base64; public class Demo { public static String get_base64(String path) { String b64=""; try { File file = new File(path); byte[] content = new byte[(int) file.length()]; FileInputStream finputstream = new FileInputStream(file); finputstream.read(content); finputstream.close(); b64 = new String(Base64.encodeBase64(content)); } catch (IOException e) { e.printStackTrace(); return b64; } return b64; } public static void main(String[] args) { String url = "https://ocr-api.shiliuai.com/api/bank_card_ocr/v1"; // 请求接口 String appcode = "你的APPCODE"; String imgFile = "本地图片路径"; String method = "POST"; Mapheaders = new HashMap (); headers.put("Authorization", "APPCODE " + appcode); # 注意英文空格 headers.put("Content-Type", "application/json"); // base64 Map querys = new HashMap (); String imgBase64 = get_base64(imgFile); // 请求体 JSONObject requestObj = new JSONObject(); requestObj.put("image_base64", imgBase64); String bodys = requestObj.toString(); try { HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); int stat = response.getStatusLine().getStatusCode(); if(stat != 200){ System.out.println("Http code: " + stat); return; } String res = EntityUtils.toString(response.getEntity()); JSONObject res_obj = JSON.parseObject(res); System.out.println(res_obj.toJSONString()); } catch (Exception e) { e.printStackTrace(); } } }
//图片转base64 function get_base64($path){ if($fp = fopen($path, "rb", 0)) { $binary = fread($fp, filesize($path)); // 文件读取 fclose($fp); $b64 = base64_encode($binary); // 转base64 }else{ $b64=""; printf("%s 文件不存在", $path); } return $b64; } $url = "https://ocr-api.shiliuai.com/api/bank_card_ocr/v1"; $appcode = "你的appcode"; $img_path = "图片路径"; $method = "POST"; //请求头 $headers = array(); array_push($headers, "Authorization:APPCODE " . $appcode); array_push($headers, "Content-Type:application/json"); //请求体 $b64 = get_base64($img_path); $data = array( "image_base64" => $b64 ); $post_data = json_encode($data); //请求 $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_FAILONERROR, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); $result = curl_exec($curl); var_dump($result);
# -*- coding: utf-8 -*- import requests import base64 import json # 请求接口 URL = "https://ocr-api.shiliuai.com/api/bank_card_ocr/v1" # 图片转base64 def get_base64(file_path): with open(file_path, 'rb') as f: data = f.read() b64 = base64.b64encode(data).decode('utf8') return b64 def demo(appcode, file_path): # 请求头 headers = { 'Authorization': 'APPCODE %s' % appcode, 'Content-Type': 'application/json' } # 请求体 b64 = get_base64(file_path) data = {"image_base64": b64} # 请求 response = requests.post(url=URL, headers=headers, json=data) content = json.loads(response.content) print(content) if __name__=="__main__": appcode = "你的APPCODE" file_path = "本地图片路径" demo(appcode, file_path)
import requests import base64 import json import hashlib import time # 请求接口 URL = "https://ocr-api.shiliuai.com/api/bank_card_ocr/v1" # 图片转base64 def get_base64(file_path): with open(file_path, 'rb') as f: data = f.read() b64 = base64.b64encode(data).decode('utf8') return b64 # md5 def md5(s): return hashlib.md5(s.encode("utf8")).hexdigest() def demo(app_key, app_secret, file_path): # 请求头 t = int(time.time() * 1000) s = "%s%d%s" % (app_key, t, app_secret) sign = md5(s) headers = {'x-ca-key': app_key, 'x-ca-timestamp': t, 'x-ca-signature': sign, "Content-Type": "application/json"} # 请求体 b64 = get_base64(file_path) data = {"image_base64": b64} # 请求 response = requests.post(url=URL, headers=headers, json=data) content = json.loads(response.content) print(content) if __name__=="__main__": app_key = "你的APP_KEY" app_secret = "你的APP_SECRET" file_path = "本地图片路径" demo(app_key, app_secret, file_path)
function get_base64($path){ if($fp = fopen($path, "rb", 0)) { $binary = fread($fp, filesize($path)); // 文件读取 fclose($fp); $b64 = base64_encode($binary); // 转base64 }else{ $b64=""; printf("%s 文件不存在", $path); } return $b64; } $url = "https://ocr-api.shiliuai.com/api/bank_card_ocr/v1"; $img_path = "图片路径"; $method = "POST"; //请求头 $app_key = "你的app_key"; $app_secret = "你的app_secret"; $sign_string = $app_key . "&" . $timestamp . "&" . $app_secret; $sign = md5($sign_string); $headers = array(); array_push($headers, "Content-Type:application/json"); array_push($headers, "x-ca-key:" . $app_key); array_push($headers, "x-ca-timestamp:" . $timestamp); array_push($headers, "x-ca-signature:" . $sign); //请求体 $b64 = get_base64($img_path); $data = array( "image_base64" => $b64 ); $post_data = json_encode($data); //请求 $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_FAILONERROR, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); $result = curl_exec($curl); var_dump($result);
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.commons.codec.binary.Base64; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Demo { public static String get_base64(String path) { String b64=""; try { File file = new File(path); byte[] content = new byte[(int) file.length()]; FileInputStream finputstream = new FileInputStream(file); finputstream.read(content); finputstream.close(); b64 = new String(Base64.encodeBase64(content)); } catch (IOException e) { e.printStackTrace(); return b64; } return b64; } public static String MD5(String input) { String result = ""; try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] messageDigest = md.digest(sourceStr.getBytes()); StringBuilder hexString = new StringBuilder(); for (byte b : messageDigest) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) hexString.append('0'); hexString.append(hex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { String url = "https://ocr-api.shiliuai.com/api/bank_card_ocr/v1"; // 请求接口 String imgFile = "本地图片路径"; String method = "POST"; String app_key = "你的APPKEY"; String app_secret = "你的APPSECRET"; String timestamp = System.currentTimeMillis() + ""; String sign = MD5(app_key + "&" + timestamp + "&" + app_secret)); Mapheaders = new HashMap (); headers.put("Content-Type", "application/json"); headers.put("x-ca-key", app_key); headers.put("x-ca-timestamp", timestamp); headers.put("x-ca-signature", sign); // base64 Map querys = new HashMap (); String imgBase64 = get_base64(imgFile); // 请求体 JSONObject requestObj = new JSONObject(); requestObj.put("image_base64", imgBase64); String bodys = requestObj.toString(); try { HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); int stat = response.getStatusLine().getStatusCode(); if(stat != 200){ System.out.println("Http code: " + stat); return; } String res = EntityUtils.toString(response.getEntity()); JSONObject res_obj = JSON.parseObject(res); System.out.println(res_obj.toJSONString()); } catch (Exception e) { e.printStackTrace(); } } }
参数 | 参数类型 | 说明 |
---|---|---|
code | int | 错误码 |
msg | string | 错误信息(英文) |
msg_cn | string | 错误信息(中文) |
success | bool | 识别是否成功 |
request_id | string | 唯一请求ID |
data | dict or list | 识别结果,不同api结果的格式不同 |
参数 | 参数类型 | 说明 | 举例 |
---|---|---|---|
card_number | string | 银行卡号 | |
bank_name | string | 银行名称 | 中国农业银行 |
card_type | string | 卡类型 | 借记卡 |
错误码 | 说明 |
---|---|
200 | 成功 |
400 | 错误请求,比如参数错误 |
401 | 未经授权 |
403 | 禁止访问 |
429 | 请求过多 |
500 | 内部错误 |
604 | 接口停用 |
1001 | 服务异常,返回具体的错误原因 |
银行卡OCR(API)定义:银行卡OCR实现用户信息的自动识别和录入,应用于金融场景用户远程的身份认证,识别速度快,准确率高,减少用户输入成本,有效提高用户的体验。
支持各种程序和设备接入,包括小程序、APP、采集设备等,灵活适用于不同应用场景。
银行卡OCR接口能够精准识别各类银行卡中的关键信息,包括卡号、卡种类、有效期等。其主要功能包括:
1.多类型覆盖:支持横卡、竖卡及银行卡多角度偏斜情况的识别与提取。
2.多银行:支持交通银行、中国工商银行、中国银行、邮政银行等国内大多数银行。
3.卡面多样:支持各种位数、凸字卡面、平面卡面等的识别。
4.超高精度与性能:识别准确率高,识别速度快,响应及时。
API类型 | 价格说明 |
---|---|
银行卡OCR API | 每次调用消耗一积分 |
如有问题联系右侧“客服” |