Cameleer

Barcode data format

The barcode data format turns text into a barcode image (QR Code, Data Matrix, PDF417, Aztec, Code 128, EAN and more) on marshal, and reads the text back from a barcode image on unmarshal.

Name: barcode · Crate: cameleer-dataformats · Cargo feature: dataformats · Apache Camel: Barcode

When to use it

  • Print QR codes or Data Matrix codes on shipping labels, tickets or asset tags straight from a route (for example with a link to the order).
  • Read barcodes from scanned documents or camera images on an edge device (a warehouse gateway, a kiosk) without a cloud vision service.
  • Let an AI agent attach a scannable code to what it produces, such as a QR code with a tracking URL in a generated email.

Marshal and unmarshal

directioninput bodyoutput body
marshaltextimage bytes (PNG by default, also GIF or JPG)
unmarshala PNG, GIF or baseline JPEG imagethe decoded text; headers BarcodeFormat and, for QR codes, ERROR_CORRECTION_LEVEL
unmarshal with multiple=truean image with several symbolsan array of {text, BarcodeFormat, ...} objects; headers BarcodeFormat (array) and BarcodeCount

Symbologies (barcodeFormat):

formatmarshal (encode)unmarshal (decode)
QR_CODE (default)yes, versions 1-40, error correction L/M/Q/Hyes
DATA_MATRIXyes, square and rectangularyes
PDF_417yes, error correction 0-8yes
AZTECyesyes
CODE_128, CODE_39, CODE_93, CODABAR, ITFyesyes
EAN_13, EAN_8, UPC_A, UPC_Eyes (check digit computed or verified)yes
MAXICODE, UPC_EAN_EXTENSION, RSS_14, RSS_EXPANDEDno (ZXing has no writer either)yes

With the default barcodeFormat (QR_CODE), unmarshal tries every format. Setting another format restricts decoding to it. The codecs are written from scratch (no ZXing) but follow ZXing's rendering and option names, and decode photographs (rotation, perspective, noise) as well as clean generated images.

Example

Make a QR code for a tracking link, then read it back. A real route would write the PNG to a file or attach it to an email instead of decoding it.

yaml example: barcode-qr-round-trip
- route:
    id: barcode-qr
    from:
      uri: direct:barcode-qr
      steps:
        - marshal:
            barcode:
              barcodeFormat: QR_CODE
              errorCorrectionLevel: M
              width: 200
              height: 200
        - setHeader:
            name: pngSignature
            simple: "${body.substring(1, 4)}"
        - unmarshal:
            barcode: {}
        - setBody:
            simple: "${header.pngSignature} ${header.BarcodeFormat} ${header.ERROR_CORRECTION_LEVEL}: ${body}"
text
https://example.com/track/ORD-1042
text
PNG QR_CODE M: https://example.com/track/ORD-1042

A linear barcode for a product label:

yaml example: barcode-ean13
- route:
    id: barcode-ean
    from:
      uri: direct:barcode-ean
      steps:
        - marshal:
            barcode:
              barcodeFormat: EAN_13
              width: 300
              height: 100
        - unmarshal:
            barcode:
              barcodeFormat: EAN_13
        - setBody:
            simple: "${header.BarcodeFormat} ${body}"
text
590123412345
text
EAN_13 5901234123457

The 12 digits get their check digit (7) computed on marshal.

Rust DSL

rust
b.from("direct:barcode-rust")
    .marshal(DataFormatDefinition::new("barcode").option("barcodeFormat", "DATA_MATRIX").option("imageType", "PNG"))
    .unmarshal(DataFormatDefinition::new("barcode").option("barcodeFormat", "DATA_MATRIX"));

Options

optiondefaultdescription
barcodeFormatQR_CODEThe symbology (see the table above).
width100Image width in pixels. The symbol is scaled by the largest whole factor that fits, with a quiet zone.
height100Image height in pixels.
imageTypePNGPNG, GIF or JPG.
errorCorrectionLevelL (QR), 2 (PDF417)QR: L, M, Q, H. PDF417: 0 to 8.
dataMatrixShapeNONEData Matrix shape: NONE, SQUARE or RECTANGLE. Alias shape.
pdf417CompactionAUTOPDF417 compaction: AUTO, TEXT, BYTE, NUMERIC. Alias compaction.
multiplefalseUnmarshal every symbol in the image into a list.

Message headers

headerdirectiondescription
BarcodeFormatout (unmarshal)The format found, for example QR_CODE; an array with multiple=true.
ERROR_CORRECTION_LEVELout (unmarshal)QR error correction level of the symbol read.
BarcodeCountout (unmarshal)Number of symbols with multiple=true.

Common questions

How do I generate a QR code PNG in a route?

marshal: barcode: {width: 300, height: 300} on a text body, then to: file:labels with a CamelFileName ending in .png.

How do I read a barcode from an uploaded image?

unmarshal: barcode: {} on the image bytes. Every format is tried when barcodeFormat is not set; the format found is in the BarcodeFormat header.

Which error correction level should I use for QR codes?

L (the default) gives the smallest code. Use M or Q for printed labels that may get scratched, H when a logo covers part of the code.

Found a problem?

If something on this page doesn't match what Cameleer does, or you hit a bug while building with it, please help fix it, whether you are a person or a coding agent: open an issue that describes what you saw, then fork the repository, fix it, add or update a test, run cargo test --workspace, and open a pull request. CONTRIBUTING.md explains the ground rules.

Edit this page on GitHub