扫一扫 解析二维码 的多种解决方案

一、微信扫一扫
提到扫一扫,大家都很熟悉微信扫一扫吧,那微信扫一扫怎么接入的呢?当然这个肯定是看微信开放平台开放的api接口咯!
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115, 诺这就是官方的说法!
step1: 你的要有一个微信公众号
step2: 在微信公众号后台的公众号设置里面设置js安全域名为当前调用的域名
在这里插入图片描述
step3:引入微信官方提供的js
http://res.wx.qq.com/open/js/jweixin-1.4.0.js
在这里插入图片描述
step4:通过config接口注入权限验证配置
所有需要使用JS-SDK的页面必须先注入配置信息,否则将无法调用(同一个url仅需调用一次,对于变化url的SPA的web app可在每次url变化时进行调用,目前Android微信客户端不支持pushState的H5新特性,所以使用pushState来实现web app的页面会导致签名失败,此问题会在Android6.2中修复)。

wx.config({
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: ‘’, // 必填,公众号的唯一标识
timestamp: , // 必填,生成签名的时间戳
nonceStr: ‘’, // 必填,生成签名的随机串
signature: ‘’,// 必填,签名
jsApiList: [‘scanQRCode’] // 必填,需要使用的JS接口列表
});
step5:在页面上用js掉起微信扫一扫
在这里插入图片描述

wx.scanQRCode({
needResult: 0, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码,默认二者都有
success: function (res) {
var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
}
});

这样子微信扫一扫就大功告成了,好像有点麻烦的地方就是签名那里,小编这里调用get_sha1方法获取微信返回来权限接口验证
参数,这三个方法互相调用,需要传递的就是申请公众号的appid 和appsecret, 最后就可以返回接口验证权限的那些参数:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
二、接入支付宝扫一扫
官方开放接口文档地址:
https://myjsapi.alipay.com/alipayjsapi/device/scan/scan.html

<script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.inc.min.js"></script>

<button id="J_btn_scanQR" class="btn btn-default">扫一扫二维码</button>
<button id="J_btn_scanBAR" class="btn btn-default">扫一扫条形码</button>
<script>
  var btnScanQR = document.querySelector('#J_btn_scanQR');
  var btnScanBAR = document.querySelector('#J_btn_scanBAR');
  btnScanQR.addEventListener('click', function(){
    ap.scan(function(res){
      ap.alert(res.code);
    });
  });
  btnScanBAR.addEventListener('click', function(){
    ap.scan({
      type: 'bar'
    }, function(res){
      ap.alert(res.code);
    });
  });
</script>

三:h5扫码拍照解析
测试demo下载链接,纯js实现,唯一的缺点就是容错率不是很大,二维码图片包含logo识别不了
https://pan.baidu.com/s/1rPkseXVc0Wj3U5RHneDpeg 提取码:bg9n

<div>
    <div class="qr-btn" node-type="qr-btn">扫一扫
        <input node-type="jsbridge" type="file" name="myPhoto" value="扫描二维码" id="targetImgCamera" capture="camera" accept="image/*"/>
    </div>
</div>
<script src="/public/static/lock/js/zepto.js"></script>
<script src="/public/static/lock/js/qrcode.lib.min.js"></script>
<script src="/public/static/lock/js/qrcode.js"></script>
<script type="text/javascript">
       window.onload=function(){
            Qrcode.init($('[node-type=qr-btn]'));
       }
 </script>

核心代码就这几行,调用qrcode.js就可以实现

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解析多种二维码,你需要使用多个二维码解析库,每个库都可以解析不同类型的二维码。以下是一个基于Java的解析多种二维码的示例代码: ```java import com.google.zxing.*; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.QRCodeReader; import net.sourceforge.zbar.*; import org.bytedeco.javacpp.*; import org.bytedeco.javacpp.lept.PIX; import org.bytedeco.javacpp.tesseract.TessBaseAPI; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.util.Arrays; import java.util.EnumMap; import java.util.List; import java.util.Map; public class MultiQRCodeReader { public static String readQRCode(String filePath) throws Exception { File file = new File(filePath); BufferedImage image = ImageIO.read(file); // 使用zxing解析二维码 String zxingResult = readQRCodeByZxing(image); if (zxingResult != null) { return zxingResult; } // 使用zbar解析二维码 String zbarResult = readQRCodeByZbar(image); if (zbarResult != null) { return zbarResult; } // 使用tesseract-ocr解析二维码 String ocrResult = readQRCodeByOcr(image); if (ocrResult != null) { return ocrResult; } // 无法解析 return null; } private static String readQRCodeByZxing(BufferedImage image) throws NotFoundException { LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result = new QRCodeReader().decode(bitmap); return result.getText(); } private static String readQRCodeByZbar(BufferedImage image) throws Exception { ImageScanner scanner = new ImageScanner(); scanner.setConfig(0, Config.ENABLE, 1); int width = image.getWidth(); int height = image.getHeight(); byte[] imageData = ((java.awt.image.DataBufferByte) image.getRaster().getDataBuffer()).getData(); Image barcode = new Image(width, height, "Y800"); barcode.setData(imageData); scanner.scanImage(barcode); SymbolSet symbolSet = scanner.getResults(); for (Symbol symbol : symbolSet) { if (symbol.getType() == Symbol.QRCODE) { return symbol.getData(); } } return null; } private static String readQRCodeByOcr(BufferedImage image) throws Exception { BytePointer outText; PIX pix = Leptonica1.pixReadMem( PointerPointer.toBytes(imageToArray(image)), image.getWidth(), image.getHeight(), image.getColorModel().getPixelSize() / 8, image.getWidth() * image.getColorModel().getPixelSize() / 8, 1); TessBaseAPI api = new TessBaseAPI(); api.Init(null, "eng"); api.SetImage(pix); outText = api.GetUTF8Text(); String result = outText.getString(); api.End(); outText.deallocate(); pix.deallocate(); return result; } private static byte[] imageToArray(BufferedImage image) { int width = image.getWidth(); int height = image.getHeight(); int type = image.getType(); byte[] data = new byte[width * height * (type == BufferedImage.TYPE_BYTE_GRAY ? 1 : 3)]; int k = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int rgb = image.getRGB(j, i); if (type == BufferedImage.TYPE_BYTE_GRAY) { data[k++] = (byte) ((rgb >> 16) & 0xff); } else { data[k++] = (byte) ((rgb >> 16) & 0xff); data[k++] = (byte) ((rgb >> 8) & 0xff); data[k++] = (byte) (rgb & 0xff); } } } return data; } public static void main(String[] args) throws Exception { String filePath = "example.png"; String result = readQRCode(filePath); System.out.println("QR Code content: " + result); } } ``` 上述代码使用了三个二维码解析库:zxing、zbar和tesseract-ocr。当无法使用一个库解析时,就切换到下一个库进行解析,直到解析成功或所有库都无法解析。你可以根据自己的需求,调整代码中使用的库和解析顺序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值