Skip to content

Commit

Permalink
Android 2.x doesn't support Data-URI
Browse files Browse the repository at this point in the history
  • Loading branch information
davidshimjs committed Jan 8, 2013
1 parent ea5a4b8 commit a413bc3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
45 changes: 43 additions & 2 deletions qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ var QRCode;
return typeof CanvasRenderingContext2D != "undefined";
}

// android 2.x doesn't support Data-URI spec
function _getAndroid() {
var android = false;
var sAgent = navigator.userAgent;

if (/android/i.test(sAgent)) { // android
android = true;
aMat = sAgent.toString().match(/android ([0-9]\.[0-9])/i);

if (aMat && aMat[1]) {
android = parseFloat(aMat[1]);
}
}

return android;
}

// Drawing in DOM by using Table tag
var Drawing = !_isSupportCanvas() ? (function () {
var _el = null;
Expand Down Expand Up @@ -161,13 +178,35 @@ var QRCode;
var _bSupportDataURI = null;
var _oContext = null;
var _bIsPainted = false;
var _android = _getAndroid();

function _onMakeImage() {
_elImage.src = _elCanvas.toDataURL("image/png");
_elImage.style.display = "block";
_elCanvas.style.display = "none";
}

// Android 2.1 bug workaround
// http://code.google.com/p/android/issues/detail?id=5141
if (_android && _android <= 2.1) {
var factor = 1 / window.devicePixelRatio;
var drawImage = CanvasRenderingContext2D.prototype.drawImage;
CanvasRenderingContext2D.prototype.drawImage = function (image, sx, sy, sw, sh, dx, dy, dw, dh) {
if (("nodeName" in image) && /img/i.test(image.nodeName)) {
for (var i = arguments.length - 1; i >= 1; i--) {
arguments[i] = arguments[i] * factor;
}
} else if (typeof dw == "undefined") {
arguments[1] *= factor;
arguments[2] *= factor;
arguments[3] *= factor;
arguments[4] *= factor;
}

drawImage.apply(this, arguments);
};
}

/**
* Check whether the user's browser supports Data URI or not
*
Expand Down Expand Up @@ -409,8 +448,9 @@ var QRCode;

if (typeof el == "string") {
el = document.getElementById(el);
}
}

this._android = _getAndroid();
this._el = el;
this._oQRCode = null;
this._oDrawing = new Drawing(this._el, this._htOption);
Expand All @@ -437,11 +477,12 @@ var QRCode;
/**
* Make the Image from Canvas element
* - It occurs automatically
* - Android below 3 doesn't support Data-URI spec.
*
* @private
*/
QRCode.prototype.makeImage = function () {
if (typeof this._oDrawing.makeImage == "function") {
if (typeof this._oDrawing.makeImage == "function" && (!this._android || this._android >= 3)) {
this._oDrawing.makeImage();
}
};
Expand Down
Loading

0 comments on commit a413bc3

Please sign in to comment.