Unleashing the Power of the Rainbow Patch

krish gokul
4 min readNov 22, 2023

--

Elevating Barcode Scanning with “html5-qrcode”

Barcode scanning has become an indispensable part of our daily lives, seamlessly integrating into applications and systems. However, the effectiveness of traditional scanning packages is often challenged when confronted with barcodes of diverse colors. In this blog post, we introduce the innovative Rainbow Patch — a coding solution designed to enhance barcode scanning performance, especially in scenarios where barcode colors vary widely, using the “html5-qrcode” NPM package (version 2.3.8 or later).

The Challenge: Colorful Barcodes

Typically, barcode scanners perform admirably when dealing with standard black and white barcodes, reminiscent of those produced by Xerox machines. However, the real world isn’t always so monochromatic. Barcodes can come in a multitude of colors, presenting a unique challenge for scanners. Whether it’s a purple barcode against a black background, a white barcode on a blue background, a metallic barcode, or a barcode with a white line on a green background — the variations can lead to inaccurate data retrieval.

The Concept Behind the Rainbow Patch

The Rainbow Patch concept is rooted in simplicity. It acknowledges the power of primary colors, focusing solely on black and white. The idea is to convert any other color to these primary shades, making barcode scanning more straightforward and robust.

Initialization : Introducing the Rainbow Patch

RainbowPatch() {
const oldFunc = (ZXing.HTMLCanvasElementLuminanceSource as any).toGrayscaleBuffer;
let inverterToggle = false;
(ZXing.HTMLCanvasElementLuminanceSource as any).toGrayscaleBuffer = function (imagebuffer, width, height) {
let grayscaleBuffer = oldFunc(imagebuffer, width, height);
inverterToggle = !inverterToggle;
if(inverterToggle) {
const colorConverter = convertTo3DArray(imagebuffer, width, height);
const grayscaleTo1DArrayBuffer = convertTo1DArray(colorConverter, width, height);
grayscaleBuffer = grayscaleTo1DArrayBuffer;
}
return grayscaleBuffer;
};
}

Rainbow Patch : Constants and Functions: The Color Palette

// Constants
export const colorBlack = '#000000';
export const colorWhite = '#ffffff';

// Functions
function convertTo3DArray(imageData, width, height) {
const result = new Array(height);
for (let y = 0; y < height; y++) {
result[y] = new Array(width);

for (let x = 0; x < width; x++) {
const index = (y * width + x) * 4; // Each pixel has 4 values (R, G, B, A)
const r = imageData[index];
const g = imageData[index + 1];
const b = imageData[index + 2];

// Map the color to black or white and store it in the 3D array
result[y][x] = mapColorsToBlackAndWhite(r, g, b);
}
}
return result;
}

function mapColorsToBlackAndWhite(r, g, b) {
// Calculate the Euclidean distance to white and black
const distanceToWhite = getDiffColor({ r, g, b }, { r: 255, g: 255, b: 255 });
const distanceToBlack = getDiffColor({ r, g, b }, { r: 0, g: 0, b: 0 });

// Decide which color to map to
if (distanceToWhite <= distanceToBlack) {
return colorWhite; // Map to white
} else {
return colorBlack; // Map to black
}
}

function getDiffColor(a, b) {
return Math.sqrt(Math.pow((a.r - b.r), 2) + Math.pow((a.g - b.g), 2) + Math.pow((a.b - b.b), 2));
}

// Function to convert the 3D array of colors back to a 1D grayscale array
function convertTo1DArray(colorArray, width, height) {
const grayscaleBuffer = new Uint8ClampedArray(width * height * 4);

for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const color = colorArray[y][x];
const isWhite = color === colorWhite;

const pixelIndex = (y * width + x) * 4;

// Set RGBA values to 0x00 for black and 0xFF for white
grayscaleBuffer[pixelIndex] = isWhite ? 0xFF : 0x00; // R
grayscaleBuffer[pixelIndex + 1] = isWhite ? 0xFF : 0x00; // G
grayscaleBuffer[pixelIndex + 2] = isWhite ? 0xFF : 0x00; // B
grayscaleBuffer[pixelIndex + 3] = 0xFF; // A
}
}
return grayscaleBuffer;
}

These constants and functions lay the foundation for the color conversion process. They define the primary colors and the logic for mapping the varied colors in the barcode to either black or white.

Integrating with “html5-qrcode”

The Rainbow Patch seamlessly integrates with the “html5-qrcode” NPM package, enhancing its scanning functionality. By incorporating the Rainbow Patch into your application, you can extend the capabilities of the “html5-qrcode” package to handle a diverse range of barcode colors.

The Rainbow Patch in Action

Understanding the Rainbow Patch involves grasping its three fundamental steps:

1. Color Conversion: The barcode image undergoes a transformation where each pixel is analyzed, and its color is converted to either black or white.

2. Euclidean Distance Calculation: The algorithm calculates the Euclidean distance of each pixel color to both white and black, determining which color the pixel should be mapped to.

3. Conversion Back to 1D Array: After the color conversion, the 3D color array is reverted to a 1D grayscale array suitable for traditional barcode scanning.

Benefits of the Rainbow Patch

Implementing the Rainbow Patch yields several advantages for barcode scanning applications:

  • Versatility: The Rainbow Patch proves its mettle by handling a wide spectrum of barcode colors.
  • Enhanced Performance: By simplifying the color variations, the Rainbow Patch significantly boosts scanning performance.
  • Ease of Integration: Integration into existing barcode scanning packages is seamless, requiring minimal adjustments.

Conclusion: Coding for a Colorful World

In conclusion, the Rainbow Patch is more than just a code snippet; it’s a solution crafted to meet the challenges posed by the vibrant palette of barcode colors in the real world. By reducing the complexity of color variations to a binary decision — black or white — developers can elevate the reliability and efficiency of their barcode scanning applications.

As you embark on your coding journey, feel free to experiment with and adapt the Rainbow Patch to suit your specific use cases, seamlessly integrating it with the “html5-qrcode” package. In the ever-evolving landscape of technology, solutions like the Rainbow Patch demonstrate the ingenuity and adaptability that make coding a thrilling endeavor.

Happy coding!

--

--