小程序剖析二维码:jsQR

打印 上一主题 下一主题

主题 1761|帖子 1761|积分 5283

1.相识jsQR

jsQR是一个纯javascript脚本实现的二维码辨认库,不仅可以在欣赏器端利用,而且支持后端node.js环境。jsQR利用较为简朴,有着不错的辨认率。
2.效果图


3.二维码


4.下载jsqr包

  1. npm i -d jsqr
复制代码
5.代码

  1. <!-- index.wxml -->
  2. <view class="container">
  3.   <button bindtap="chooseImage">选择图片识别二维码</button>
  4.   <canvas id="qrcodeCanvas" canvas-id="qrcodeCanvas" style="width: {{canvasWidth}}px; height: {{canvasHeight}}px"></canvas>
  5. </view>
  6. <button bind:tap="process">识别</button>
  7. <button style="width: 100vw; margin-top: 20rpx;">识别结果:{{msg}}</button>
复制代码


  • 旧canvas【canvas 2d 下】
  1. // index.js
  2. import jsQR from "jsqr";
  3. Page({
  4.   data: {
  5.     msg: "",
  6.     canvasWidth: 0,
  7.     canvasHeight: 0,
  8.   },
  9.   chooseImage() {
  10.     wx.chooseMedia({
  11.       count: 1,
  12.       mediaType: ["image"],
  13.       sourceType: ["album", "camera"],
  14.       success: (res) => {
  15.         this.decodeQRCode(res.tempFiles[0].tempFilePath);
  16.       },
  17.       fail: (err) => {
  18.         console.error("选择图片失败", err);
  19.       },
  20.     });
  21.   },
  22.   decodeQRCode(imagePath) {
  23.     wx.getImageInfo({
  24.       src: imagePath,
  25.       success: (imageInfo) => {
  26.         this.setData({
  27.           canvasWidth: imageInfo.width,
  28.           canvasHeight: imageInfo.height,
  29.         });
  30.         const canvasId = "qrcodeCanvas";
  31.         const ctx = wx.createCanvasContext(canvasId);
  32.         ctx.drawImage(imagePath, 0, 0, imageInfo.width, imageInfo.height);
  33.         ctx.draw();
  34.       },
  35.       fail: (err) => {
  36.         console.error("获取图片信息失败", err);
  37.       },
  38.     });
  39.   },
  40.   process() {
  41.     wx.canvasGetImageData({
  42.       canvasId: "qrcodeCanvas",
  43.       x: 0,
  44.       y: 0,
  45.       width: this.data.canvasWidth,
  46.       height: this.data.canvasHeight,
  47.       success: (res) => {
  48.         console.log(res);
  49.         const decodedResult = jsQR(
  50.           res.data,
  51.           this.data.canvasWidth,
  52.           this.data.canvasHeight,
  53.           {
  54.             inversionAttempts: "dontInvert",
  55.           }
  56.         );
  57.         console.log("结果", decodedResult);
  58.         if (decodedResult) {
  59.           console.log(decodedResult.data); // 识别结果
  60.           this.setData({
  61.             msg: decodedResult.data,
  62.           });
  63.         } else {
  64.           wx.showToast({
  65.             icon: "none",
  66.             title: "未识别到二维码!",
  67.           });
  68.         }
  69.       },
  70.       fail: (err) => {
  71.         console.error("获取 Canvas 像素数据失败", err);
  72.       },
  73.     });
  74.   },
  75. });
复制代码


  • Canvas 2d
  1. // index.js
  2. import jsQR from "jsqr";
  3. Page({
  4.   data: {
  5.     msg: "",
  6.     canvasWidth: 400,
  7.     canvasHeight: 400,
  8.   },
  9.   chooseImage() {
  10.     wx.chooseMedia({
  11.       count: 1,
  12.       mediaType: ["image"],
  13.       sourceType: ["album", "camera"],
  14.       success: (res) => {
  15.         this.decodeQRCode(res.tempFiles[0].tempFilePath);
  16.       },
  17.       fail: (err) => {
  18.         console.error("选择图片失败", err);
  19.       },
  20.     });
  21.   },
  22.   decodeQRCode(imagePath) {
  23.     wx.createSelectorQuery()
  24.       .select("#qrcodeCanvas") // 在 WXML 中填入的 id
  25.       .fields({ node: true, size: true })
  26.       .exec((res) => {
  27.         // Canvas 对象
  28.         this.canvas = res[0].node;
  29.         const renderWidth = res[0].width;
  30.         const renderHeight = res[0].height;
  31.         this.ctx = this.canvas.getContext("2d");
  32.         // 初始化画布大小
  33.         const dpr = wx.getWindowInfo().pixelRatio;
  34.         this.canvas.width = renderWidth * dpr;
  35.         this.canvas.height = renderHeight * dpr;
  36.         this.ctx.scale(dpr, dpr);
  37.         const image = this.canvas.createImage();
  38.         image.onload = () => {
  39.           this.ctx.drawImage(
  40.             image,
  41.             0,
  42.             0,
  43.             this.data.canvasWidth,
  44.             this.data.canvasHeight
  45.           );
  46.           this.process();
  47.         };
  48.         image.src = imagePath;
  49.       });
  50.   },
  51.   process() {
  52.     var imgData = this.ctx.getImageData(
  53.       0,
  54.       0,
  55.       this.canvas.width,
  56.       this.canvas.height
  57.     );
  58.     const decodedResult = jsQR(
  59.       imgData.data,
  60.       this.canvas.width,
  61.       this.canvas.height,
  62.       {
  63.         inversionAttempts: "dontInvert",
  64.       }
  65.     );
  66.     if (decodedResult) {
  67.       console.log(decodedResult.data); // 识别结果
  68.       this.setData({
  69.         msg: decodedResult.data,
  70.       });
  71.     } else {
  72.       wx.showToast({
  73.         icon: "none",
  74.         title: "未识别到二维码!",
  75.       });
  76.     }
  77.   },
  78. });
复制代码
  仅辨认优劣类二维码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天津储鑫盛钢材现货供应商

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表