qrcode.react和jquery.qrcode生成二维码
qrcode.react
1.安装
npm install qrcode.react
2.用法(这里用的ant design)
import React from 'react';
import QRCode from 'qrcode.react';
import {Icon,Button,Form,Row,Col,Input,InputNumber,Select} from 'antd';
const Option = Select.Option;
const FormItem = Form.Item;
const formItemLayout = {
labelCol: { span: 12 },
wrapperCol: { span: 12 },
};
const textLayout = {
labelCol: { span: 2 },
wrapperCol: { span: 22 },
};
const levelLayout = {
labelCol: { span: 5 },
wrapperCol: { span: 19 },
};
function qrcode({
size,
value,
level,
bgColor,
fgColor,
changeSize,
changeBgColor,
changeFgColor,
changeLevel,
changeValue
}){
return (
<div>
<Form>
<Row>
<Col span={4}>
<FormItem {...formItemLayout} label="Size宽高(px)">
<InputNumber onChange={changeSize} defaultValue={128}/>
</FormItem>
</Col>
<Col span={6}>
<FormItem {...formItemLayout} label="Background Color">
<Input type="color" onChange={changeBgColor} value={bgColor}/>
</FormItem>
</Col>
<Col span={6}>
<FormItem {...formItemLayout} label="Foreground Color">
<Input type="color" onChange={changeFgColor} value={fgColor}/>
</FormItem>
</Col>
<Col span={6}>
<FormItem {...levelLayout} label="Level">
<Select value={level} onChange={changeLevel}>
<Option value="L">L</Option>
<Option value="M">M</Option>
<Option value="Q">Q</Option>
<Option value="H">H</Option>
</Select>
</FormItem>
</Col>
<Col span={2}></Col>
</Row>
<Row>
<Col span={16}>
<FormItem {...textLayout} label="Value">
<Input type="textarea" onChange={changeValue} value={value}/>
</FormItem>
</Col>
<Col span={8}></Col>
</Row>
</Form>
<QRCode value={value} size={size} bgColor={bgColor} fgColor={fgColor} level={level}/>
</div>)
}
export default Form.create()(qrcode);
3.参数
prop type default value
value string
size number 128
bgColor string "#FFFFFF"
fgColor string "#000000"
level string 'L'
jquery.qrcode
1.引入js
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
2.在页面中需要显示二维码的地方加入以下代码:
<div id="code"></div>
text : "https://github.com/jeromeetienne/jquery-qrcode" //设置二维码内容
render : "canvas",//设置渲染方式
width : 256, //设置宽度
height : 256, //设置高度
typeNumber : -1, //计算模式
correctLevel : QRErrorCorrectLevel.H,//纠错等级
background : "#ffffff",//背景颜色
foreground : "#000000" //前景颜色
$('#code').qrcode("http://www.cnblogs.com/cosyer/"); //任意字符串
或者
$("#code").qrcode({参数});
使用canvas方式渲染性能还是非常不错的,但是如果用table方式,性能不太理想,特别是IE9以下的浏览器,所以需要自行优化一下渲染table的方式。
3.解决不支持中文
这个库默认不支持中文。这跟js的机制有关系,jquery-qrcode这个库是采用 charCodeAt() 这个方式进行编码转换的,而这个方法默认会获取它的 Unicode 编码,一般的解码器都是采用UTF-8, ISO-8859-1等方式,英文是没有问题,如果是中文,一般情况下Unicode是UTF-16实现,长度2位,而UTF-8编码是3位,这样二维码的编解码就不匹配了。解决方式当然是,在二维码编码前把字符串转换成UTF-8,具体代码如下:
function utf16to8(str) {
var out, i, len, c;
out = "";
len = str.length;
for(i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += str.charAt(i);
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}
qrcode.react和jquery.qrcode生成二维码的更多相关文章
- QRCode.js:使用 JavaScript 生成二维码
什么是 QRCode.js? QRCode.js 是一个用于生成二维码的 JavaScript 库.主要是通过获取 DOM 的标签,再通过 HTML5 Canvas 绘制而成,不依赖任何库. 基本用法 ...
- jquery动态生成二维码添加自定义logo
动态生成二维码中间带logo. jquery.qrcode.js 动态生成二维码api很简单. 引入jquer(版本任意),引入jquery.qrcode.js 不需要中间带logo这样就可以了.带l ...
- jQuery前端生成二维码
引用: <script src="assets/js/jquery.qrcode.min.js" charset="UTF-8"></scri ...
- jquery---利用jquery插件生成二维码
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL;?>/js/jquer ...
- MVC3学习:利用jquery+ajax生成二维码(支持中文)
二维码越来越热火了,做电子商务网站,不做二维码,你就OUT了. 一.下载DLL文件(ThoughtWorks.QRCode.dll),并在项目中引用.点此下载 如果你还不知道什么是QRCode二维码, ...
- jquery自动生成二维码
把下面的jquery代码放到想要在当前页面上面生成二维码: 代码如下: <script type="text/javascript">var _qrContent='' ...
- jQuery 自动生成二维码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http ...
- Jquery.Qrcode在客户端动态生成二维码并添加自定义Logo
0 Jquery.Qrcode简介 Jquery.Qrcode.js是一个在浏览器端基于Jquery动态生成二维码的插件,支持Canvas和Table两种渲染方式,它的优点是在客户端动态生成,减轻了服 ...
- 使用jquery.qrcode生成二维码实现微信分享功能
前言: 最近有个这样的需求,在pc端的商品详情页增加分享功能. 微博分享.QQ好友分享.QQ空间分享这些都很常见.但是微信分享我还没有手动写过(以前改过). 最终效果如下图: 解决方案:使用jquer ...
- qrCode生成二维码图片
QRCode.js 是一个用于生成二维码图片的插件. 1.文件脚本 var QRCode;!function(){function a(a){this.mode=c.MODE_8BIT_BYTE,th ...
随机推荐
- 【HDU 5934】Bomb(强连通缩点)
Problem Description There are N bombs needing exploding. Each bomb has three attributes: exploding r ...
- 【HDU 1402】A * B Problem Plus(FFT)
Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to e ...
- Apache手册
一.apache的安装 如果不指定安装位置,默认为/usr/local/apache2/
- 零、常用的Mysql数据库操作语句大全
零.用户管理: 1.新建用户: >CREATE USER name IDENTIFIED BY 'ssapdrow'; 2.更改密码: >SET PASSWORD FOR name=PAS ...
- iOS时钟,秒针扫秒样式
昨天做一个时钟小demo,发现了一些问题. 描述能力有限,我封装好了一个时钟框架,朋友们可以参考 https://github.com/qianlishun/ClockView 点击这里可以 ...
- scoi2018游记
day1: t1点分树 冬令营上jry讲过原题,t2启发式合并+解二次同余方程 预计100+100+0 结果t1卡内存,t2模数太大.导致调试到没有写t3 最后t1 85 t2 15 要是我会o1快速 ...
- [Poj1185][Noi2001]炮兵阵地(状压dp)
炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29476 Accepted: 11411 Descriptio ...
- jree-创建普通折线图
对于maven工程,需要引入依赖:在pom.xml中,添加如下内容 <dependency> <groupId>jfree</groupId> <artifa ...
- rror Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnec
在mysql5中,可以设置safe mode,比如在一个更新语句中 UPDATE table_name SET bDeleted=0; 执行时会错误,报: You are using safe upd ...
- 【Nginx】基本数据结构
整型的封装 typedef intptr_t ngx_int _t;//有符号整型 typedef uintptr_t ngx_uint_t;//无符号整型 字符串的封装 typedef struct ...