lindell / JsBarcode

Barcode generation library written in JavaScript that works in both the browser and on Node.js
http://lindell.me/JsBarcode
MIT License
5.5k stars 1.11k forks source link

Adding extra number "8" to code when generating EAN #328

Closed kub1x closed 4 years ago

kub1x commented 4 years ago

IMG_20200217_143810

Hey, I've tried to generate following value 0454834 of EAN barcode as seen on a product. Using following code:

JsBarcode(canvas, "0454834", { format: "EAN8" });

Only EAN8 format worked for this number and it generated the barcode with extra digit 8 at the end. I've inspected the packaging and the "correct" code have an extra 0 at the beginning.

Why does JsBarcode behave in this way? Would not adding the extra leading 0 be the correct modification of the code in this case?

wodin commented 4 years ago

Hi

It looks like JsBarcode assumes there's a missing check digit and adds it. It seems like a reasonable thing to do and your code can add a leading zero if that's the right thing to do for your use case.

kub1x commented 4 years ago

Hey, sorry for not replying, been a bit busy. Thanks for your reply.

Is there say a way to prevent JsBarcode from doing any modifications to the input code number and throw an "invalid EAN" error instead? That way I could check such a situation and try to add leading zero. In current state, I never get the information, that the original EAN number was, in fact invalid and that there was a change being done to it so the library yields completely different EAN.

I generate plenty of barcodes, some of which are possibly incomplete (e.g. filled by users I don't control, or codes that came through systems that converted them to integer numbers and back to strings thus removing the leading zeroes).

wodin commented 4 years ago

or codes that came through systems that converted them to integer numbers and back to strings thus removing the leading zeroes

You're allowed to say "Excel" 😂

I think there's a bit of context missing. In the bug report you posted code showing the format hard coded to "EAN8". If you know it's an EAN8 barcode, then you can count the digits and find that it's one short. But I suspect this is not good enough? How did you know it was an EAN8 barcode? What about these other barcodes that are filled in by users etc?

Also, if the user left out a digit, how do you know it was a leading zero and not some other digit in the middle?

kub1x commented 4 years ago

@wodin ambushed ;D

Well I don't know really, it's simply an EAN/GTIN on a product (food package specifically) from various sources (yep including Google Sheets / Excel) - I just try/catch.

Here are the current sources: https://github.com/kub1x/barcoder/blob/master/server.js

async function generateBarcode({ ean, format }) {
  const canvas = createCanvas(500, 1000);
  await JsBarcode(canvas, ean, { format });
  return canvas;
};
// ...
const canvas = await (generatebarcode({ ean, format: "ean13" })
         .catch(() => generatebarcode({ ean, format: "ean8" }))
         .catch(() => generatebarcode({ ean, format: "code128" })));

I'm not really sure how to handle it. I thing CODE128 is wrong for this case. I've added it, as it always generates "something" my barcode scanner can read, but if the thing is not valid EAN, I should just throw an error instead.

Is EAN13 and EAN8 somewhat backwards compatible? E.g. by prepending all numbers to 13 digits with zeroes? I thing I've tried and the result wasn't valid EAN.

Guess what I'm left here is prepending any input (with other than 13/8 digits) with zeros to 13 digits -> try -> prepending it to 8 digits -> try -> fail with invalid EAN.

wodin commented 4 years ago

I don't really know what to suggest. I am no barcode expert :)

I have just found someone with the same request as you to disabled the auto-adding of the checksum, but I think even if that is implemented it won't really solve your real problem.

277