当前位置:   article > 正文

js实现二维码在线解码的方法_二维码解码

二维码解码

js实现二维码在线解码的方法,js实现二维码在线解码代码教程如下:

选择一张二维码图片

计算结果:解析出当前二维码的网址

js实现代码:

  1. $(function () {
  2. document.getElementById("uploadBtnd").onchange = function (e) {
  3. var canvas = $('#decode-canvas')[0],
  4. ctx = canvas.getContext('2d'),
  5. file = e.target.files[0],
  6. reader = new FileReader();
  7. reader.onload = (function (e) {
  8. var img = new Image();
  9. img.onload = function () {
  10. canvas.width = img.width;
  11. canvas.height = img.height;
  12. ctx.drawImage(img, 0, 0);
  13. };
  14. img.src = e.target.result;
  15. });
  16. file && reader.readAsDataURL(file);
  17. };
  18. });
  19. function renderLogo(canvas, url) {
  20. var img = new Image(),
  21. ctx = canvas.getContext('2d'),
  22. canvasSize = 200;
  23. if (!url) {
  24. ctx.fillStyle = 'rgb(255, 255, 255)';
  25. ctx.fillRect(0, 0, canvasSize, canvasSize);
  26. return;
  27. }
  28. img.onload = function () {
  29. var imgSize, zoom,
  30. imgW = img.width, imgH = img.height;
  31. canvas.width = canvasSize;
  32. canvas.height = canvasSize;
  33. ctx.fillStyle = 'rgb(255, 255, 255)';
  34. ctx.fillRect(0, 0, canvasSize, canvasSize);
  35. imgSize = Math.max(imgW, imgH);
  36. if (imgSize > canvasSize) {
  37. zoom = canvasSize / imgSize;
  38. imgW = imgW * zoom;
  39. imgH = imgH * zoom;
  40. }
  41. ctx.drawImage(img, (canvasSize - imgW) / 2, (canvasSize - imgH) / 2, imgW, imgH);
  42. };
  43. img.src = url;
  44. }
  45. $('#decode-btn').on('click', function () {
  46. result = $('#decode-canvas').QRDecode(function (e) {
  47. $("#decoderes").val(e.message);
  48. throw e;
  49. });
  50. if (result) {
  51. $("#decoderes").val(result);
  52. }
  53. });
  54. function empty() {
  55. $("#decoderes").val("");
  56. $("#uploadBtnd").val("");
  57. }

显示效果:二维码解码器

参考文章:http://www.itmacom.com/gl/33457.html

其他类似js实现在线工具

favicon制作

网站历史快照查询

在线车牌号查询

html转js在线工具

robots文件在线生成

上海个税计算器

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/575992
推荐阅读
相关标签
  

闽ICP备14008679号