为了简便财务总是要对照着别人发来的表格图片制作成自己的表格

  • 图片识别 识别成表格 表格识别 ocr
  • 使用阿里云api
  • 购买(印刷文字识别-表格识别) https://market.aliyun.com/products/57124001/cmapi024968.html
  • 获得阿里云图片识别表格的appcode
效果图如下

整合的代码
package com.xai.wuye.controller.api;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.xai.wuye.common.JsonResult;
import com.xai.wuye.exception.ResultException;
import com.xai.wuye.model.AParam
import com.xai.wuye.service.CarService;
import com.xai.wuye.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;import static org.apache.tomcat.util.codec.binary.Base64.encodeBase64;@Controller
@EnableAsync
@RequestMapping("/api/ocr")
public class AliOCRImages {@AutowiredCarService carService;private String OcrPath = "/home/runApp/car/orc/";@ResponseBody@RequestMapping("table")public JsonResult getFirstLicence(@RequestParam(value = "file", required = false) MultipartFile file) {if (file == null || file.isEmpty()||file.getSize() > 1204*1204*3)throw new ResultException(0,"文件为null,且不能大于3M");String filename = file.getOriginalFilename();String filepath = OcrPath+"temp/"+filename;File newFile = new File(filepath);try {file.transferTo(newFile);String host = "https://form.market.alicloudapi.com";String path = "/api/predict/ocr_table_parse";// 输入阿里的codeString appcode = "4926a667ee6c41329c278361*****";String imgFile = "图片路径";Boolean is_old_format = false;//如果文档的输入中含有inputs字段,设置为True, 否则设置为False//请根据线上文档修改configure字段JSONObject configObj = new JSONObject();configObj.put("format", "xlsx");configObj.put("finance", false);configObj.put("dir_assure", false);String config_str = configObj.toString();//            configObj.put("min_size", 5);//String config_str = "";String method = "POST";Map<String, String> headers = new HashMap<String, String>();//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105headers.put("Authorization", "APPCODE " + appcode);Map<String, String> querys = new HashMap<String, String>();// 对图像进行base64编码String imgBase64 = "";try {byte[] content = new byte[(int) newFile.length()];FileInputStream finputstream = new FileInputStream(newFile);finputstream.read(content);finputstream.close();imgBase64 = new String(encodeBase64(content));} catch (IOException e) {e.printStackTrace();return null;}// 拼装请求body的json字符串JSONObject requestObj = new JSONObject();try {if(is_old_format) {JSONObject obj = new JSONObject();obj.put("image", getParam(50, imgBase64));if(config_str.length() > 0) {obj.put("configure", getParam(50, config_str));}JSONArray inputArray = new JSONArray();inputArray.add(obj);requestObj.put("inputs", inputArray);}else{requestObj.put("image", imgBase64);if(config_str.length() > 0) {requestObj.put("configure", config_str);}}} catch (JSONException e) {e.printStackTrace();}String bodys = requestObj.toString();try {/*** 重要提示如下:* HttpUtils请从* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java* 下载** 相应的依赖请参照* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml*/HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);int stat = response.getStatusLine().getStatusCode();if(stat != 200){System.out.println("Http code: " + stat);System.out.println("http header error msg: "+ response.getFirstHeader("X-Ca-Error-Message"));System.out.println("Http body error msg:" + EntityUtils.toString(response.getEntity()));return null;}String res = EntityUtils.toString(response.getEntity());JSONObject res_obj = JSON.parseObject(res);Long fileName = System.currentTimeMillis();if(is_old_format) {JSONArray outputArray = res_obj.getJSONArray("outputs");String output = outputArray.getJSONObject(0).getJSONObject("outputValue").getString("dataValue");JSONObject out = JSON.parseObject(output);System.out.println(out.toJSONString());}else{String tmp_base64path = OcrPath + fileName;File tmp_base64file = new File(tmp_base64path);if(!tmp_base64file.exists()){tmp_base64file.getParentFile().mkdirs();}tmp_base64file.createNewFile();// writeFileWriter fw = new FileWriter(tmp_base64file, true);BufferedWriter bw = new BufferedWriter(fw);bw.write(res_obj.getString("tables"));bw.flush();bw.close();fw.close();String exelFilePath = OcrPath + fileName + "_1.xlsx";Runtime.getRuntime().exec("touch "+exelFilePath).destroy();Process exec = Runtime.getRuntime().exec("sed -i -e 's/\\\\n/\\n/g' " + tmp_base64path);exec.waitFor();exec.destroy();Process exec1 = null;String[] cmd = { "/bin/sh", "-c", "base64 -d " + tmp_base64path + " > " + exelFilePath };exec1 = Runtime.getRuntime().exec(cmd);exec1.waitFor();exec1.destroy();return JsonResult.success(fileName);}} catch (Exception e) {e.printStackTrace();}} catch (IOException e) {e.printStackTrace();}return null;}@ResponseBody@RequestMapping("getId")public ResponseEntity<FileSystemResource> getFirstLicence(String id) {String exelFilePath = OcrPath + id + "_1.xlsx";return export(new File(exelFilePath));}public ResponseEntity<FileSystemResource> export(File file) {if (file == null) {return null;}HttpHeaders headers = new HttpHeaders();headers.add("Cache-Control", "no-cache, no-store, must-revalidate");headers.add("Content-Disposition", "attachment; filename=" + System.currentTimeMillis() + ".xls");headers.add("Pragma", "no-cache");headers.add("Expires", "0");headers.add("Last-Modified", new Date().toString());headers.add("ETag", String.valueOf(System.currentTimeMillis()));return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file));}public static JSONObject getParam(int type, String dataValue) {JSONObject obj = new JSONObject();try {obj.put("dataType", type);obj.put("dataValue", dataValue);} catch (JSONException e) {e.printStackTrace();}return obj;}}

大功告成

  • 以下是静态页面代码
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><!-- import CSS --><link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"><title>table</title>
</head>
<body><div id="app"><el-uploadclass="upload-demo"dragaction="https://www.***.com/car/api/ocr/table":file-list="imagelist":on-preview="pre"><i class="el-icon-upload"></i><div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div><div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div></el-upload><div class="img-content" v-for="(item,key) in imagelist" :key="key"><img :src="item.url"><div class="name"><div>{{ item.name }}</div><el-button type="text" @click="handleFileName(item,key)">修改名字</el-button></div><!-- 删除icon --><div class="del"><i @click="handleFileRemove(item,key)" class="el-icon-delete2"></i></div><!-- 放大icon --><div class="layer" @click="handleFileEnlarge(item.url)"><i class="el-icon-view"></i></div></div></div>
</body><!-- import Vue before Element --><script src="https://unpkg.com/vue/dist/vue.js"></script><!-- import JavaScript --><script src="https://unpkg.com/element-ui/lib/index.js"></script><script>new Vue({el: '#app',data: function() {return {visible: false,imagelist: []}},methods: {pre(res) {console.log(res.response.msg)window.open("https://www.***.com/api/ocr/getId?id="+res.response.data);}}})</script>
</html>

转载于:https://www.cnblogs.com/rolandlee/p/10671544.html

使用阿里云的图片识别成表格ocr(将图片表格转换成excel)相关推荐

  1. 关于使用阿里云服务调用识别身份证图片、营业执照的信息抓取接口的简单实现

    1:识别身份证你可以选择用   阿里开放平台提供或者百度开放平台的识别 同理用哪个就要去注册个帐号.自行百度. 再此,使用的是阿里云的人脸识别. 传送门: https://market.aliyun. ...

  2. 阿里云 aliyun 人脸识别(1:N) java spring 小程序 小程序上传多图 阿里云oss

    前段时间开发一个小程序需要使用到阿里云(1:N)人脸识别的服务,查询资料发现网上并没有详细的教程,而官方的api文档也写得很简略,于是就有了如下教程,希望能帮助到大家. 目录 服务开通 人脸识别服务开 ...

  3. 阿里云的内容识别技术可以实现哪些场景下的智能化应用?

    阿里云的内容识别技术可以实现哪些场景下的智能化应用? [本文由阿里云代理商[聚搜云]撰写] 随着人工智能技术的快速发展,阿里云借助自身的技术和资源优势,开发了一种名为"内容识别"的 ...

  4. Java spring boot 阿里云调用人脸识别接口,本地sdk上传到阿里云调用api

    Java spring boot 阿里云调用人脸识别接口 没有写测试类,工具类如下,有access_key_id和access_key_secret传参调用就可使用 代码如下: pom.xml依赖 & ...

  5. python 智能识别 商品_阿里云货架商品识别与管理Python SDK使用示例-阿里云开发者社区...

    概述 货架商品识别与管理(Retail Image Recognition)是基于深度学习.图像检测.图像识别等技术,为新零售品牌商/经销商提供AI商品识别能力的阿里云产品:适用于货架商品识别.陈列识 ...

  6. 使用 OpenAI、阿里云函数计算平台和 PyQt5 创造智能图片生成器

    使用 OpenAI.阿里云函数计算平台和 PyQt5 创造智能图片生成器 0. 效果演示 1. 引言 随着人工智能技术的不断发展,越来越多的应用开始融入我们的生活.在这篇博客中,我将介绍如何利用 Op ...

  7. 淘宝/天猫图片识别商品接口,1688图片识别商品API接口

    淘宝/天猫图片识别商品接口,1688图片识别商品API接口接口代码如下: 1.公共参数: 名称 类型 必须 描述 key String 是 调用key(必须以GET方式拼接在URL中) secret ...

  8. nodejs将图片的像素值提取出来将RGBA转换成16位彩色

    nodejs将图片的像素值提取出来将RGBA转换成16位彩色 RGBA转16位彩色的原理 RGBA由4*8bit组成(255, 255, 255,255),16位彩色为2*8bit组成.16位彩色需要 ...

  9. cad转换成jpg,怎样把cad转换成图片

    ​以下就是今天和大家分享的关于cad转换成jpg,怎样把cad转换成图片的演示操作: 方法一:CAD转换器 1.百度浏览器搜索\CAD转换器\点击进入内部页面,下载并安装到电脑桌面上.2.双击启动软件 ...

  10. json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值...

    json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值 原文:json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值 主要内容: 一 ...

最新文章

  1. 几个WIindows函数。坐标转换
  2. C++11 智能指针unique_ptr使用 -- 以排序二叉树为例
  3. MySQL-主从架构的搭建
  4. [Java基础]反射练习之越过泛型检查,运行配置文件制定内容
  5. 阿里巴巴制定了这 16 条
  6. 数据创造价值_展示数据并创造价值
  7. jqgrid mysql 分页_jQgrid 分页显示
  8. css文件插入背景音乐,h5页面添加背景音乐
  9. python代码大全和用法用量_Python生成器的使用方法和示例代码
  10. 建个数据中心就想发展IDC?没那么简单!
  11. [转]黑盒测试和白盒测试
  12. 如何实现parseFloat保留小数点后2位
  13. 小学生机器人挑战赛_厉害了我的小学生!德阳中小学生机器人大赛
  14. 黑马程序员_面向对象简介
  15. OEL6.5+11GR2安装(超级详细版)
  16. Java企业汽车调度系统
  17. 《影响力》(你为什么会说“是”?)读书
  18. STM32F103ZET6超声波测距(hcsr04模块)实验
  19. 面试官:你对Redis缓存了解吗?面对这11道面试题你是否有很多问号?
  20. Java核心基础知识汇总

热门文章

  1. Android使用xml自定义软键盘效果(附源码)
  2. 游戏建模与动画建模的区别,小白学建模前一定要知道
  3. bilibili_api,仅用 3 行代码获取B站(弹幕、评论、用户)数据
  4. 携手合作伙伴,傲腾技术加速释放数据中心潜能
  5. 每天看一个fortran文件(2)
  6. 实施ERP系统后需要维护吗?
  7. 字节跳动自研万亿级图数据库 图计算实践
  8. 安防4G摄像头视频流媒体服务器EasyNVR关于视频集成自我展示web端嵌入视频广场的流程
  9. 苹果地图副总裁_看完这篇文章 或许你会再给“苹果地图”一次机会
  10. 图片怎么缩小到300k?如何将图片缩小到300k以内?