Is there a Mathematica barcode image reader (much like TextRecognize
) that allows the user to enter an image of a barcode and then read it to get an output string?
-
1$\begingroup$ 1D or 2D? What symbology (i.e. standard)? $\endgroup$– DavidCSep 27, 2013 at 1:57
2 Answers
Here is a bare bones, non-robust, use at your own risk, etc. code39 reader
(*get image *)
i = Binarize[Import@"https://i.stack.imgur.com/Cx3JD.png", .7]
(* Char encodings from Paul's article - See link bellow *)
c1 = IntegerDigits[#, 2, 9] &@{168,42,148,138,133,388,162,52,289,97,352,49,304,112,37,292,100,265,
73,328,25,280,88,13,268,76,28,259,67,322,19,274,82,7,262,70,22,385,193,448,145,400,208};
c2 = Characters["$%*+-./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
(*binary code for the image, drop the white flanks at the ends*)
code = Join @@ Split[Round /@ Mean /@ Transpose@ImageData@i][[2 ;; -2]];
(*get strip widths... err lengths... whatever,dropping the start/stop marks*)
lengths = Most /@ Partition[Length /@ Split@code, 10];
(*determine what is a wide and a thin strips are*)
{max, min} = {Max@#, Min@#} &@lengths;
f = Nearest[{{max} -> 1, {min} -> 0}];
(*convert from wide/thin encoding to chars*)
StringJoin @@ (Join @@@ Map[f, lengths, {2}] /. Thread[Rule[c1, c2]])
"CODE39"
Table and encoding shamelessly stolen from Paul Abbott's article on "The Mathematica Journal"
-
-
$\begingroup$ @cormullion Yep, that image hasn't white flanks. But the code works OK if you pad it. Try with
i = ImageTake[ImagePad[Import["http://worldbarcodes.com/wp-content/uploads/39123439-code39.gif"], {{40, 40}, {0, 0}}, White], 100]
instead` $\endgroup$ Sep 27, 2013 at 11:57 -
4$\begingroup$ @DavidG.Stork This code implements a barcode reader, not a creator as you could have found if you cared to test it. Enough. $\endgroup$ Sep 27, 2013 at 17:11
-
1$\begingroup$ You are correct. The code you posted looked much like (http : // www.mathematica - journal.com/issue/v9i2/contents/Tricks9 - 2/Tricks9 - 2 _ 2. html) and the output appeared to be a crisp barcode (not a .jpg input image), so I mistakenly assumed yours was the same (creator) code. My mistake. $\endgroup$ Sep 27, 2013 at 17:29
-
2$\begingroup$ @cormullion You may use a small tripod to hold the camera still $\endgroup$ Sep 27, 2013 at 18:48
Yes!... It's new in version 10. Here is the documentation page
The syntax is very simple:
BarcodeRecognize["image"]
It uses ZXing library (http://code.google.com/p/zxing/).
You can alse create barcode using BarcodeImage
as these examples:
-
$\begingroup$ How do you know it's using the zxing library? (I missed this in the docs and I'm curious) $\endgroup$– AjasjaDec 5, 2013 at 12:32
-
1$\begingroup$ Secret!.. I'm a beta tester and exchanged some mails with the developer. :) $\endgroup$– MurtaDec 5, 2013 at 13:56
-
6$\begingroup$ Very useful for a lot of Mathematica users in the supermarket business :) $\endgroup$ Nov 19, 2014 at 23:49