javaScript生成二维码(支持中文,生成logo)
资料搜索
选择star最多的两个
第一个就是用的比较多的jquery.qrcode.js(但不支持中文,不能带logo)啦,第二个支持ie6+,支持中文,根据第二个源代码,使得,jquery.qrcode.js,支持中文。
支持中文
- //qrcode.js
- function QR8bitByte(data) {
- this.mode = QRMode.MODE_8BIT_BYTE;
- this.data = data;
- }
- QR8bitByte.prototype = {
- getLength : function(buffer) {
- return this.data.length;
- },
- write : function(buffer) {
- for (var i = 0; i < this.data.length; i++) {
- // not JIS ...
- buffer.put(this.data.charCodeAt(i), 8);
- }
- }
- };
修改如下(就是复制粘贴了第二份代码的头部):
- function QR8bitByte(data) {
- this.mode = QRMode.MODE_8BIT_BYTE;
- this.data = data;
- this.parsedData = [];
- // Added to support UTF-8 Characters
- for (var i = 0, l = this.data.length; i < l; i++) {
- var byteArray = [];
- var code = this.data.charCodeAt(i);
- if (code > 0x10000) {
- byteArray[0] = 0xF0 | ((code & 0x1C0000) >>> 18);
- byteArray[1] = 0x80 | ((code & 0x3F000) >>> 12);
- byteArray[2] = 0x80 | ((code & 0xFC0) >>> 6);
- byteArray[3] = 0x80 | (code & 0x3F);
- } else if (code > 0x800) {
- byteArray[0] = 0xE0 | ((code & 0xF000) >>> 12);
- byteArray[1] = 0x80 | ((code & 0xFC0) >>> 6);
- byteArray[2] = 0x80 | (code & 0x3F);
- } else if (code > 0x80) {
- byteArray[0] = 0xC0 | ((code & 0x7C0) >>> 6);
- byteArray[1] = 0x80 | (code & 0x3F);
- } else {
- byteArray[0] = code;
- }
- this.parsedData.push(byteArray);
- }
- this.parsedData = Array.prototype.concat.apply([], this.parsedData);
- if (this.parsedData.length != this.data.length) {
- this.parsedData.unshift(191);
- this.parsedData.unshift(187);
- this.parsedData.unshift(239);
- }
- }
- QR8bitByte.prototype = {
- getLength: function (buffer) {
- return this.parsedData.length;
- },
- write: function (buffer) {
- for (var i = 0, l = this.parsedData.length; i < l; i++) {
- buffer.put(this.parsedData[i], 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;
- }
支持自定义logo
- 修改jquery.qrcode.js,createCanvas函数
- var createCanvas = function(){
- // create the qrcode itself
- var qrcode = new QRCode(options.typeNumber, options.correctLevel);
- qrcode.addData(options.text);
- qrcode.make();
- // create canvas element
- var canvas = document.createElement('canvas');
- canvas.width = options.width;
- canvas.height = options.height;
- var ctx = canvas.getContext('2d');
- //增加以下代码,把图片画出来
- if( options.src ) {//传进来的图片地址
- //图片大小
- options.imgWidth = options.imgWidth || options.width / 4.7;
- options.imgHeight = options.imgHeight || options.height / 4.7;
- var img = new Image();
- img.src = options.src;
- //不放在onload里,图片出不来
- img.onload = function () {
- ctx.drawImage(img, (options.width - options.imgWidth) / 2, (options.height - options.imgHeight) / 2, options.imgWidth, options.imgHeight);
- }
- }
- // compute tileW/tileH based on options.width/options.height
- var tileW = options.width / qrcode.getModuleCount();
- var tileH = options.height / qrcode.getModuleCount();
- // draw in the canvas
- for( var row = 0; row < qrcode.getModuleCount(); row++ ){
- for( var col = 0; col < qrcode.getModuleCount(); col++ ){
- ctx.fillStyle = qrcode.isDark(row, col) ? options.foreground : options.background;
- var w = (Math.ceil((col+1)*tileW) - Math.floor(col*tileW));
- var h = (Math.ceil((row+1)*tileW) - Math.floor(row*tileW));
- ctx.fillRect(Math.round(col*tileW),Math.round(row*tileH), w, h);
- }
- }
- // return just built canvas
- return canvas;
- };
- 修改jquery.qrcode.js,createTable函数(不支持canvas用table画二维码)
- var createTable = function(){
- // create the qrcode itself
- var qrcode = new QRCode(options.typeNumber, options.correctLevel);
- qrcode.addData(options.text);
- qrcode.make();
- // create table element
- var $table = $('<table></table>')
- .css("width", options.width+"px")
- .css("height", options.height+"px")
- .css("border", "0px")
- .css("border-collapse", "collapse")
- .css('background-color', options.background);
- // compute tileS percentage
- var tileW = options.width / qrcode.getModuleCount();
- var tileH = options.height / qrcode.getModuleCount();
- // draw in the table
- for(var row = 0; row < qrcode.getModuleCount(); row++ ){
- var $row = $('<tr></tr>').css('height', tileH+"px").appendTo($table);
- for(var col = 0; col < qrcode.getModuleCount(); col++ ){
- $('<td></td>')
- .css('width', tileW+"px")
- .css('background-color', qrcode.isDark(row, col) ? options.foreground : options.background)
- .appendTo($row);
- }
- }
- //主要思想,把table,和img标签放在同一个div下,div relative定位,然后使得图片absolute定位在table中间
- if( options.src ) {
- options.imgWidth = options.imgWidth || options.width / 4.7;
- options.imgHeight = options.imgHeight || options.height / 4.7;
- var $img = $('<img>').attr("src", options.src)
- .css("width", options.imgWidth)
- .css("height", options.imgHeight)
- .css("position", "absolute")
- .css("left", (options.width - options.imgWidth) / 2)
- .css("top", (options.height - options.imgHeight) / 2);
- $table = $('<div style="position:relative;"></div>')
- .append($table)
- .append($img);
- }
- // return just built canvas
- return $table;
- };
- 对IE做特殊判断,大家懂的
- //判断是否IE, IE8以下,用 table,否则用 canvas
- var isIE = function() {
- var b = document.createElement('b');
- b.innerHTML = '<!--[if IE]><i></i><![endif]-->';
- return b.getElementsByTagName('i').length === 1;
- };
- options.render = options.render ||
- (isIE(6) || isIE(7) || isIE(8))? "table": "canvas";
- 改过后的jquery.qrcode.js如下:
- (function( $ ){
- $.fn.qrcode = function(options) {
- // if options is string,
- if( typeof options === 'string' ){
- options = { text: options };
- }
- //判断是否IE, IE8以下,用 table,否则用 canvas
- var isIE = function() {
- var b = document.createElement('b');
- b.innerHTML = '<!--[if IE]><i></i><![endif]-->';
- return b.getElementsByTagName('i').length === 1;
- };
- options.render = options.render ||
- (isIE(6) || isIE(7) || isIE(8))? "table": "canvas";
- // set default values
- // typeNumber < 1 for automatic calculation
- options = $.extend( {}, {
- // render : "canvas",
- width : 256,
- height : 256,
- typeNumber : -1,
- correctLevel : QRErrorCorrectLevel.H,
- background : "#ffffff",
- foreground : "#000000"
- }, options);
- var createCanvas = function(){
- // create the qrcode itself
- var qrcode = new QRCode(options.typeNumber, options.correctLevel);
- qrcode.addData(options.text);
- qrcode.make();
- // create canvas element
- var canvas = document.createElement('canvas');
- canvas.width = options.width;
- canvas.height = options.height;
- var ctx = canvas.getContext('2d');
- //在中间画logo
- if( options.src ) {
- options.imgWidth = options.imgWidth || options.width / 4.7;
- options.imgHeight = options.imgHeight || options.height / 4.7;
- var img = new Image();
- img.src = options.src;
- img.onload = function () {
- ctx.drawImage(img, (options.width - options.imgWidth) / 2, (options.height - options.imgHeight) / 2, options.imgWidth, options.imgHeight);
- }
- }
- // compute tileW/tileH based on options.width/options.height
- var tileW = options.width / qrcode.getModuleCount();
- var tileH = options.height / qrcode.getModuleCount();
- // draw in the canvas
- for( var row = 0; row < qrcode.getModuleCount(); row++ ){
- for( var col = 0; col < qrcode.getModuleCount(); col++ ){
- ctx.fillStyle = qrcode.isDark(row, col) ? options.foreground : options.background;
- var w = (Math.ceil((col+1)*tileW) - Math.floor(col*tileW));
- var h = (Math.ceil((row+1)*tileW) - Math.floor(row*tileW));
- ctx.fillRect(Math.round(col*tileW),Math.round(row*tileH), w, h);
- }
- }
- // return just built canvas
- return canvas;
- };
- // from Jon-Carlos Rivera (https://github.com/imbcmdth)
- var createTable = function(){
- // create the qrcode itself
- var qrcode = new QRCode(options.typeNumber, options.correctLevel);
- qrcode.addData(options.text);
- qrcode.make();
- // create table element
- var $table = $('<table></table>')
- .css("width", options.width+"px")
- .css("height", options.height+"px")
- .css("border", "0px")
- .css("border-collapse", "collapse")
- .css('background-color', options.background);
- // compute tileS percentage
- var tileW = options.width / qrcode.getModuleCount();
- var tileH = options.height / qrcode.getModuleCount();
- // draw in the table
- for(var row = 0; row < qrcode.getModuleCount(); row++ ){
- var $row = $('<tr></tr>').css('height', tileH+"px").appendTo($table);
- for(var col = 0; col < qrcode.getModuleCount(); col++ ){
- $('<td></td>')
- .css('width', tileW+"px")
- .css('background-color', qrcode.isDark(row, col) ? options.foreground : options.background)
- .appendTo($row);
- }
- }
- //生成logo
- if( options.src ) {
- options.imgWidth = options.imgWidth || options.width / 4.7;
- options.imgHeight = options.imgHeight || options.height / 4.7;
- var $img = $('<img>').attr("src", options.src)
- .css("width", options.imgWidth)
- .css("height", options.imgHeight)
- .css("position", "absolute")
- .css("left", (options.width - options.imgWidth) / 2)
- .css("top", (options.height - options.imgHeight) / 2);
- $table = $('<div style="position:relative;"></div>')
- .append($table)
- .append($img);
- }
- // return just built canvas
- return $table;
- };
- return this.each(function(){
- var element = options.render == "canvas" ? createCanvas() : createTable();
- $(element).appendTo(this);
- });
- };
- })( jQuery );
- 测试
- jQuery('#qrcodeTable').qrcode({
- render : "table",
- text : "中文://jetienne.com",
- src: './logo32.png'
- });
jQuery('#qrcodeCanvas').qrcode({- text : "中午你://jetienne.com",
- src: './logo32.png'
- });
javaScript生成二维码(支持中文,生成logo)的更多相关文章
- 使用jquery-qrcode在页面上生成二维码,支持中文
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C#Qrcode生成二维码支持中文,带图片,带文字
C#Qrcode生成二维码支持中文带图片的操作请看二楼的帖子,当然开始需要下载一下C#Qrcode的源码 下载地址 : http://www.codeproject.com/Articles/2057 ...
- C# Qrcode生成二维码支持中文,带图片,带文字 2015-01-22 15:11 616人阅读 评论(1) 收藏
1.下载Qrcode库源码,下载地址:http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library 2.打开源码时,部分类库 ...
- Qrcode生成二维码支持中文,带图片,带文字
1.下载Qrcode库源码, 下载地址:http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library2.打开源码时, 部分类 ...
- jquery.qrcode生成二维码支持中文
基本使用方法: 1.首先在页面中加入jquery库文件和qrcode插件. <script type="text/javascript" src="jquery.j ...
- 使用jquery.qrcode生成二维码支持logo,和中文
/* utf.js - UTF-8 <=> UTF-16 convertion * * Copyright (C) 1999 Masanao Izumo <iz@onicos.co. ...
- spring boot:用zxing生成二维码,支持logo(spring boot 2.3.2)
一,zxing是什么? 1,zxing的用途 如果我们做二维码的生成和扫描,通常会用到zxing这个库, ZXing是一个开源的,用Java实现的多种格式的1D/2D条码图像处理库. zxing还可以 ...
- 使用PHP生成二维码支持自定义logo
require_once 'phpqrcode/phpqrcode.php'; //引入类库 $text = "https://www.baidu.com/";//要生成二维码的文 ...
- jquery.qrcode.js生成二维码(前端生成二维码)
官网地址:http://jeromeetienne.github.io/jquery-qrcode/ 第一步引入插件: <script type='text/javascript' src='h ...
- QRCode生成二维码,jq QRCode生成二维码,QRCode生成电子名片
[QRCode官网]http://phpqrcode.sourceforge.net/ PHP QRCode生成二维码 官网下载QRCode源码包,引入源码包中的 qrlib.php . <?p ...
随机推荐
- B树——算法导论(25)
B树 1. 简介 在之前我们学习了红黑树,今天再学习一种树--B树.它与红黑树有许多类似的地方,比如都是平衡搜索树,但它们在功能和结构上却有较大的差别. 从功能上看,B树是为磁盘或其他存储设备设计的, ...
- SQL Server-表表达式基础回顾(二十四)
前言 从这一节开始我们开始进入表表达式章节的学习,Microsoft SQL Server支持4种类型的表表达式:派生表.公用表表达式(CTE).视图.内嵌表值函数(TVF).简短的内容,深入的理解, ...
- 【开源】.Net Aop(静态织入)框架 BSF.Aop
BSF.Aop .Net 免费开源,静态Aop织入(直接修改IL中间语言)框架,类似PostSharp(收费): 实现前后Aop切面和INotifyPropertyChanged注入方式. 开源地址: ...
- .net erp(办公oa)开发平台架构概要说明之表单设计器
背景:搭建一个适合公司erp业务的开发平台. 架构概要图: 表单设计开发部署示例图 表单设计开发部署示例说明1)每个开发人员可以自己部署表单设计至本地一份(当然也可以共用一套开发环境,但是如 ...
- CSS 3学习——文本效果和@font-face
文本效果 关于文本效果,这里仅仅记录得到大多数浏览器支持的几个属性,分别是: text-overflow text-shadow word-break word-wrap text-overflow ...
- 通过自定义特性,使用EF6拦截器完成创建人、创建时间、更新人、更新时间的统一赋值(使用数据库服务器时间赋值,接上一篇)
目录: 前言 设计(完成扩展) 实现效果 扩展设计方案 扩展后代码结构 集思广益(问题) 前言: 在上一篇文章我写了如何重建IDbCommandTreeInterceptor来实现创建人.创建时间.更 ...
- Activity之概览屏幕(Overview Screen)
概览屏幕 概览屏幕(也称为最新动态屏幕.最近任务列表或最近使用的应用)是一个系统级别 UI,其中列出了最近访问过的 Activity 和任务. 用户可以浏览该列表并选择要恢复的任务,也可以通过滑动清除 ...
- Android程序中--不能改变的事情
有时,开发人员会对应用程序进行更改,当安装为以前版本的更新时出现令人惊讶的结果 - 快捷方式断开,小部件消失或甚至根本无法安装. 应用程序的某些部分在发布后是不可变的,您可以通过理解它们来避免意外. ...
- U盘安装Kali 出现cd-rom无法挂载 已解决
用U盘安装Kali Linux的过程中,出现cd-rom无法挂载的现象,百度坑比啊,醉了.下面亲测成功 出现无法挂载后,选择执行shell 第一步:df -m此时会看到挂载信息,最下面的是/dev/* ...
- [css]实现垂直居中水平居中的几种方式
转自博客 http://blog.csdn.net/freshlover/article/details/11579669 居中方式: 一.容器内(Within Container) 内容块的父容器设 ...