Skip to content

TTMSFNCWXBarcodeDecoder

The TTMSFNCWXBarcodeDecoder component allows decoding multiple types of barcodes from images. 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.

Decode multiple barcode types

The TTMSFNCWXBarcodeDecoder can only decode one barcode at a time. To decode multiple types, use as many TTMSFNCWXBarcodeDecoder components as the amount of types needed to be decoded but do not assign a camera to them. Instead start taking snapshots from the camera as soon as it starts and feed the snapshot image to all decoders. Use Boolean flags make sure to not take a new snapshot before all decoders finished their work. Below is an example of decoding 2 different types (EAN13 and EAN8) simultaniously.

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

//TTMSFNCWXBarcodeDecoder OnDecoded event (for EAN13):
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;
    FEan13Finished := True;

    //Here you can extend the condition further with 
    //the other flags from the other decoders:
    if FEan8Finished and not FStopScan then 
      TMSFNCWXCamera1.TakeSnapshot;
  end;
end;

//TTMSFNCWXBarcodeDecoder OnDecoded event (for EAN8):
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;
    FEan8Finished := True;

    //Again, here the condition can be extended to
    //accomodate for more decoders
    if FEan13Finished and not FStopScan then
      TMSFNCWXCamera1.TakeSnapshot;
  end;
end;

//Do the implementation for theother decoders similarly

//TTMSFNCWXCamera.OnSnapshot event:
procedure TForm1.TMSFNCWXCamera1Snapshot(Sender: TObject;
  ABitmap: TTMSFNCBitmap);
begin
  FStopScan := False;
  FEan8Finished := False;
  FEan13Finished := False;
  //Set the rest of the decoder flags to False

  ean8Decoder.DecodeFromImage(ABitmap);
  ean13Decoder.DecodeFromImage(ABitmap);
  //Call DecodeFromImage(ABitmap) on the rest of the decoders
end;

//TTMSFNCWXCamera.OnStart event:
procedure TForm1.TMSFNCWXCamera1Start(Sender: TObject);
begin
  TMSFNCWXCamera1.TakeSnapshot;
end;

Connect with TTMSFNCWXCamera

The TTMSFNCWXBarcodeDecoder supports decoding barcodes when connected to a TTMSFNCWXCamera. Simply assign the TTMSFNCWXCamera to the Camera property.

Improve decoding

When decoding via the camera, the decoding happens by taking continuous snapshots and analyizing them. In most cases the default settings don't need any change and they are sufficient to handle the decoding. However, there are some instances (mostly observed on mobile platforms) where the decoding success rate is low. Check the following to improve it:

  1. Resolution: The ideal resolution can be set via the TTMSFNCWXCamera.Resolution property. We are referring to ideal resolution because the resolution 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. The default ideal resolution is set to 640x480. In many cases this is enough, however there might be instances where increasing the resolution will result in better decoding success. It's a good first step to experiment with in case the decoding success rate is low.
  2. Environment: Make sure the barcode is fully visible and sufficiently lit as dark images might reduce the success of the library.
  3. Default camera module: Mobile devices nowdays come with multiple camera modules. On Android we've observed that the default back facing module that is selected by the browser is usually the wide lens camera module. Images taken with wide lens might introduce distortion that makes the barcode hard to decode. Try selecting a different camera device from the available list, but keep in mind this feature is only available when there is internet connection. Click here for more information.

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 guarantess 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 name Description
BarcodeType Set the barcode type to read. By default bdtCode128 is selected.
Camera Assign a TTMSFNCWXCamera to this property to read barcode from the camera.

Methods

Property name Description
DecodeFromBase64(ABase64; ACallback = nil) Decode from a base64 encoded image. If ACallback is assigned it’s called after processing the image.
DecodeFromFile(AFileName; ACallback = nil) Decode from a file. Not available for WEB. If ACallback is assigned it’s called after processing the image.
DecodeFromImage(ABitmap; ACallback = nil) Decode from a TTMSFNCBitmap. If ACallback is assigned it’s called after processing the image.

Events

Property name Description
OnDecoded Event triggered each time the image is processed. Check the AFound Boolean parameter to see if the reading was successful. The AResult string parameter contains the decoded result.