3
$\begingroup$

So, I have a function called sillyBarcodeMaker[]. It generates images like:

Barcode Example 1. It is pink and green with guard characters. Barcode Example 2. It is black and white with guard characters. Barcode Example 3. It is blue and yellow with guard characters. Barcode Example 4. It is black and white without guard characters.

But when I use BarcodeRecognize[Rasterize[sillBarcodeMaker[]]], it just gives: {}

I believe that I had encoded the information correctly for what the numbers for the barcode should mean in the list allowedCodeUnits.

Here is my code:

sillyBarcodeMaker[]:=
Module[{barcodeDigitQuantity,allowedColorCombinations,chosenColor,backgroundColor,color,centerCharacterColors,endCharacterColors,guardCharacters,barWidth,
barHeight, guardScale,hasGuards,codesInBarcode,  allowedCodeUnits, segmentLengths,accumulatedLengths},
barcodeDigitQuantity=RandomInteger[{8,14}];
allowedColorCombinations={
{RGBColor[0,0,0], RGBColor[1,1,1]},{RGBColor[1,0,0], RGBColor[0,1,1]},
{RGBColor[0,1,0], RGBColor[1,0,1]},{RGBColor[0,0,1], RGBColor[1,1,0]},
{RGBColor[1,1,1], RGBColor[0,0,0]},{RGBColor[0,1,1], RGBColor[1,0,0]},
{RGBColor[1,0,1], RGBColor[0,1,0]},{RGBColor[1,1,0], RGBColor[0,0,1]}};
chosenColor=RandomChoice[allowedColorCombinations];color=chosenColor[[1]];backgroundColor=chosenColor[[2]];
centerCharacterColors = {backgroundColor, color, backgroundColor, color, backgroundColor};
endCharacterColors = {color, backgroundColor, color};
guardCharacters={endCharacterColors,centerCharacterColors, endCharacterColors};
allowedCodeUnits={
{0, {backgroundColor, backgroundColor, backgroundColor, color, color, backgroundColor, color}},
{1, {backgroundColor, backgroundColor, color, color, backgroundColor, backgroundColor, color}},
{2,{backgroundColor, backgroundColor,  color, backgroundColor, backgroundColor, color, color}},
{3,{backgroundColor, color,  color, color, color, backgroundColor, color}},
{4,{backgroundColor,color,backgroundColor,backgroundColor,backgroundColor,color,color}},
{5,{backgroundColor,color,color,backgroundColor,backgroundColor,backgroundColor,color}},
{6,{backgroundColor,color,backgroundColor,color,color,color,color}},
{7,{backgroundColor,color,color,color,backgroundColor,color,color}},
{8,{backgroundColor,color,color,backgroundColor,color,color,color}},
{9,{backgroundColor,backgroundColor,backgroundColor,color,backgroundColor,color,color}}};
barWidth=RandomReal[{.25,.5}];
barHeight=RandomReal[{4,8}];
guardScale=RandomReal[{1,2}];
randomIndex[]:=RandomInteger[{1,Length[allowedCodeUnits]}];
randomBoolean[]:=RandomChoice[{True,False}];
hasGuards=randomBoolean[];
codesInBarcode=Flatten[Table[allowedCodeUnits[[randomIndex[]]][[2]],{j,1,barcodeDigitQuantity}]];
segmentLengths={Length[endCharacterColors],Length[codesInBarcode]/2,Length[centerCharacterColors],Length[codesInBarcode]/2,Length[endCharacterColors]};
accumulatedLengths=Accumulate[segmentLengths];

   If[EvenQ[barcodeDigitQuantity],
    (* This block executes if barcodeDigitQuantity is even *)
    If[hasGuards === True,
   Graphics[
   Join[
   Table[{endCharacterColors[[i]], Rectangle[{(i-1)*barWidth, -guardScale*barHeight+barHeight},{i*barWidth,barHeight}]},{i,1,Length[endCharacterColors]}],
   Table[{codesInBarcode[[i]], Rectangle[{((i-1)+ accumulatedLengths[[1]])*barWidth, 0},{(i+accumulatedLengths[[1]])*barWidth,barHeight}]},{i,1,Length[codesInBarcode]/2}],
   Table[{centerCharacterColors[[i]], Rectangle[{((i-1)+accumulatedLengths[[2]])*barWidth, -guardScale*barHeight+barHeight },{(i+accumulatedLengths[[2]])*barWidth,barHeight}]},{i,1,Length[centerCharacterColors]}],
   Table[{codesInBarcode[[i]], Rectangle[{((i-1)+accumulatedLengths[[3]])*barWidth, 0},{(i+accumulatedLengths[[3]])*barWidth,barHeight}]},{i,1,Length[codesInBarcode]/2}],
   Table[{endCharacterColors[[i]], Rectangle[{((i-1)+accumulatedLengths[[4]])*barWidth, -guardScale*barHeight+barHeight},{(i+accumulatedLengths[[4]])*barWidth,barHeight}]},{i,1,Length[endCharacterColors]}]
   ],
   AspectRatio->Automatic],
   
        Graphics[Table[{codesInBarcode[[i]], Rectangle[{(i-1)*barWidth, 0},{i*barWidth,barHeight}]},{i,1,Length[codesInBarcode]}],AspectRatio->Automatic]
    ],
    
    hasGuards = False; (* Since it's odd, there cannot be any guards *)
  Graphics[Table[{codesInBarcode[[i]], Rectangle[{(i-1)*barWidth, 0},{i*barWidth,barHeight}]},{i,1,Length[codesInBarcode]}],AspectRatio->Automatic]
]
] 

Update:

Here is some buggy code that I wrote with the built-in BarcodeImage. The problem with this is that it gives errors unless if it is exact, and I can not tell how to get different sized guard characters. While it accepts industry standards, it is not as flexible, at least with my small skillset.

barcodeFormats = {"Aztec", "Codabar", "Code128", "Code39", "Code93", "DataMatrix", "EAN13", "EAN8", "ITF", "PDF417", "QR", "UPC", "UPCE"};
allowedColorCombinations={
{RGBColor[0,0,0], RGBColor[1,1,1]},{RGBColor[1,0,0], RGBColor[0,1,1]},
{RGBColor[0,1,0], RGBColor[1,0,1]},{RGBColor[0,0,1], RGBColor[1,1,0]},
{RGBColor[1,1,1], RGBColor[0,0,0]},{RGBColor[0,1,1], RGBColor[1,0,0]},
{RGBColor[1,0,1], RGBColor[0,1,0]},{RGBColor[1,1,0], RGBColor[0,0,1]}
};

barCodeGenerator[format_,chosenColorCombination_] :=
Module[
  {numericList, alphaNumericList, level, barcode},
  numericList = CharacterRange["0", "9"];
  alphaNumericList = Join[CharacterRange["0", "9"], CharacterRange["A", "Z"], CharacterRange["a", "z"]];
  Switch[format,
    "Aztec" | "PDF417" | "QR",
    barcode = StringJoin[RandomChoice[alphaNumericList, RandomInteger[{8, 14}]]];
    level = RandomInteger[{0, 8}];
    ColorReplace[BarcodeImage[barcode, {format, level}],{Black->chosenColorCombination[[1]],White->chosenColorCombination[[2]]}],
     "Code93" | "Code128"  ,
    barcode = StringJoin[RandomChoice[alphaNumericList, RandomInteger[{8, 14}]]];
    ColorReplace[BarcodeImage[barcode, format],{Black->chosenColorCombination[[1]],White->chosenColorCombination[[2]]}],
 "Code39" | "Codabar"| "DataMatrix",
 barcode = StringJoin[RandomChoice[alphaNumericList, RandomInteger[{8, 14}]],RandomChoice[{"-","$",":","/","."}]];
    ColorReplace[BarcodeImage[barcode, format],{Black->chosenColorCombination[[1]],White->chosenColorCombination[[2]]}],
    "ITF",
    barcode = StringJoin[RandomChoice[numericList, RandomChoice[Table[2*i, {i, 1, 14}]]]];
    ColorReplace[BarcodeImage[barcode, format],{Black->chosenColorCombination[[1]],White->chosenColorCombination[[2]]}],
    "EAN13",
    barcode = StringJoin[RandomChoice[numericList, 13]];
    ColorReplace[BarcodeImage[barcode, format],{Black->chosenColorCombination[[1]],White->chosenColorCombination[[2]]}],
    "UPC",
    barcode = StringJoin[RandomChoice[numericList, 12]];
    ColorReplace[BarcodeImage[barcode, format],{Black->chosenColorCombination[[1]],White->chosenColorCombination[[2]]}],
    "EAN8" | "UPCE",
    barcode = StringJoin[RandomChoice[numericList, 8]];
    ColorReplace[BarcodeImage[barcode, format],{Black->chosenColorCombination[[1]],White->chosenColorCombination[[2]]}]
  ]
]

barCodeGenerator[RandomChoice[barcodeFormats],RandomChoice[allowedColorCombinations]]
 
$\endgroup$
3
  • $\begingroup$ BarcodeImage["123456789.123", "Codabar"]? $\endgroup$
    – cvgmt
    Commented Feb 11 at 1:06
  • $\begingroup$ @cvgmt The problem I have with using BarcodeImage[] is having guard characters. And maybe a little awkard for specificying if the text on the bottom is contained within the end caps, or if the first and last number when there is text is outside of the ends of the barcodes. It is easy to replace the colors though. $\endgroup$
    – Teg Louis
    Commented Feb 11 at 1:19
  • $\begingroup$ And guard characters that are larger than the rest of the bar code. $\endgroup$
    – Teg Louis
    Commented Feb 11 at 1:25

1 Answer 1

2
$\begingroup$

Edit

Clear["Global`*"];
img = BarcodeImage["123456789999", "UPC"];
data = ArrayPad[
   ImageData[img], -1]; (* remove the boundary of the imge data *)
data = data /. {1 -> 125};
v = data[[1]]; (* the first row *)
groups = Split[v]; (* split to difference groups *)
n = Length@groups;
erase = SubsetMap[ReplaceAll[{0 -> 255, 125 -> 255}], groups, 
    Complement[Range[n], Range[1, Floor[.1*n]], 
     Range[Floor[.5   n], Floor[.6   n]], 
     Range[Floor[.9   n], Floor[n]]]] // Flatten; (* according to the rates 0%,10%, 50%,60%,90% 100% *)
im = ColorReplace[
  Image[Join[data[[1 ;; -20]], ConstantArray[erase, 20]], 
   "Byte"], {Black -> Magenta, Gray -> Green}]

BarcodeRecognize[im]

123456789999.

enter image description here

Origional

  • Need to update later since I don't know how to do with the color of the gap.
Clear["Global`*"];
img = BarcodeImage["123456789.123", "Codabar"]
data = ImageData[img][[2 ;; -2]];
v = data[[1]];
group = Split[v];
n = Length@group;
erase = SubsetMap[ReplaceAll[0 -> 1], group, 
    Complement[Range[n], Range[1, Floor[.1*n]], 
     Range[Floor[.5 n], Floor[.6 n]], Range[Floor[.9 n], Floor[n]]]] //
    Flatten;
im = ColorReplace[
  Join[data[[1 ;; -20]], ConstantArray[erase, 20]] // 
   Image, {Black -> Blue}]
BarcodeRecognize[im]

enter image description here

123456789.123.

$\endgroup$
1
  • $\begingroup$ You taught me about SubsetMap[]. I will be using that a lot in the future now. $\endgroup$
    – Teg Louis
    Commented Feb 11 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.