HTMLCanvasElementLuminanceSource.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.HTMLCanvasElementLuminanceSource = void 0;
  17. var InvertedLuminanceSource_1 = require("../core/InvertedLuminanceSource");
  18. var LuminanceSource_1 = require("../core/LuminanceSource");
  19. var IllegalArgumentException_1 = require("../core/IllegalArgumentException");
  20. /**
  21. * @deprecated Moving to @zxing/browser
  22. */
  23. var HTMLCanvasElementLuminanceSource = /** @class */ (function (_super) {
  24. __extends(HTMLCanvasElementLuminanceSource, _super);
  25. function HTMLCanvasElementLuminanceSource(canvas) {
  26. var _this = _super.call(this, canvas.width, canvas.height) || this;
  27. _this.canvas = canvas;
  28. _this.tempCanvasElement = null;
  29. _this.buffer = HTMLCanvasElementLuminanceSource.makeBufferFromCanvasImageData(canvas);
  30. return _this;
  31. }
  32. HTMLCanvasElementLuminanceSource.makeBufferFromCanvasImageData = function (canvas) {
  33. var imageData = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height);
  34. return HTMLCanvasElementLuminanceSource.toGrayscaleBuffer(imageData.data, canvas.width, canvas.height);
  35. };
  36. HTMLCanvasElementLuminanceSource.toGrayscaleBuffer = function (imageBuffer, width, height) {
  37. var grayscaleBuffer = new Uint8ClampedArray(width * height);
  38. HTMLCanvasElementLuminanceSource.FRAME_INDEX = !HTMLCanvasElementLuminanceSource.FRAME_INDEX;
  39. if (HTMLCanvasElementLuminanceSource.FRAME_INDEX) {
  40. for (var i = 0, j = 0, length_1 = imageBuffer.length; i < length_1; i += 4, j++) {
  41. var gray = void 0;
  42. var alpha = imageBuffer[i + 3];
  43. // The color of fully-transparent pixels is irrelevant. They are often, technically, fully-transparent
  44. // black (0 alpha, and then 0 RGB). They are often used, of course as the "white" area in a
  45. // barcode image. Force any such pixel to be white:
  46. if (alpha === 0) {
  47. gray = 0xFF;
  48. }
  49. else {
  50. var pixelR = imageBuffer[i];
  51. var pixelG = imageBuffer[i + 1];
  52. var pixelB = imageBuffer[i + 2];
  53. // .299R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC),
  54. // (306*R) >> 10 is approximately equal to R*0.299, and so on.
  55. // 0x200 >> 10 is 0.5, it implements rounding.
  56. gray = (306 * pixelR +
  57. 601 * pixelG +
  58. 117 * pixelB +
  59. 0x200) >> 10;
  60. }
  61. grayscaleBuffer[j] = gray;
  62. }
  63. }
  64. else {
  65. for (var i = 0, j = 0, length_2 = imageBuffer.length; i < length_2; i += 4, j++) {
  66. var gray = void 0;
  67. var alpha = imageBuffer[i + 3];
  68. // The color of fully-transparent pixels is irrelevant. They are often, technically, fully-transparent
  69. // black (0 alpha, and then 0 RGB). They are often used, of course as the "white" area in a
  70. // barcode image. Force any such pixel to be white:
  71. if (alpha === 0) {
  72. gray = 0xFF;
  73. }
  74. else {
  75. var pixelR = imageBuffer[i];
  76. var pixelG = imageBuffer[i + 1];
  77. var pixelB = imageBuffer[i + 2];
  78. // .299R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC),
  79. // (306*R) >> 10 is approximately equal to R*0.299, and so on.
  80. // 0x200 >> 10 is 0.5, it implements rounding.
  81. gray = (306 * pixelR +
  82. 601 * pixelG +
  83. 117 * pixelB +
  84. 0x200) >> 10;
  85. }
  86. grayscaleBuffer[j] = 0xFF - gray;
  87. }
  88. }
  89. return grayscaleBuffer;
  90. };
  91. HTMLCanvasElementLuminanceSource.prototype.getRow = function (y /*int*/, row) {
  92. if (y < 0 || y >= this.getHeight()) {
  93. throw new IllegalArgumentException_1.default('Requested row is outside the image: ' + y);
  94. }
  95. var width = this.getWidth();
  96. var start = y * width;
  97. if (row === null) {
  98. row = this.buffer.slice(start, start + width);
  99. }
  100. else {
  101. if (row.length < width) {
  102. row = new Uint8ClampedArray(width);
  103. }
  104. // The underlying raster of image consists of bytes with the luminance values
  105. // TODO: can avoid set/slice?
  106. row.set(this.buffer.slice(start, start + width));
  107. }
  108. return row;
  109. };
  110. HTMLCanvasElementLuminanceSource.prototype.getMatrix = function () {
  111. return this.buffer;
  112. };
  113. HTMLCanvasElementLuminanceSource.prototype.isCropSupported = function () {
  114. return true;
  115. };
  116. HTMLCanvasElementLuminanceSource.prototype.crop = function (left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  117. _super.prototype.crop.call(this, left, top, width, height);
  118. return this;
  119. };
  120. /**
  121. * This is always true, since the image is a gray-scale image.
  122. *
  123. * @return true
  124. */
  125. HTMLCanvasElementLuminanceSource.prototype.isRotateSupported = function () {
  126. return true;
  127. };
  128. HTMLCanvasElementLuminanceSource.prototype.rotateCounterClockwise = function () {
  129. this.rotate(-90);
  130. return this;
  131. };
  132. HTMLCanvasElementLuminanceSource.prototype.rotateCounterClockwise45 = function () {
  133. this.rotate(-45);
  134. return this;
  135. };
  136. HTMLCanvasElementLuminanceSource.prototype.getTempCanvasElement = function () {
  137. if (null === this.tempCanvasElement) {
  138. var tempCanvasElement = this.canvas.ownerDocument.createElement('canvas');
  139. tempCanvasElement.width = this.canvas.width;
  140. tempCanvasElement.height = this.canvas.height;
  141. this.tempCanvasElement = tempCanvasElement;
  142. }
  143. return this.tempCanvasElement;
  144. };
  145. HTMLCanvasElementLuminanceSource.prototype.rotate = function (angle) {
  146. var tempCanvasElement = this.getTempCanvasElement();
  147. var tempContext = tempCanvasElement.getContext('2d');
  148. var angleRadians = angle * HTMLCanvasElementLuminanceSource.DEGREE_TO_RADIANS;
  149. // Calculate and set new dimensions for temp canvas
  150. var width = this.canvas.width;
  151. var height = this.canvas.height;
  152. var newWidth = Math.ceil(Math.abs(Math.cos(angleRadians)) * width + Math.abs(Math.sin(angleRadians)) * height);
  153. var newHeight = Math.ceil(Math.abs(Math.sin(angleRadians)) * width + Math.abs(Math.cos(angleRadians)) * height);
  154. tempCanvasElement.width = newWidth;
  155. tempCanvasElement.height = newHeight;
  156. // Draw at center of temp canvas to prevent clipping of image data
  157. tempContext.translate(newWidth / 2, newHeight / 2);
  158. tempContext.rotate(angleRadians);
  159. tempContext.drawImage(this.canvas, width / -2, height / -2);
  160. this.buffer = HTMLCanvasElementLuminanceSource.makeBufferFromCanvasImageData(tempCanvasElement);
  161. return this;
  162. };
  163. HTMLCanvasElementLuminanceSource.prototype.invert = function () {
  164. return new InvertedLuminanceSource_1.default(this);
  165. };
  166. HTMLCanvasElementLuminanceSource.DEGREE_TO_RADIANS = Math.PI / 180;
  167. HTMLCanvasElementLuminanceSource.FRAME_INDEX = true;
  168. return HTMLCanvasElementLuminanceSource;
  169. }(LuminanceSource_1.default));
  170. exports.HTMLCanvasElementLuminanceSource = HTMLCanvasElementLuminanceSource;