qrcode length overflow (1632>1056)--qrcode.js使用过程中二维码长度溢出解决办法

  近日在开发过程中需要为页面动态生成一个二维码信息,由于这个二维码中包含了很多文字,字母以及符号,测试过程中发现有些二维码会报错,因为二维码内容太多了,没办法显示。后来在GitHub中找到了解决办法。

 

这是源码中修改前的内容:

make:function(){this.makeImpl(false,this.getBestMaskPattern());}

QRCode.prototype.makeCode = function (sText) { this._oQRCode = new QRCodeModel(_getTypeNumber(sText, this._htOption.correctLevel), this._htOption.correctLevel); this._oQRCode.addData(sText); this._oQRCode.make(); this._el.title = sText; this._oDrawing.draw(this._oQRCode); this.makeImage(); };

 

这是修改后的内容:

make:function(){
if(this.typeNumber<1){
var typeNumber = 1;
for (typeNumber = 1;typeNumber<40;typeNumber++){
var rsBlocks = QRRSBlock.getRSBlocks(typeNumber, this.errorCorrectLevel);
var buffer = new QRBitBuffer();var totalDataCount = 0;
for(var i=0;i<rsBlocks.length;i++) {
  totalDataCount+=rsBlocks[i].dataCount;
}
for (var i = 0; i < this.dataList.length; i++) {
  var data = this.dataList[i];
  buffer.put(data.mode, 4);
  buffer.put(data.getLength(), QRUtil.getLengthInBits(data.mode, typeNumber));
  data.write(buffer);}
if (buffer.getLengthInBits() <= totalDataCount * 8)break;}
  this.typeNumber = typeNumber;
}



QRCode.prototype.makeCode = function (sText) {
this._oQRCode = new QRCodeModel(-1, this._htOption.correctLevel);
this._oQRCode.addData(sText);
this._oQRCode.make();
this._el.title = sText;
this._oDrawing.draw(this._oQRCode);
this.makeImage();
};

 

posted @ 2017-10-24 15:35  Miracle--  阅读(5908)  评论(2编辑  收藏  举报