barnhill / barcodelib

C# Barcode Image Generation Library
Apache License 2.0
733 stars 238 forks source link

Code 39, quiet zone and narrow/wide #160

Closed rob313663 closed 11 months ago

rob313663 commented 1 year ago

Hi!

I have started to do some coding myself generating barcodes, just as a hobby, I started with Code 39 since it is quite simple.

My findings so far:

Quiet Zone • There are no quiet zones before and after the barcode. It should be 10X, where X is the narrow width, or configurable (maybe it is and I just do not know enough yet).

Ratio • By default, it generates a 2:1 barcode where the wide stuff is twice the width of the narrow stuff. The 3:1 ratio is preferred. I do not understand the code of BarcodeLib enough yet to know if this is an option. I fixed this in my code by looking ahead in the encoded "binary" string to check if there are two consecutive 1's or 0's and make them 3x wider that a single 1 or 0, and also "consuming" double digits in the bit pattern string.

Dark Mode • In dark mode in Windows, the barcode white/black is inverted and can't be read. Maybe hardcoded (or default) black bars on white background would be better.

Performance • The lists of the code words and the full ASCII mapping are initialized every time for a barcode. which is expensive if you are generating many barcodes. They should also be Dictionary<T,T> which is better than Hashtable I think. The clearing of them saves some bytes, really not an issue today or even ten years ago since it is in the range of 1k bytes. I made them statically initialized in my class.

I would be happy to share code if you would like.

Best regards Robert

barnhill commented 11 months ago

I purposefully remove the mappings from memory as creating them as static leaves them in memory. I tested this with using a dictionary vs the hashtable but making the dictionary static and only initing them once but the memory usage spikes very fast to over 3gb where as the hashtable and initing it every time doesnt go above 400mb even when it spikes. The CPU usage was relatively minor even when repopulating the list every time. This is the age old trade off of memory vs cpu I believe. The main goal was to be fast enough while compressing memory usage here so it can utilize the smallest instance possible if this is wrapped and put into a container. (which is what I do and have it running in AWS on the tiniest instance and it can still generate and generate thousands of requests a second ... its more the http server thats limiting it from what I can tell with that many requests being handled)

rob313663 commented 11 months ago

Ok, I see (about the memory). I need to make some tests in this area. Maybe the class is initialized from scratch for each call in an AWS instance and instead of saving time memory gets wasted a lot.