patzly / grocy-android

ERP beyond your fridge, now on your phone – An awesome companion app for grocy
https://patrickzedler.com/grocy/
GNU General Public License v3.0
830 stars 80 forks source link

[minor] trim barcodes (grocycodes) before processing #777

Open TinkerTob opened 10 months ago

TinkerTob commented 10 months ago

Hi,

for whatever reason, the grocycode-barcodes on my product labels seem to have leading / trailing whitespaces when scanned in grocy-android. Thus, they don't get recognized as grocycodes but get treated as normal (unknown) barcodes, rendering them unfunctional.

Click here for details, probably unrelated to grocy-android. --- I am Using DataMatrix barcodes generated in PHP using the [kreativekorp/barcode](https://github.com/kreativekorp/barcode) library and explicitly trim the grocycode before generating the barcode: ```php $gr_barcode = trim($_POST['grocycode']); $symbology = "gs1-dmtx-s"; $barcode_options = []; $generator = new barcode_generator(); $barcode = $generator->render_image($symbology, $gr_barcode, $barcode_options); ``` Result: ![grafik](https://github.com/patzly/grocy-android/assets/66213768/652e2434-045e-4f9b-b8ca-8b69226c079e) When I scan this with my external hardware scanner, the samsung stock camera app or the XZing Barcode Scanner app, everything appears fine. However, when I scan it in grocy-android or the the [Barcode-to-PC](https://barcodetopc.com/) android app, there seems to be a leading whitespace. When generating a DataMatrix code on https://barcode.tec-it.com/de/DataMatrix, everything works as intended. I suspect this might be an issue regarding the DataMatrix symbology / dialect, either in the PHP library or in the XZing Barcode Scanner API. However, hunting this down would probably take quite some time and require me to replace hundreds of labels, so I'd be really grateful for a quick and painless workaround in grocy-android. ---

While I'm aware that the root of the problem does not originate with grocy-android, it would make my life a lot easier if leading and trailing whitespaces could be trimmed from the barcode before applying the grocycode regex matcher.

Thank you for the good work you're doing here!

Best regards, Tobias

ssiegel commented 9 months ago

The problem here is that the Datamatrix was encoded indicating the GS1 Datamatrix symbology ("gs1-dmtx-s"), not the regular Datamatrix symbology ("dmtx-s"). The use of GS1 Datamatrix symbology indicates that the encoded data follows GS1 data structures, which the data in this barcode does not, making the barcode technically invalid.

The GS1 Datamatrix starts with a special FNC1 control code that XZing decodes to ASCII 29. While simply removing the ASCII 29 character on the Grocy side might help in this particular case, it doesn't look like the right thing to do. Instead, if processing is to be done at all, I think the sensible approach would be to extract the appropriate piece of information from the GS1 data structure, most likely the GTIN, marked by the application identifier 01.