Barcode Capturing with Delphi XE4 iOS App!

Posted by on in Blogs
Hello Everyone!

See in this article how to read and decode barcodes through an Delphi XE4 iOS App. The process consists in capturing an image using the device camera and decoding it through a specialized SDK.

CodeBar Libraries for iOS Apps


There are a lot of SDKs focused in barcode decoding, supporting numerous platforms, some paid, some free, with different levels of features, just go to Google and you'll receive a huge list of suggestions to explore. Here, we are interested in SDKs for the iOS platform.

In general, these SDKs are created to be used with Objective-C, so we need to translate the SDK's header to Pascal through one of the available ways. Here you can find more information about the translation process:

http://alturl.com/v2wtx

http://alturl.com/y87xb

To create this sample, I'm using the ZBar SDK (http://zbar.sourceforge.net/), an open source library, which has its header translated to Pascal by Simon Choi, originally published here: http://blog.naver.com/simonsayz/120175561755.

Developing the App


Once you have the SDK (in this case represented by a file called "libzbar.a") and the header translation in  hands, you can create a Delphi class to expose the SDK methods in a easy and friendly way to your iOS app. See below how this class looks:
  TZBarCode = class(TObject)
  private
    ZBarView: ZBarReaderView;
    ZBarEvent: TZBarReaderViewDelegate;
    FActive: Boolean;
    function GetActive: Boolean;
    procedure SetActive(value: Boolean);
    function GetOnBarCode: TOnBarCode;
    procedure SetOnBarCode(value: TOnBarCode);
  protected
    destructor Destroy; override;
  public
    constructor Create; virtual;
    procedure Free;
    procedure SetFrame(View: UIView; Frame: CGRect);
    property Active: Boolean Read GetActive Write SetActive;
    property OnBarCode: TOnBarCode Read GetOnBarCode Write SetOnBarCode;
  end;

Building the Visual Interface


Considering the visual part of the application, we have:

- An TEdit (edtResult) to receive the barcode data;

- An TMemo (memImage) to define the image display area;

- An TListBox (lstHistory) to store the capture log;

- An TSwitch (swtONOFF) to activate/deactivate the capture process;

- An TButton (btnCopy) to copy the barcode data to the clipboard;

- An TButton (btnClear) to clear the capture log;

You can see the expected form appearance in this image:


Adding Some Code


Start by adding - in the private section of the main application form - one field to instantiate our capture class and a method to be assigned to the capture event:
  private
    { Private declarations }
    ZBarCode: TZBarCode;
    procedure OnFindBarCode(Sender: TObject; BarCode: String);
  public

Looking to the OnFindBarCode event, the BarCode variable brings the captured value, so you just need to assign it to the edtResult, as well to the lstHistory to create the log:
procedure TMainForm.OnFindBarCode(Sender: TObject; BarCode: String);
begin
  edtResult.Text := BarCode;
  lstHistory.Items.Add(FormatDateTime('dd/mm/yyyy hh:nn:ss', Now) + ' - ' + BarCode);
end;

Using the OnSwitch event from TSwitch, we'll instantiate the capture's class, assign the capture event, as well as define the image plotting area, and finally start the capture:
procedure TMainForm.swtONOFFSwitch(Sender: TObject);
begin
  if not Assigned(ZBarCode) then
  begin
    ZBarCode := TZBarCode.Create;
    ZBarCode.OnBarCode := OnFindBarCode;
    ZBarCode.setFrame(WindowHandleToPlatform(Self.Handle).View,
      CGRectMake(memImage.Position.X, memImage.Position.Y, memImage.Width,
      memImage.Height));
  end;
  ZBarCode.Active := swtONOFF.IsChecked;
end;

To complete the app, here is the code for the two added buttons:
procedure TMainForm.butClearClick(Sender: TObject);
begin
  edtResult.Text := '';
  lstHistory.Items.Clear;
end;

procedure TMainForm.btnCopyClick(Sender: TObject);
begin
  edtResult.SelectAll;
  edtResult.CopyToClipboard;
end;

Note: This app is intended to run only in a real device. Here you can see the app in action:



You can download the sample with the complete source code here: http://cc.embarcadero.com/item/29485

Hope this can be useful for your projects, have fun!


Comments

  • Guest
    Anders Ohlsson Sunday, 30 June 2013

    Very cool!

  • Guest
    Roland Sunday, 30 June 2013

    Very very cool. Exactly what I need.
    Thanks a lot.

  • Guest
    Oliver Monday, 1 July 2013

    Hi , thanks this is exactly what I have been looking for.

    Just a question, I am a newbie , when I try run it on the iOS simulator I get [DCC Error] libzbar.pas(27): E1026 File not found: 'libiconv.dylib'

    I have tried updating my SDK's etc but cannot get it to work. Is there something I am missing ?

  • Guest
    Oliver Monday, 1 July 2013

    Sorry about my message above, just with regard to the question, I assume the answer is because it can only run on a real device, so the simulator will fail when compiling.

  • Guest
    Fernando Rizzato Monday, 1 July 2013

    Exactly Oliver!

  • Guest
    Emrah KUCUKALI Thursday, 4 July 2013

    This library can read QRCode?

  • Guest
    David I Friday, 5 July 2013

    Fernando - would be great to have this functionality as a reusable component :)

  • Guest
    brent Wednesday, 10 July 2013

    How do I get rid of the image that shows in the Memo once the TZBarCode.Active := False??

    I've tried to Free it

  • Guest
    René Hjorth Tuesday, 13 August 2013

    Thanks a lot!

    It would, however, be quite cool to also have the symbology returned as a part of the OnBarCode event?

  • Guest
    Lewis Harris Thursday, 15 August 2013

    Have you ported the barcode scanner app to android?

  • Guest
    Paulo Tenório Thursday, 15 August 2013

    I tested the example and could not capture the barcode. The apliocativo activates the camera displays the image but does not convert. What can it be?

  • Guest
    Heida Pierpaolo Friday, 23 August 2013

    Really thanks a lot!

    I have tested your example and all works fine.

  • Guest

    [...] Fernando Rizzato has written a blog about how you can read bar codes using Delphi XE4 for iOS. [...]

  • Guest
    Rodrigo Friday, 20 September 2013

    Que tal um exemplo com o XE5?

  • Guest
    Landerson Gomes Monday, 23 September 2013

    Fernando bom dia.
    Você teria algo sobre essa leitura de códigos de barras vinda sendo feita no Delphi XE5 e sendo feita pelo Android?

  • Guest
    Douglas Tuesday, 24 September 2013

    Este post sem dúvida nenhuma me ajudou muito no XE4, seguindo a linha dos amigos acima, ter uma rotina dessa rodando no XE5 com Android seria lindo! rss

  • Guest
    Naim Jain Sunday, 29 September 2013

    Hi,

    Just wondering can the code be ported to android? I'm currently trying to design an android bar-code reader but failed. I was able to execute the ZXing through intent call but unable to obtain the result which is in the onActivityResult. Do you have any idea to overcome this? Thank you in advance. :)

  • Guest
    Murilo Sunday, 3 November 2013

    Opa, muito bom, mais também não consegui compilar por falta de libiconv.dylib, como compilar?

    Obrigado!

  • Guest
    Fernando Sunday, 3 November 2013

    Murilo, compile para o "device". Esta biblioteca não vai funcionar para o simulador...

  • Guest
    Barcode printer Thursday, 14 November 2013

    Hello guys are you looking http://www.duralabel.net/products.html" rel="nofollow">Barcode printer in India. Then please try Dura label Graphic Pvt Ltd they provide Best quality Stickers and labels at very low cost. They provide every type of labels and stickers according to the business such as Paper label, filmic label, heat resistance label, http://www.duralabel.net/prod9.html" rel="nofollow">Thermal ribbon.

  • Please login first in order for you to submit comments

Check out more tips and tricks in this development video: