Main Content

readBarcode

Detect and decode 1-D or 2-D barcode in image

Since R2020a

Description

example

msg = readBarcode(I) detects and decodes a 1-D or 2-D barcode in the input image and returns the message associated with that barcode.

If the input image contains multiple barcodes, the readBarcode function decodes only the first barcode detected.

msg = readBarcode(I,roi) specifies a rectangular region of interest (ROI) to reduce the area in which the function searches for a barcode. The ROI must be fully contained in the input image.

msg = readBarcode(___,format) specifies barcode formats to reduce the types of barcodes for which the function searches. Specify this argument in addition to any of the input argument combinations in previous syntaxes.

[msg,detectedFormat,loc] = readBarcode(___) additionally returns the format, detectedFormat, and location, loc, of the detected barcode.

Examples

collapse all

Read an image containing a barcode into the workspace.

I = imread("barcode1D.jpg");

Detect the barcode and decode its message.

msg = readBarcode(I);

Display the decoded barcode message.

disp("Decoded barcode message: " + msg)
Decoded barcode message: 1234567890128

Read an image containing a barcode into the workspace.

I = imread("barcode1D.jpg");

Search the image for a 1-D barcode, returning its message, format, and location.

[msg,detectedFormat,loc] = readBarcode(I,'1D');

Display the detected barcode format.

disp("Barcode format: " + detectedFormat)
Barcode format: EAN-13

Annotate the image with the decoded barcode message.

xyBegin = loc(1,:);
Imsg = insertText(I,xyBegin,msg,'BoxOpacity',1,'FontSize',30);

Insert a line to show the scan row.

imSize = size(Imsg);
Imsg = insertShape(Imsg,'Line',[1 xyBegin(2) imSize(2) xyBegin(2)],'LineWidth',5);

Display the image.

imshow(Imsg)

Read an image containing a barcode.

I = imread("barcodeQR.jpg");

Define the ROI in the image that contains the barcode.

roi = [470, 300, 720, 620];

Search for a QR barcode within the ROI.

[msg,~,loc] = readBarcode(I,roi,"QR-CODE");

Annotate the image with the decoded message from the detected barcode.

xyText =  loc(2,:);
Imsg = insertText(I,xyText,msg,"BoxOpacity",1,"FontSize",25);

Insert red circles onto the image to indicate the finder pattern locations.

Imsg = insertShape(Imsg, "FilledCircle", [loc, ...
     repmat(10, length(loc), 1)],"ShapeColor","red","Opacity",1);

Display the image.

imshow(Imsg)

Input Arguments

collapse all

Input image, specified as a truecolor or grayscale image.

Region of interest, specified as a four-element row vector of the form [x, y, width, height]. The rectangular ROI must be fully contained in the input image. [x,y] specifies the starting point for the ROI relative to the upper-left corner of the image.

If an image contains multiple barcodes, specifying an ROI can help the function detect a particular barcode. For more information, see Localize and Read Multiple Barcodes in Image.

Barcode format, specified as one of these options. The table lists valid barcode formats.

  • 'all' — Use this option to specify all valid barcode formats. If you do not specify a format, the function uses this option.

  • '1D' — Use this option to specify all valid 1-D barcode formats.

  • '2D' — Use this option to specify all valid 2-D barcode formats.

  • A character vector or string scalar of a valid format — Use this option to specify one barcode format.

  • A cell array of character vectors or vector of strings of valid formats — Use this option to specify multiple barcode formats. The function prioritizes its search for specific barcode formats based on the order of the elements in this array.

Specifying a format can reduce the run time of the function by restricting the barcode search.

1-D Formats2-D Formats
UPC-AQR-CODE
UPC-EDATA-MATRIX
EAN-8AZTEC
EAN-13PDF-417
CODE-39 
CODE-93 
CODE-128 
CODABAR 
ITF 
RSS-14 
RSS-EXPANDED 

Output Arguments

collapse all

Barcode message, returned as a string scalar.

Detected barcode format, returned as a string scalar of one of the formats in this table.

1-D Formats2-D Formats
UPC-AQR-CODE
UPC-EDATA-MATRIX
EAN-8AZTEC
EAN-13PDF-417
CODE-39 
CODE-93 
CODE-128 
CODABAR 
ITF 
RSS-14 
RSS-EXPANDED 

Location of barcode, returned as an M-by-2 matrix for 2-D barcodes or a 2-by-2 matrix for 1-D barcodes. The matrix elements represent finder pattern locations.

Barcodeloc ValueFinder Pattern Locations
2-DM-by-2 matrix. M represents the number of [x, y] locations of finder patterns.

1-D2-by-2 matrix of the form [x1, y1;x2, y2], where each row represents the location of a finder pattern.

Tips

  • The function detects only clearly visible barcodes.

  • Specifying a format can reduce the run time of the function by restricting the barcode search.

  • For noisy images with unclear barcodes, use image preprocessing functions, such as imsharpen.

  • The function detects only horizontally or vertically aligned barcodes. Use imrotate to correct poorly aligned barcodes.

Extended Capabilities

Version History

Introduced in R2020a