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();
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM