栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot图片上传 百度ocr文字识别提取

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot图片上传 百度ocr文字识别提取

效果

前提:百度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")>
    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;
        }
    }
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778443.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号