Promena Transformer - barcode detector - ZXing & OpenCV
This transformer provides functionality to detect barcodes in a application/pdf
document using ZXing 3.4.0 and OpenCV 4.1.2.
Visit Promena#Transformer to understand the repository structure.
The solution was developed thanks to the following resources:
- https://www.pyimagesearch.com/2014/11/24/detecting-barcodes-images-python-opencv/
- https://stackoverflow.com/a/50519348
Algorithm (for one page)
- Convert to grayscale image with 300 DPI
- Compute the Scharr gradient magnitude representation
- Subtract the y-gradient from the x-gradient
- Apply the binary threshold using values from
linearThresholdValue
andlinearThresholdMaxValue
parameters - Construct and apply the closing kernel using values from
linearKernelSizeWidth
andlinearKernelSizeHeight
parameters - Apply erosions and dilations using values from
linearErosionsIterations
andlinearDilationsIterations
parameters - Determine bounding boxes
- Find the convex hull of each bounding box
- If
linearAdditionalVerticalTransformation
is true, swaplinearKernelSizeWidth
parameter withlinearKernelSizeHeight
parameter and repeat steps 2-8 - Cut out found contours to separate images
- Calculate tilt of each image and rotation angles to rotate the image perpendicularly
- After rotation apply additional rotations using value from
linearRotationThresholdDegrees
parameter - Try to detect a barcode on the image using formats from
formats
parameter - Repeat steps 2-13 for the set of parameters with
matrix
prefix - Filter found barcodes using value from
regexFilter
parameter
Transformation ZxingOpenCvBarcodeDetectorDsl
, ZxingOpenCvBarcodeDetectorParametersDsl
The DataDescriptor
has to contain at least one descriptor. If more than one descriptor is passed, the transformation will be performed on each of them separately.
Support ZxingOpenCvBarcodeDetectorSupport
Media type ZxingOpenCvBarcodeDetectorSupport.MediaTypeSupport
application/pdf; UTF-8
➡️ application/pdf; UTF-8
Parameters ZxingOpenCvBarcodeDetectorSupport.ParametersSupport
Take a look at Algorithm (for one page) to understand the meaning of given parameter.
formats
,List<String>
,["Codabar", "UPC-A", "UPC-E", "EAN-8", "EAN-13", "Code 39", "Code 128", "ITF", "RSS-14", "RSS Expanded", "QR Code", "PDF417", "Aztec Code", "Data Matrix", "MaxiCode"]
optionalregexFilter
,String
, optionallinearRotationThresholdDegrees
,Int
,<1, 360>
, optionallinearAdditionalVerticalTransformation
,Boolean
, optionallinearThresholdValue
,Double
,<1.0, 255.0>
, optionallinearThresholdMaxVal
,Double
,<1.0, 255.0>
, optionallinearKernelSizeWidth
,Double
,<1.0, 1000.0>
, optionallinearKernelSizeHeight
,Double
,<1.0, 1000.0>
, optionallinearErosionsIterations
,Int
,<1, 1000>
, optionallinearDilationsIterations
,Int
,<1, 1000>
, optionalmatrixRotationThresholdDegrees
,Int
,<1, 360>
, optionalmatrixAdditionalVerticalTransformation
,Boolean
, optionalmatrixThresholdValue
,Double
,<1.0, 255.0>
, optionalmatrixThresholdMaxVal
,Double
,<1.0, 255.0>
, optionalmatrixKernelSizeWidth
,Double
,<1.0, 1000.0>
, optionalmatrixKernelSizeHeight
,Double
,<1.0, 1000.0>
, optionalmatrixErosionsIterations
,Int
,<1, 1000>
, optionalmatrixDilationsIterations
,Int
,<1, 1000>
, optional
Metadata
Visit to find out how to build and process barcode metadata.
Dependency
<dependency>
<groupId>pl.beone.promena.transformer</groupId>
<artifactId>barcode-detector-zxing-opencv-configuration</artifactId>
<version>1.0.1</version>
</dependency>
promena-docker-maven-plugin
<dependency>
<groupId>pl.beone.promena.transformer</groupId>
<artifactId>barcode-detector-zxing-opencv</artifactId>
<version>1.0.1</version>
</dependency>
Properties
The default values are adjusted to recognise barcodes on scanned documents.
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.ZxingOpenCvBarcodeDetectorTransformer.priority=1
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.ZxingOpenCvBarcodeDetectorTransformer.actors=1
# RSS-14, RSS Expanded, UPC-E produces many false-positive detections
# UPC-A, EAN-8, EAN-13 also produces false-positive detections but less than previous ones
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.formats=Codabar,Code 39,Code 128,ITF,QR Code,PDF417,Aztec Code,Data Matrix,MaxiCode
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.regex-filter=
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.linear-rotation-threshold-degrees=1
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.linear-additional-vertical-transformation=true
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.linear-threshold-value=150.0
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.linear-threshold-max-val=255.0
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.linear-kernel-size-width=25.0
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.linear-kernel-size-height=1.0
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.linear-erosions-iterations=20
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.linear-dilations-iterations=20
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.matrix-rotation-threshold-degrees=1
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.matrix-additional-vertical-transformation=false
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.matrix-threshold-value=100.0
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.matrix-threshold-max-val=255.0
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.matrix-kernel-size-width=20.0
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.matrix-kernel-size-height=20.0
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.matrix-erosions-iterations=20
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.matrix-dilations-iterations=20
transformer.pl.beone.promena.transformer.barcodedetector.zxingopencv.default.parameters.timeout=
Evaluator
If you want to adjust parameters, run Main
from barcode-detector-zxing-opencv-evaluator
module (you have to install OpenCV 4.1.2 on your computer).