效果
前提:百度ocr需要自行免费申请,在百度云里面自己申请,获得APP-ID,key和secret的一些必要参数。
- 需要在pom.xml中添加:(添加使用百度ocr接口的环境)
4.16.3 com.baidu.aip java-sdk${baiduocr.version}
2.在application.yml里面添加OCR配置3.html页面以及js
OCX辅助
<#include "/header.html">
<#if shiro.hasPermission("archive:ocr:uploadOcr")>#if>
var vm = new Vue({
el:'#rrapp',
data:{
remark:''
},
methods: {
uploadOcr: function (event) {
var ocrshow=$("#ocrshow").val();
if(ocrshow){
let myfile = this.$refs.myfile;
let files = myfile.files;
let file = files[0];
var formData = new FormData();
formData.append("file", file);
let url= baseURL + 'archive/ocr/uploadOcr';
$.ajax({
type:"POST",
dataType:"json",
url:url,
data:formData,
async:false,
cache:false,
contentType:false,
processdata:false,
success:function(res){
vm.remark=res;
}
})
}else{
vm.remark="此账号无识别权限!";
}
}
}
});
4.Controller
package com.framework.modules.ocr;
import com.baidu.aip.ocr.AipOcr;
import com.framework.common.config.OcrApiConfig;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
@RestController
@RequestMapping("archive/ocr")
public class OcrController {
@Autowired
private OcrApiConfig ocrApiConfig;
@PostMapping("/uploadOcr")
@ResponseBody
public String uploadOcr(MultipartFile file) throws IOException {
StringBuffer str=new StringBuffer();
HashMap options = new HashMap();
options.put("language_type", "CHN_ENG");
AipOcr client=new AipOcr(ocrApiConfig.getAppId(),ocrApiConfig.getApiKey(),ocrApiConfig.getSecretKey());
byte [] bite=file.getBytes();
JSonObject jsonObject=client.basicGeneral(bite,options);
if(jsonObject.toString().contains("error_code")){
str.append("文件识别有误");
}else{
JSonArray jsonArray= jsonObject.getJSonArray("words_result");
for (int i = 0; i < jsonArray.length(); i++) {
JSonObject ob= (JSONObject)jsonArray.get(i);
str.append(ob.get("words")).append("n");
}
}
return str.toString();
}
}
package com.framework.common.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix="ocr")
public class OcrApiConfig {
private String appId;
private String apiKey;
private String secretKey;
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
}



