一、添加依赖
<dependency><groupId>com.squareup.okhttp</groupId><artifactId>okhttp</artifactId><version>2.7.5</version></dependency>
二、编写一个工具类
package com.szfa.reg.ocr.utils;import org.apache.tomcat.util.codec.binary.Base64;import javax.crypto.Mac;import javax.crypto.spec.SecretKeySpec;/*** @author chengwu 封装了常用的MD5、SHA1、HmacSha1函数*/public class CommonCodecUtils {private static final String HMAC_SHA1 = "HmacSHA1";/*** 对二进制数据进行BASE64编码* * @param binaryData* 二进制数据* @return 编码后的字符串*/public static String Base64Encode(byte[] binaryData) {String encodedstr =new String(Base64.encodeBase64(binaryData));return encodedstr;}/*** 计算数据的Hmac值* * @param binaryData* 二进制数据* @param key* 秘钥* @return 加密后的hmacsha1值* @throws Exception 异常*/public static byte[] HmacSha1(byte[] binaryData, String key) throws Exception {Mac mac = Mac.getInstance(HMAC_SHA1);SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1);mac.init(secretKey);byte[] HmacSha1Digest = mac.doFinal(binaryData);return HmacSha1Digest;}/*** 计算数据的Hmac值** @param plainText* 文本数据* @param key* 秘钥* @return 加密后的hmacsha1值* @throws Exception 异常*/public static byte[] HmacSha1(String plainText, String key) throws Exception {return HmacSha1(plainText.getBytes(), key);}}
三、编写一个service实现类
package com.szfa.reg.ocr.service.impl;import com.squareup.okhttp.*;import com.szfa.reg.ocr.model.Weight;import com.szfa.reg.ocr.service.OcrService;import com.szfa.reg.monCodecUtils;import com.szfa.reg.ocr.utils.FtpClientUtils;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import .ftp.FTPClient;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Service;import org.springframework.web.multipart.MultipartFile;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.concurrent.TimeUnit;/*** @aouhor YangHuiLiang* @create /6/27*/@Servicepublic class OcrServiceImpl implements OcrService {@Value("${ocr.appId}")private String appId;@Value("${ocr.secretId}")private String secretId;@Value("${ocr.secretKey}")private String secretKey;private String bucketName = "";@Overridepublic String qualified(MultipartFile multipartFile) throws IOException {String authorization = appSign(300);MultipartBuilder multipartBuilder = new MultipartBuilder();multipartBuilder.addFormDataPart("bucket","");multipartBuilder.addFormDataPart("appid",appId);multipartBuilder.addFormDataPart("ret_image","0");multipartBuilder.type(MultipartBuilder.FORM);multipartBuilder.addPart(Headers.of("Content-Disposition", String.format("form-data; name=\"%s\"; filename=\"%s\"", "image[0]", "bytes" + System.currentTimeMillis())),RequestBody.create(MediaType.parse("image/jpg"), multipartFile.getBytes()));RequestBody requestBody = multipartBuilder.build();Request.Builder requestBuilder = new Request.Builder();requestBuilder.url("https://recognition./ocr/businesscard");requestBuilder.post(requestBody);requestBuilder.addHeader("Authorization",authorization);OkHttpClient okHttpClient = new OkHttpClient();okHttpClient.setConnectTimeout(60 * 1000, TimeUnit.MILLISECONDS);okHttpClient.setReadTimeout(60 * 1000, TimeUnit.MILLISECONDS);okHttpClient.setWriteTimeout(60 * 1000,TimeUnit.MILLISECONDS);Response response = okHttpClient.newCall(requestBuilder.build()).execute();return response.body().string();}/*** 获取签名* @param expired* @return*/private String appSign(long expired) {long now = System.currentTimeMillis() / 1000;String plainText = String.format("a=%s&b=%s&k=%s&t=%d&e=%d", appId, bucketName, secretId, now, now + expired);byte[] hmacDigest = new byte[0];try {hmacDigest = CommonCodecUtils.HmacSha1(plainText, secretKey);} catch (Exception e) {e.printStackTrace();}byte[] signContent = new byte[hmacDigest.length + plainText.getBytes().length];System.arraycopy(hmacDigest, 0, signContent, 0, hmacDigest.length);System.arraycopy(plainText.getBytes(), 0, signContent, hmacDigest.length, plainText.getBytes().length);return CommonCodecUtils.Base64Encode(signContent);}}
四、编写一个控制层
@RequestMapping("qualified")public String qualified(MultipartFile multipartFile) throws IOException {String info = ocrService.qualified(multipartFile);return info;}
以上就是通过腾讯云实现的ocr名片识别