Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#527 Unable to scan PDF_417 codes #614

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/browser/BrowserCodeReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,14 +934,17 @@ export class BrowserCodeReader {
mediaElement: HTMLVisualMediaElement
): BinaryBitmap {
const ctx = this.getCaptureCanvasContext(mediaElement);
if(mediaElement instanceof HTMLVideoElement) {
// doing a scan with inverted colors on the second scan should only happen for video elements
let doAutoInvert = false;
if (mediaElement instanceof HTMLVideoElement) {
this.drawFrameOnCanvas(<HTMLVideoElement>mediaElement);
doAutoInvert = true;
} else {
this.drawImageOnCanvas(<HTMLImageElement>mediaElement);
}
const canvas = this.getCaptureCanvas(mediaElement);

const luminanceSource = new HTMLCanvasElementLuminanceSource(canvas);
const luminanceSource = new HTMLCanvasElementLuminanceSource(canvas, doAutoInvert);
const hybridBinarizer = new HybridBinarizer(luminanceSource);

return new BinaryBitmap(hybridBinarizer);
Expand Down
121 changes: 59 additions & 62 deletions src/browser/HTMLCanvasElementLuminanceSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,74 +10,71 @@ export class HTMLCanvasElementLuminanceSource extends LuminanceSource {
private buffer: Uint8ClampedArray;

private static DEGREE_TO_RADIANS = Math.PI / 180;
private static FRAME_INDEX = true;
private static FRAME_INDEX = true;

private tempCanvasElement: HTMLCanvasElement = null;

public constructor(private canvas: HTMLCanvasElement) {
super(canvas.width, canvas.height);
this.buffer = HTMLCanvasElementLuminanceSource.makeBufferFromCanvasImageData(canvas);
public constructor(private canvas: HTMLCanvasElement, doAutoInvert: boolean = false) {
super(canvas.width, canvas.height);
this.buffer = HTMLCanvasElementLuminanceSource.makeBufferFromCanvasImageData(canvas, doAutoInvert);
}

private static makeBufferFromCanvasImageData(canvas: HTMLCanvasElement): Uint8ClampedArray {
const imageData = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height);
return HTMLCanvasElementLuminanceSource.toGrayscaleBuffer(imageData.data, canvas.width, canvas.height);
private static makeBufferFromCanvasImageData(canvas: HTMLCanvasElement, doAutoInvert: boolean = false): Uint8ClampedArray {
const imageData = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height);
return HTMLCanvasElementLuminanceSource.toGrayscaleBuffer(imageData.data, canvas.width, canvas.height, doAutoInvert);
}

private static toGrayscaleBuffer(imageBuffer: Uint8ClampedArray, width: number, height: number): Uint8ClampedArray {
const grayscaleBuffer = new Uint8ClampedArray(width * height);
HTMLCanvasElementLuminanceSource.FRAME_INDEX = !HTMLCanvasElementLuminanceSource.FRAME_INDEX;
if(HTMLCanvasElementLuminanceSource.FRAME_INDEX)
{
for (let i = 0, j = 0, length = imageBuffer.length; i < length; i += 4, j++) {
let gray;
const alpha = imageBuffer[i + 3];
// The color of fully-transparent pixels is irrelevant. They are often, technically, fully-transparent
// black (0 alpha, and then 0 RGB). They are often used, of course as the "white" area in a
// barcode image. Force any such pixel to be white:
if (alpha === 0) {
gray = 0xFF;
} else {
const pixelR = imageBuffer[i];
const pixelG = imageBuffer[i + 1];
const pixelB = imageBuffer[i + 2];
// .299R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC),
// (306*R) >> 10 is approximately equal to R*0.299, and so on.
// 0x200 >> 10 is 0.5, it implements rounding.
gray = (306 * pixelR +
601 * pixelG +
117 * pixelB +
0x200) >> 10;
}
grayscaleBuffer[j] = gray;
}
}
else
{
for (let i = 0, j = 0, length = imageBuffer.length; i < length; i += 4, j++) {
let gray;
const alpha = imageBuffer[i + 3];
// The color of fully-transparent pixels is irrelevant. They are often, technically, fully-transparent
// black (0 alpha, and then 0 RGB). They are often used, of course as the "white" area in a
// barcode image. Force any such pixel to be white:
if (alpha === 0) {
gray = 0xFF;
} else {
const pixelR = imageBuffer[i];
const pixelG = imageBuffer[i + 1];
const pixelB = imageBuffer[i + 2];
// .299R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC),
// (306*R) >> 10 is approximately equal to R*0.299, and so on.
// 0x200 >> 10 is 0.5, it implements rounding.
gray = (306 * pixelR +
601 * pixelG +
117 * pixelB +
0x200) >> 10;
}
grayscaleBuffer[j] = 0xFF - gray;
}
}
return grayscaleBuffer;
private static toGrayscaleBuffer(imageBuffer: Uint8ClampedArray, width: number, height: number, doAutoInvert: boolean = false): Uint8ClampedArray {
const grayscaleBuffer = new Uint8ClampedArray(width * height);
HTMLCanvasElementLuminanceSource.FRAME_INDEX = !HTMLCanvasElementLuminanceSource.FRAME_INDEX;
if (HTMLCanvasElementLuminanceSource.FRAME_INDEX || !doAutoInvert) {
for (let i = 0, j = 0, length = imageBuffer.length; i < length; i += 4, j++) {
let gray;
const alpha = imageBuffer[i + 3];
// The color of fully-transparent pixels is irrelevant. They are often, technically, fully-transparent
// black (0 alpha, and then 0 RGB). They are often used, of course as the "white" area in a
// barcode image. Force any such pixel to be white:
if (alpha === 0) {
gray = 0xFF;
} else {
const pixelR = imageBuffer[i];
const pixelG = imageBuffer[i + 1];
const pixelB = imageBuffer[i + 2];
// .299R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC),
// (306*R) >> 10 is approximately equal to R*0.299, and so on.
// 0x200 >> 10 is 0.5, it implements rounding.
gray = (306 * pixelR +
601 * pixelG +
117 * pixelB +
0x200) >> 10;
}
grayscaleBuffer[j] = gray;
}
} else {
for (let i = 0, j = 0, length = imageBuffer.length; i < length; i += 4, j++) {
let gray;
const alpha = imageBuffer[i + 3];
// The color of fully-transparent pixels is irrelevant. They are often, technically, fully-transparent
// black (0 alpha, and then 0 RGB). They are often used, of course as the "white" area in a
// barcode image. Force any such pixel to be white:
if (alpha === 0) {
gray = 0xFF;
} else {
const pixelR = imageBuffer[i];
const pixelG = imageBuffer[i + 1];
const pixelB = imageBuffer[i + 2];
// .299R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC),
// (306*R) >> 10 is approximately equal to R*0.299, and so on.
// 0x200 >> 10 is 0.5, it implements rounding.
gray = (306 * pixelR +
601 * pixelG +
117 * pixelB +
0x200) >> 10;
}
grayscaleBuffer[j] = 0xFF - gray;
}
}
return grayscaleBuffer;
}

public getRow(y: number /*int*/, row: Uint8ClampedArray): Uint8ClampedArray {
Expand Down Expand Up @@ -167,4 +164,4 @@ export class HTMLCanvasElementLuminanceSource extends LuminanceSource {
public invert(): LuminanceSource {
return new InvertedLuminanceSource(this);
}
}
}
Loading