Vue+Java+Base64实现条码解析的示例

作者:Mr_Bingzi 时间:2024-05-02 17:08:27 

前端部分(Vue + Vant)

  • 引入Vant、使用Vant中的Uploader组件上传文件(支持手机拍照)


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部分、文件上传完毕后会触发 after-read 回调函数,获取到对应的 file 对象。


afterRead(file) {
 var self = this;
 //调用上传回调函数 - 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 =''
   // 创建Canvas对象(画布)
   debugger
   let canvas = document.createElement("canvas");
   // 获取对应的CanvasRenderingContext2D对象(画笔)
   let context = canvas.getContext("2d");
   // 创建新的图片对象
   let img = new Image();
   // 指定图片的DataURL(图片的base64编码数据)
   img.src = file.content;
   // 监听浏览器加载图片完成,然后进行进行绘制
   img.onload = () => {
     // 指定canvas画布大小,该大小为最后生成图片的大小
     canvas.width = 400;
     canvas.height = 300;
     /* drawImage画布绘制的方法。(0,0)表示以Canvas画布左上角为起点,400,300是将图片按给定的像素进行缩小。
     如果不指定缩小的像素图片将以图片原始大小进行绘制,图片像素如果大于画布将会从左上角开始按画布大小部分绘制图片,最后的图片就是张局部图。*/

context.drawImage(img, 0, 0, 400, 300);
     // 将绘制完成的图片重新转化为base64编码,file.file.type为图片类型,0.92为默认压缩质量
     file.content = canvas.toDataURL(file.file.type, 0.92);
     fileBase64 = file.content
     // 最后将base64编码的图片保存到数组中,留待上传。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("条形码的内容是:" + decode.getText());
       rm.setMsg(decode.getText());

} catch (Exception e) {
       e.printStackTrace();
       log.debug("解析失败:",e);
       rm.setSuccess(false);
       rm.setMsg("解析失败");
     }
    return rm;
 }

来源:https://www.cnblogs.com/bingziweb/p/13713505.html

标签:Vue,Java,Base64,条码,解析
0
投稿

猜你喜欢

  • Python实现简单层次聚类算法以及可视化

    2023-09-04 00:16:21
  • Python字符串常用方法以及其应用场景详解

    2022-02-15 18:39:53
  • 生成静态页面的php函数,php爱好者站推荐

    2024-05-03 15:50:09
  • 解读! Python在人工智能中的作用

    2022-02-20 20:51:54
  • Vue自定义Form组件实现方法介绍

    2024-04-28 09:21:14
  • MYSQL必知必会读书笔记 第一章(基础)

    2024-01-20 09:23:52
  • MySQL null的一些易错点

    2024-01-22 13:00:30
  • java使用正则表达式查找包含的字符串示例

    2022-06-18 21:02:08
  • Python实现将蓝底照片转化为白底照片功能完整实例

    2021-06-08 21:40:55
  • Python实现计算两个时间之间相差天数的方法

    2022-11-09 06:04:59
  • JS中==与===操作符的比较

    2024-04-17 10:10:23
  • 详解Python多线程Selenium跨浏览器测试

    2023-05-05 08:10:11
  • 基于django ManyToMany 使用的注意事项详解

    2022-10-16 00:29:33
  • 让ThinkPHP支持大小写url地址访问的方法

    2023-11-17 02:28:37
  • JS获取对象代码总结

    2011-03-07 16:14:00
  • python中的None与NULL用法说明

    2022-01-01 13:59:15
  • JavaScript控制flash操作 兼容IE FF[译]

    2009-11-29 16:28:00
  • js 浏览器版本及版本号判断函数2009年

    2024-04-28 09:40:54
  • vux-scroller实现移动端上拉加载功能过程解析

    2024-05-09 10:42:21
  • 详解Python pygame安装过程笔记

    2023-07-24 12:20:36
  • asp之家 网络编程 m.aspxhome.com