Barcode detection from an image on server side (particularly on node.js)

31 Views Asked by At

I've been trying to research a lot and try different libraries that will do the job: barcode detection from an image on the server-side.

I tried some libraries that try to do the job and can work on a node environment on the server side, including:

For such libraries, an image data would be expected to be in Uint8ClampedArray object. I have prepared a function that is doing the job of eventually passing it to the barcode detection libraries:

interface ImageData {
  luminanceData: Uint8ClampedArray;
  width: number;
  height: number;
}

export async function getImageDataFromUrl(url: string): Promise<ImageData> {
  // Fetch the image from the URL
  const response = await fetch(url);
  const arrayBuffer = await response.arrayBuffer();
  const buffer = Buffer.from(arrayBuffer);

  // Process the image with Jimp
  const image = await Jimp.read(buffer);
  image.grayscale(); // This line converts the image to grayscale
  const { width, height } = image.bitmap;

  // Convert image data to weighted luminance data
  // Proceed with luminance data conversion as before
  const luminanceData = new Uint8ClampedArray(width * height);
  image.scan(0, 0, width, height, (x, y, idx) => {
    // Now, since the image is grayscale, R, G, and B are equal
    const gray = image.bitmap.data[idx + 0]; // Can use any channel since R=G=B in grayscale
    luminanceData[y * width + x] = gray;
  });
  // Return the image data
  return {
    luminanceData,
    width,
    height,
  };
}

The result of the above and processing through such libraries does not always return the expected barcode number. Most of the time, the image showing the barcode needs to be clearly visible in order to be able to expect good barcode detection and a successful result.

We've been using as well the Expo barcode Scanner on mobile devices which is really doing a great job when coming to barcode detection by camera. Very curious about what is used under the hood of the expo barcode scanner that makes it that good that it is detecting barcodes very accurately and fast.

My question is whether someone can suggest a good library for the server side like node.js, where from an image of a product containing the barcode, it can detect a barcode number.

Any help would be much appreciated.

0

There are 0 best solutions below