r/WebScrapingInsider • • 21d ago

Decoding EAN barcodes from product photos when the API has none

I've been building grocery data scrapers for a food-ingredients app, and one problem took more thought than I expected, so I'm sharing how I handled it.

**The problem:** Blinkit (Indian quick commerce) has a clean product API: price, MRP, stock, the full ingredient list, nutrition text, manufacturer, and FSSAI license. What it doesn't have is a barcode. No EAN field anywhere, and the barcode is what you need to match products across stores or against Open Food Facts.

**What worked:** most packaged products have a back-of-pack photo in the gallery, and that photo usually shows the barcode. So:

  1. Pull the full image gallery from the product endpoint.
  2. Scan the images **last-to-first**. Blinkit usually puts the back-of-pack shot at the end, so you usually hit it early and can stop.
  3. Decode with `zxing-wasm` (ZXing compiled to WebAssembly, so it runs in plain Node with no native dependencies), restricted to EAN-13, EAN-8, UPC-A and UPC-E with `tryHarder` on.
  4. **Validate the check digit**, because a false positive is worse than a miss.

**Two gotchas:**

- **The CDN serves WebP to browser-like clients.** My first run decoded almost nothing because the decoder can't read WebP. Sending `Accept: image/jpeg` gets
JPEGs back. That one header was the difference between barely working and working.
- **Coverage depends on the category.** In my tests: 9 of 16 peanut butters, 34 of 100 products in a general run, 4 of 27 chocolates. When a gallery has no barcode photo, no decoder will find one. I report `null` rather than guess.

Decoding takes about 50 ms per image, so it adds well under a second per product.

**Other details if useful:**
- Category path (l0/l1/l2) isn't on the product object; it's in a separate category-bar tracking block.
- Nutrition comes either as a text block ("Energy - 325 kcal\nProtein - 7.7 g") or as separate attributes. I parse both into one `{energy_kcal, protein_g, ...}` object and normalise aliases like "Total Sugars" / "Sugar" / "Total Sugar".

Happy to answer questions about the approach, or to compare notes if you've solved this differently (e.g. matching products by name and pack size instead).

7 Upvotes

5 comments sorted by

1

u/James-w07 21d ago

Cool to see zxing-wasm used in Node instead of some paid SaaS API, and that one Accept header basically unlocked it. how did you first realise WebP was the issue-did you inspect the response headers or just save images and open them?

Also if you ever hit cases where the barcode is slightly curved /noisy and tryHarder still fails?

1

u/SinghReddit 21d ago

Like a barcode scraping as a service, but free.. Also love the last-to-first gallery scan; tiny heuristic, big savings on wasted decode cycles. Can you share the code, or is that too much of an ask?

Btw post formmating is messed up, you need to paste that in markdown.

1

u/veganwurst 20d ago

Nice trick, especially last-to-first. A couple of things we ran into when matching on barcodes across stores:

  • Validate the check digit before trusting a decode. A misread EAN-13 that still passes the length check will match the wrong product with full confidence.
  • The same product can carry different barcodes (regional packs, multipacks, a new pack design), and occasionally the same barcode gets reused for a reformulated product. So treat a barcode match as strong evidence, then confirm with brand plus size or weight from the text you already have.

Curious what share of Blinkit products actually have a readable barcode in the gallery.

1

u/BhetwalEveliina74 10d ago

for the chocolates and shiny wrappers where coverage tanks, a quick contrast threshold or grayscale pass before feeding the buffer into the decoder usually rescues a few more reads. glare on the foil messes with the standard binarizer pretty badly