Skip to main content

QR Code and Barcode Detection

API reference for detecting and decoding QR codes, barcodes, and other graphical codes.

GraphicalCodeDetector

Base class for graphical code detection and decoding.

Methods

detect

Detects graphical code in image and returns the quadrangle containing the code.
img
InputArray
Grayscale or color (BGR) image containing (or not) graphical code
points
OutputArray
Output vector of vertices of the minimum-area quadrangle containing the code
Returns: true if a code is detected

decode

Decodes graphical code once it’s found by the detect() method.
img
InputArray
Grayscale or color (BGR) image containing graphical code
points
InputArray
Quadrangle vertices found by detect() method
straight_code
OutputArray
Optional output image containing binarized code
Returns: UTF8-encoded output string or empty string if the code cannot be decoded

detectAndDecode

Both detects and decodes graphical code.
img
InputArray
Grayscale or color (BGR) image containing graphical code
points
OutputArray
Optional output array of vertices of the found graphical code quadrangle
straight_code
OutputArray
Optional output image containing binarized code
Returns: Decoded string or empty string if not found

detectMulti

Detects multiple graphical codes in image.
img
InputArray
Grayscale or color (BGR) image
points
OutputArray
Output vector of vector of vertices of the minimum-area quadrangles containing the codes
Returns: true if at least one code is detected

decodeMulti

Decodes multiple graphical codes.
img
InputArray
Grayscale or color (BGR) image containing graphical codes
points
InputArray
Vector of quadrangle vertices found by detect() method
decoded_info
std::vector<std::string>&
UTF8-encoded output vector of strings or empty vector if codes cannot be decoded
straight_code
OutputArrayOfArrays
Optional output vector of images containing binarized codes
Returns: true if at least one code is decoded

detectAndDecodeMulti

Both detects and decodes multiple graphical codes.
decoded_info
std::vector<std::string>&
UTF8-encoded output vector of strings or empty vector if codes cannot be decoded
If there are QR codes encoded with Structured Append mode and all are detected and decoded correctly, the method writes the full message to the position corresponding to the 0-th code in the sequence. The rest of the QR codes from the same sequence have empty strings.

QRCodeDetector

QR code detector and decoder.

Constructor

Methods

setEpsX

Sets the epsilon used during horizontal scan of QR code stop marker detection.
epsX
double
Epsilon neighborhood for determining the horizontal pattern of the scheme 1:1:3:1:1 according to QR code standard
Returns: Reference to this QRCodeDetector

setEpsY

Sets the epsilon used during vertical scan of QR code stop marker detection.
epsY
double
Epsilon neighborhood for determining the vertical pattern of the scheme 1:1:3:1:1 according to QR code standard
Returns: Reference to this QRCodeDetector

setUseAlignmentMarkers

Enables or disables use of alignment markers to improve corner position.
useAlignmentMarkers
bool
Flag to enable alignment markers (enabled by default)
Returns: Reference to this QRCodeDetector

decodeCurved

Decodes QR code on a curved surface once found by detect().
img
InputArray
Grayscale or color (BGR) image containing QR code
points
InputArray
Quadrangle vertices found by detect() method
straight_qrcode
OutputArray
Optional output image containing rectified and binarized QR code
Returns: UTF8-encoded output string or empty string if the code cannot be decoded

detectAndDecodeCurved

Both detects and decodes QR code on a curved surface.

getEncoding

Returns the encoding type for the decoded info from the latest decode call.
codeIdx
int
default:"0"
Index of the previously decoded QR code. For single code detection, use 0.
Returns: Encoding type (e.g., ECI_UTF8, ECI_SHIFT_JIS)

Example Usage


QRCodeEncoder

QR code encoder for generating QR codes.

Constructor

Use the static create() method.

Enums

EncodeMode

CorrectionLevel

ECIEncodings

Params Structure

Methods

encode

Generates QR code from input string.
encoded_info
String
Input string to encode
qrcode
OutputArray
Generated QR code image

encodeStructuredAppend

Generates QR code in Structured Append mode, splitting the message over multiple QR codes.
encoded_info
String
Input string to encode
qrcodes
OutputArrayOfArrays
Vector of generated QR code images

Example Usage


BarcodeDetector

Barcode detector and decoder. Inherits from GraphicalCodeDetector.

Constructor

prototxt_path
std::string
Prototxt file path for the super resolution model (optional)
model_path
std::string
Model file path for the super resolution model (optional)

Methods

decodeWithType

Decodes barcode in image once found by detect().
img
InputArray
Grayscale or color (BGR) image containing barcode
points
InputArray
Vector of rotated rectangle vertices found by detect(). Order: bottomLeft, topLeft, topRight, bottomRight.
decoded_info
std::vector<std::string>&
UTF8-encoded output vector of strings or empty if codes cannot be decoded
decoded_type
std::vector<std::string>&
Vector of strings specifying the type of barcodes (e.g., “EAN_13”, “CODE_128”)
Returns: true if at least one valid barcode is found

detectAndDecodeWithType

Both detects and decodes barcode.
img
InputArray
Grayscale or color (BGR) image containing barcode
decoded_info
std::vector<std::string>&
UTF8-encoded output vector of strings
decoded_type
std::vector<std::string>&
Vector of strings specifying the barcode types
points
OutputArray
Optional output vector of vertices of the found barcode rectangle
Returns: true if at least one valid barcode is found

setDownsamplingThreshold

Sets detector downsampling threshold.
thresh
double
Downsampling limit to apply (default 512). The detect method resizes the input image to this limit if the smallest dimension is greater than the threshold.
Returns: Reference to this BarcodeDetector

getDownsamplingThreshold

Gets detector downsampling threshold.
Returns: Current downsampling threshold

setDetectorScales

Sets detector box filter sizes.
sizes
std::vector<float>
Box filter sizes relative to minimum dimension of the image (default [0.01, 0.03, 0.06, 0.08])
Filter sizes directly correlate with the expected line widths for a barcode. If the downsampling limit is increased, filter sizes need to be adjusted inversely.
Returns: Reference to this BarcodeDetector

getDetectorScales

Gets detector box filter sizes.
sizes
std::vector<float>&
Output parameter for returning the sizes

setGradientThreshold

Sets detector gradient magnitude threshold.
thresh
double
Gradient magnitude threshold (default 64). Values between 16 and 1024 generally work.
Returns: Reference to this BarcodeDetector

getGradientThreshold

Gets detector gradient magnitude threshold.
Returns: Current gradient threshold

Example Usage

Supported Barcode Types

The BarcodeDetector supports various 1D and 2D barcode formats:
  • EAN-8, EAN-13
  • UPC-A, UPC-E
  • Code 39, Code 93, Code 128
  • ITF (Interleaved 2 of 5)
  • Codabar
  • QR Code
  • DataMatrix
  • PDF417

See Also