Home > Web Front-end > JS Tutorial > jQuery implements WeChat long press recognition QR code function

jQuery implements WeChat long press recognition QR code function

高洛峰
Release: 2017-01-20 15:02:27
Original
2305 people have browsed it

I have been working on a WeChat development project recently, and it is finally almost completed. Today I took some time to record the solution to the problem that the QR code generated by jquery.qrcode.min.js was not recognized when long pressed during the project development process. Method, hope it helps everyone!

1.Introduce JS library

<script src="jquery-1.8.3.js" type="text/javascript" charset="utf-8"></script>
<script src="jquery.qrcode.min.js" type="text/javascript" charset="utf-8"></script>
Copy after login

2.Create an empty div on the page;

<div id="qrDiv"></div>
Copy after login

3.Generate QR code

$("#qrDiv").qrcode({
width: 120, //宽度
height:120, //高度
text: "需要生成的二维码内容" //任意内容
});
Copy after login

Note: The QR code generated at this time has no response in Changan in WeChat, because the qrcode generates canvas tags instead of img tags

4. Convert canvas tag to img tag

//从 canvas 提取图片 image
function convertCanvasToImage(canvas) {
//新Image对象,可以理解为DOM
var image = new Image();
// canvas.toDataURL 返回的是一串Base64编码的URL,当然,浏览器自己肯定支持
// 指定格式 PNG
image.src = canvas.toDataURL("image/png");
return image;
}
//获取网页中的canvas对象
var mycanvas1=document.getElementsByTagName(&#39;canvas&#39;)[0];
//将转换后的img标签插入到html中
var img=convertCanvasToImage(mycanvas1);
$(&#39;#imagQrDiv&#39;).append(img);//imagQrDiv表示你要插入的容器id
Copy after login

Note: After completing the above steps, you can long press and identify it in WeChat

The above is given by the editor The jQuery introduced by you implements the function of long-press recognition of QR codes on WeChat. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to jQuery’s implementation of WeChat’s long press recognition QR code function, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template