日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 百度ocr文字识别-发票识别,并在页面显示信息

發布時間:2023/12/18 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 百度ocr文字识别-发票识别,并在页面显示信息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最底下有全部代碼鏈接

架構目錄如下

?

效果如下

?

其中access_token獲取方法看官網文檔http://ai.baidu.com/docs#/Auth/top

我用的Fiddler

?

Controller層

package ocr.controller;import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.util.Map;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest;import com.alibaba.fastjson.JSONObject;import ocr.pojo.Invoice; import tools.OcrTools;@Controller @RequestMapping("/invoice") public class OcrController {@Autowiredprivate OcrTools ocrTools;@RequestMapping("/index")public String index(Model m) {return "/static/testOcr.jsp";} @RequestMapping(value="/getMsg", method=RequestMethod.POST)@ResponseBodypublic Invoice upload(MultipartHttpServletRequest filesRequest) {MultipartFile file = filesRequest.getFile("file");try {InputStream in = file.getInputStream();String ocrResult = ocrTools.getocrByInputStream(in);Map resultMap = JSONObject.parseObject(ocrResult,Map.class);String invoiceString = JSONObject.toJSONString(resultMap.get("words_result"));Invoice invoice = JSONObject.parseObject(invoiceString,Invoice.class);return invoice;} catch (IOException e) {e.printStackTrace();}return null;} }

?

實體層

package ocr.pojo;import java.math.BigDecimal;import org.springframework.stereotype.Controller;@Controller public class Invoice {private String invoiceCode; //發票代碼private String invoiceDate; //開票時間private String purchaserName; //購方名稱private String purchaserRegisterNum; //購方納稅人識別號private BigDecimal amountInFiguers; //價稅合計private String sellerName; //售方名稱private String sellerRegisterNum; //售方納稅人識別號public String getInvoiceCode() {return invoiceCode;}public void setInvoiceCode(String invoiceCode) {this.invoiceCode = invoiceCode;}public String getInvoiceDate() {return invoiceDate;}public void setInvoiceDate(String invoiceDate) {this.invoiceDate = invoiceDate;}public String getPurchaserName() {return purchaserName;}public void setPurchaserName(String purchaserName) {this.purchaserName = purchaserName;}public String getPurchaserRegisterNum() {return purchaserRegisterNum;}public void setPurchaserRegisterNum(String purchaserRegisterNum) {this.purchaserRegisterNum = purchaserRegisterNum;}public BigDecimal getAmountInFiguers() {return amountInFiguers;}public void setAmountInFiguers(BigDecimal amountInFiguers) {this.amountInFiguers = amountInFiguers;}public String getSellerName() {return sellerName;}public void setSellerName(String sellerName) {this.sellerName = sellerName;}public String getSellerRegisterNum() {return sellerRegisterNum;}public void setSellerRegisterNum(String sellerRegisterNum) {this.sellerRegisterNum = sellerRegisterNum;}@Overridepublic String toString() {return "Invoice [invoiceCode=" + invoiceCode + ", invoiceDate=" + invoiceDate + ", purchaserName="+ purchaserName + ", purchaserRegisterNum=" + purchaserRegisterNum + ", amountInFiguers="+ amountInFiguers + ", sellerName=" + sellerName + ", sellerRegisterNum=" + sellerRegisterNum + "]";} }

?

獲取識別文字tools層

package tools;import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Base64;import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicHeader; import org.springframework.stereotype.Component;@Component public class OcrTools {/*** 識別圖片* @param filePath 圖片路徑* @return 識別結果*/public static String getocrByInputStream(InputStream in){byte[] fileByte = getFileBytes(in);// 獲取圖片字節數組String base64UrlencodedImg = base64Urlencode(fileByte);// 編碼return sendOcr(base64UrlencodedImg);// 發送給百度進行文字識別}/*** 傳入base64 + UrlEncode 編碼后的圖片* 得到圖片解析結果字符串(百度返回的Json)* @param base64UrlencodedImg* @return* @throws ClientProtocolException* @throws IOException*/public static String sendOcr(String base64UrlencodedImg){CloseableHttpClient httpclient = HttpClients.createMinimal();HttpPost post = new HttpPost("https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice?access_token=填自己的!!!");Header header = new BasicHeader("Content-Type","application/x-www-form-urlencoded");post.setHeader(header);try {HttpEntity entity = new StringEntity("image=" + base64UrlencodedImg);post.setEntity(entity);CloseableHttpResponse response = httpclient.execute(post);InputStream in = response.getEntity().getContent();ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1000]; int n; while ((n = in.read(b)) != -1) { bos.write(b, 0, n); }in.close();bos.close(); byte[] buffer = bos.toByteArray(); return new String(buffer,"utf-8");}catch(Exception e){e.printStackTrace();}return null;}/*** 圖片轉字節數組* @param filePath 圖片本地路徑* @return 圖片字節數組*/private static byte[] getFileBytes(InputStream in){ byte[] buffer = null; try { // File file = new File(filePath); // FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1000]; int n; while ((n = in.read(b)) != -1) { bos.write(b, 0, n); } in.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer;}/*** 對字節數組進行base64編碼與url編碼* @param b* @return*/private static String base64Urlencode(byte[] b) {byte[] base64Img = Base64.getEncoder().encode(b);try {String base64UrlencodedImg = URLEncoder.encode(new String(base64Img), "utf-8");return base64UrlencodedImg;} catch (UnsupportedEncodingException e) {e.printStackTrace();}return null;}/*** InputStream 轉String* @param is* @return*/public String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "/n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } }

?

頁面jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> <title>Insert title here</title> </head> <body><div class="container"><input type="file" name="file" style="display:none" id="fileInput"><button type="button" class="btn btn-default" id="choice">選擇圖片</button><button type="submit" class="btn btn-default" id="submit">識別</button><table class="table table-bordered"><tr><td>發票代碼</td><td id="invoiceCode"></td><td>開票時間</td><td id="invoiceDate"></td></tr><tr><td>購方名稱</td><td id="purchaserName"></td><td>購方納稅人識別號</td><td id="purchaserRegisterNum"></td></tr><tr><td>價稅合計</td><td id="amountInFiguers"></td></tr><tr><td>售方名稱</td><td id="sellerName"></td><td>售方納稅人識別號</td><td id="sellerRegisterNum"></td></tr></table></div><script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script><script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script><script type="text/javascript">$('#choice').click(function(){$('#fileInput').click();});$('#submit').click(function(){var formData = new FormData();formData.append('file',$('#fileInput')[0].files[0]);$.ajax({url:'/ocr/invoice/getMsg',type:'POST',data:formData,cache:false,processData:false,contentType:false,success:function(result){$('#invoiceCode').text(result.invoiceCode);$('#invoiceDate').text(result.invoiceDate);$('#purchaserName').text(result.purchaserName);$('#purchaserRegisterNum').text(result.purchaserRegisterNum);$('#amountInFiguers').text(result.amountInFiguers);$('#sellerName').text(result.sellerName);$('#sellerRegisterNum').text(result.sellerRegisterNum);}});});</script> </body> </html>

?

配置文件和pom.xml不詳細貼了

?

完整代碼

鏈接:https://pan.baidu.com/s/1uo8aFDtX5es4kkTIGEvXiA?
提取碼:gb7x?
?

想學習更多看官方文檔https://cloud.baidu.com/doc/OCR/s/ijwvxzq2g

總結

以上是生活随笔為你收集整理的Java 百度ocr文字识别-发票识别,并在页面显示信息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。