So, I have a function called sillyBarcodeMaker[]
. It generates images like:
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]]
BarcodeImage["123456789.123", "Codabar"]
? $\endgroup$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$