Vue+Java+Base64實現(xiàn)條碼解析的示例
前端部分(Vue + Vant)
- 引入Vant、使用Vant中的Uploader組件上傳文件(支持手機(jī)拍照)
import Vue from 'vue' import { Uploader } from 'vant' Vue.use(Uploader);
- 使用Uploader上傳組件
<van-uploader> <van-button icon="plus" type="primary" :after-read="afterRead"> 上傳文件(識別條碼) </van-button> </van-uploader>
- js部分、文件上傳完畢后會觸發(fā)
after-read
回調(diào)函數(shù),獲取到對應(yīng)的file
對象。
afterRead(file) { var self = this; //調(diào)用上傳回調(diào)函數(shù) - upload this.upLoad(this.$baseUrl + "upload/uploadParsing", file, function (response) { if( response.msg.length >0){ console.log(response.msg) }else{ Toast.fail('識別失敗,請重新上傳條碼!',3500) } }); }, upLoad(url, file, func) { var fileBase64 ='' // 創(chuàng)建Canvas對象(畫布) debugger let canvas = document.createElement("canvas"); // 獲取對應(yīng)的CanvasRenderingContext2D對象(畫筆) let context = canvas.getContext("2d"); // 創(chuàng)建新的圖片對象 let img = new Image(); // 指定圖片的DataURL(圖片的base64編碼數(shù)據(jù)) img.src = file.content; // 監(jiān)聽瀏覽器加載圖片完成,然后進(jìn)行進(jìn)行繪制 img.onload = () => { // 指定canvas畫布大小,該大小為最后生成圖片的大小 canvas.width = 400; canvas.height = 300; /* drawImage畫布繪制的方法。(0,0)表示以Canvas畫布左上角為起點,400,300是將圖片按給定的像素進(jìn)行縮小。 如果不指定縮小的像素圖片將以圖片原始大小進(jìn)行繪制,圖片像素如果大于畫布將會從左上角開始按畫布大小部分繪制圖片,最后的圖片就是張局部圖。*/ context.drawImage(img, 0, 0, 400, 300); // 將繪制完成的圖片重新轉(zhuǎn)化為base64編碼,file.file.type為圖片類型,0.92為默認(rèn)壓縮質(zhì)量 file.content = canvas.toDataURL(file.file.type, 0.92); fileBase64 = file.content // 最后將base64編碼的圖片保存到數(shù)組中,留待上傳。43 console.log(fileBase64) //查詢字典值 this.$axios.post(url,{'fileBase64Code' :fileBase64}) .then(function (response) { func(response.data); }.bind(this)) .catch(function (error) { Toast.file("識別失敗,請重新上傳條碼!",3500); }) }; },
后端部分(Java )
添加 zxing + base64 依賴
<!-- 解析二維碼 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.3</version> </dependency> <!-- Base64 --> <!-- https://mvnrepository.com/artifact/net.iharder/base64 --> <dependency> <groupId>net.iharder</groupId> <artifactId>base64</artifactId> <version>2.3.8</version> </dependency>
Controller
@ResponseBody @RequestMapping(value = "/uploadParsing", method = RequestMethod.POST) public ResponseMessage uploadParsing(@RequestBody imgUploadMessage uploadFile){ ResponseMessage rm=new ResponseMessage(); //解析Base64編碼之后 讀取條 try { String imgStr = uploadFile.getFileBase64Code().substring(uploadFile.getFileBase64Code().indexOf(",")+1); Decoder decoder = Base64.getDecoder(); byte[] base = decoder.decode(imgStr); for (int i = 0; i < base.length; ++i) { if (base[i] < 0) { base[i] += 256; } } ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base); BufferedImage read = ImageIO.read( byteArrayInputStream); if (null==read) { rm.setMsg("解析失敗"); rm.setSuccess(false); return rm; } BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(read); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Map<DecodeHintType,Object> hints=new HashMap<>(); hints.put(DecodeHintType.CHARACTER_SET,"GBK"); hints.put(DecodeHintType.PURE_BARCODE,Boolean.TRUE); hints.put(DecodeHintType.TRY_HARDER,Boolean.TRUE); Result decode = new MultiFormatReader().decode(bitmap, hints); log.debug("條形碼的內(nèi)容是:" + decode.getText()); rm.setMsg(decode.getText()); } catch (Exception e) { e.printStackTrace(); log.debug("解析失?。?,e); rm.setSuccess(false); rm.setMsg("解析失敗"); } return rm; }
以上就是Vue+Java+Base64實現(xiàn)條碼解析的示例的詳細(xì)內(nèi)容,更多關(guān)于Vue+Java+Base64實現(xiàn)條碼解析的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue.js中使用${}實現(xiàn)變量和字符串的拼接方式
這篇文章主要介紹了Vue.js中使用${}實現(xiàn)變量和字符串的拼接方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07詳解基于vue-cli3快速發(fā)布一個fullpage組件
這篇文章主要介紹了詳解基于vue-cli3快速發(fā)布一個fullpage組件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03vue用戶長時間不操作退出到登錄頁的兩種實現(xiàn)方式
出于安全考慮,用戶長時間不操作,就回到登錄頁面,讓用戶重新登錄,本文就記錄一下實現(xiàn)這種效果的兩種方式,具有一定的參考價值,感興趣的可以了解一下2021-09-09ElementUI時間選擇器限制選擇范圍disabledData的使用
本文主要介紹了ElementUI時間選擇器限制選擇范圍disabledData的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06淺析Vue2和Vue3中雙向綁定機(jī)制的優(yōu)化與差異
Vue.js?核心特性之一就是雙向綁定,本文將深入探討?Vue2?和?Vue3?在雙向綁定上的區(qū)別,并分析這些改進(jìn)對性能和開發(fā)體驗的影響,希望對大家有所幫助2024-11-11Vue?socket.io模塊實現(xiàn)聊天室流程詳解
vue-socket.io其實是在socket.io-client(在瀏覽器和服務(wù)器之間實現(xiàn)實時、雙向和基于事件的通信)基礎(chǔ)上做了一層封裝,將socket掛載到vue實例上,同時可使用sockets對象輕松實現(xiàn)組件化的事件監(jiān)聽,在vue項目中使用起來更方便2022-12-12