Skip to content

TTMSFNCWXBarcodeDecoder

The TTMSFNCWXBarcodeDecoder component decodes 1D barcodes from still images. Images can be supplied as a file, a Base64 string, or a TTMSFNCBitmap. The component can also be connected to a TTMSFNCWXCamera for continuous live decoding. This component uses the quagga2 library.

Notice

With the default settings this component requires internet connection. If you'd like to run the component in an offline environment, please change the LibraryLocation property. Read more about this at the Loading modes paragraph of this documentation.

Supported barcode types

Set BarcodeType to one of the following values before decoding:

Value Symbology
bdtCode128 Code 128 (default)
bdtEAN EAN-13
bdtEAN8 EAN-8
bdtCode39 Code 39
bdtCode39Vin Code 39 VIN
bdtCodabar Codabar
bdtUPC UPC-A
bdtUPCE UPC-E
bdtI2Of5 Interleaved 2 of 5
bdt2Of5 Standard 2 of 5
bdtCode93 Code 93
bdtCode32 Code 32 (Italian Pharmacode)

Decoding a single image

procedure TForm1.Button1Click(Sender: TObject);
begin
  // From file (not available for WEB):
  TMSFNCWXBarcodeDecoder1.BarcodeType := bdtEAN;
  TMSFNCWXBarcodeDecoder1.DecodeFromFile('C:\barcodes\sample.png');
end;

procedure TForm1.TMSFNCWXBarcodeDecoder1Decoded(Sender: TObject;
  AFound: Boolean; AResult: string);
begin
  if AFound then
    ShowMessage('Barcode: ' + AResult)
  else
    ShowMessage('No barcode found.');
end;

Decoding from a Base64 image

TMSFNCWXBarcodeDecoder1.DecodeFromBase64(myBase64ImageString);

Decoding from a bitmap

TMSFNCWXBarcodeDecoder1.DecodeFromImage(MyTMSFNCBitmap);

Connect with TTMSFNCWXCamera

Assign a TTMSFNCWXCamera to the Camera property to start continuous live decoding. The decoder takes snapshots internally and calls OnDecoded for each frame.

procedure TForm1.FormCreate(Sender: TObject);
begin
  TMSFNCWXBarcodeDecoder1.BarcodeType := bdtCode128;
  TMSFNCWXBarcodeDecoder1.Camera := TMSFNCWXCamera1;
  TMSFNCWXCamera1.Start;
end;

procedure TForm1.TMSFNCWXBarcodeDecoder1Decoded(Sender: TObject;
  AFound: Boolean; AResult: string);
begin
  if AFound then
  begin
    Memo1.Lines.Add('Scanned: ' + AResult);
    TMSFNCWXCamera1.Stop;
  end;
end;

Improve decoding

When decoding via the camera, decoding happens by analyzing continuous snapshots. If the success rate is low (especially on mobile), try the following:

  1. Resolution — Increase TTMSFNCWXCamera.Resolution.IdealWidth and IdealHeight (default is 640x480). The ideal resolution refers to a resolution that cannot be guaranteed (e.g. the selected camera module does not support it), however the underlying API will try to select one as close to it as possible. Higher resolution can improve barcode detection but uses more memory.
  2. Lighting and framing — Ensure the barcode is fully visible and well-lit. Dark or blurry images reduce accuracy.
  3. Camera module — On Android, the default back-facing camera is often a wide-angle lens which can distort barcodes. Use RequestDevices and SelectDevice to choose a different camera. See Camera device selection.

Decoding multiple barcode types simultaneously

TTMSFNCWXBarcodeDecoder can only decode one barcode type per instance. To decode multiple types at once, use multiple decoder instances — one per type — without assigning a camera to any of them. Drive all decoders manually from the camera's OnSnapshot event and use Boolean flags to avoid requesting a new snapshot before all decoders finish.

// Camera start
procedure TForm1.Button1Click(Sender: TObject);
begin
  TMSFNCWXCamera1.Start;
end;

// Camera OnStart: take the first snapshot
procedure TForm1.TMSFNCWXCamera1Start(Sender: TObject);
begin
  TMSFNCWXCamera1.TakeSnapshot;
end;

// Camera OnSnapshot: feed the image to all decoders
procedure TForm1.TMSFNCWXCamera1Snapshot(Sender: TObject; ABitmap: TTMSFNCBitmap);
begin
  FStopScan    := False;
  FEan13Done   := False;
  FEan8Done    := False;
  ean13Decoder.DecodeFromImage(ABitmap);
  ean8Decoder.DecodeFromImage(ABitmap);
end;

// EAN-13 decoder result
procedure TForm1.ean13DecoderDecoded(Sender: TObject; AFound: Boolean; AResult: string);
begin
  if not FStopScan then
  begin
    if AFound then
    begin
      Memo1.Lines.Add('EAN13: ' + AResult);
      FStopScan := True;
      TMSFNCWXCamera1.Stop;
    end;
    FEan13Done := True;
    if FEan8Done and not FStopScan then
      TMSFNCWXCamera1.TakeSnapshot;  // All decoders done; get next frame
  end;
end;

// EAN-8 decoder result (mirror of above)
procedure TForm1.ean8DecoderDecoded(Sender: TObject; AFound: Boolean; AResult: string);
begin
  if not FStopScan then
  begin
    if AFound then
    begin
      Memo1.Lines.Add('EAN8: ' + AResult);
      FStopScan := True;
      TMSFNCWXCamera1.Stop;
    end;
    FEan8Done := True;
    if FEan13Done and not FStopScan then
      TMSFNCWXCamera1.TakeSnapshot;
  end;
end;

Loading modes (LibraryLocation)

Most of the WX components offer the option to load the JavaScript library it depends on either online or offline. This option can be set via the LibraryLocation property. The available options means the following:

  • llOnline: The JavaScript library will be loaded live from an external URL. This allows a faster loading of the library, decreasing the loading time of the component and providing a better performance. However, this option also requires an active internet connection.
  • llOffline: The JavaScript library will be loaded from resources. This guarantees the component is going to work in offline mode, but performance can decrease, especially on less powerful devices.

When you are ready to deploy your application, consider the differences above and make a choice based on them.

Properties

Property Type Default Description
BarcodeType TTMSFNCWXBarcodeDecoderType bdtCode128 The barcode symbology to decode. See the table above.
Camera TTMSFNCWXCamera nil Assign a camera component for live continuous decoding.
LibraryLocation TTMSFNCWXLibraryLocation llOnline Controls whether quagga2 is loaded from CDN or embedded resources.

Methods

Method Description
DecodeFromBase64(ABase64; ACallback) Decodes a Base64-encoded image. ACallback is called when processing is complete.
DecodeFromFile(AFileName; ACallback) Decodes a file. Not available for WEB. ACallback is called when processing is complete.
DecodeFromImage(ABitmap; ACallback) Decodes a TTMSFNCBitmap. ACallback is called when processing is complete.

Events

Event Parameters Description
OnDecoded AFound: Boolean; AResult: string Fires each time an image is processed. AFound is True if a barcode was decoded. AResult contains the decoded value.