6

In my app, developed with XE7 for Android/iOS, I have a form for scanning barcodes. Upon a found barcode, my app validates whether it is an acceptable barcode or not. Following tutorials here: http://www.fmxexpress.com/qr-code-scanner-source-code-for-delphi-xe5-firemonkey-on-android-and-ios/

Currently I am testing on Android and I am able to integrate scanning and reading of barcodes, but the 'onBarCode' event does not fire when returned from the shared Activity of finding the barcode. Same code worked well with previous versions of Rad Studio ( XE4, XE5, XE6) but now in XE7 it does not.

Here are some snippets of code:

...
begin
    Scanner := TAndroidBarcodeScanner.Create(true);
    Scanner.OnBarCode := BarcodeHandler;
    Scanner.Scan;
end;

procedure TmScannerForm.BarcodeHandler(Sender: TAndroidBarcodeScanner;
  BarCode: String);
begin
  text1.Text := Barcode;
  memo1.PasteFromClipboard;
  AddBarcode(BarCode, true);
end;

AddBarCode is the even I used to validate and add barcode to a list, but I didnt include it, because that code isn't the problem - it's not even triggering. The Text1.text:=Barcode and memo1.paseFromClipboard were in their for validating the even wasn't firing too. I can confirm the barcodes are being read because if I tap and manually paste, the barcode shows.

Why is this not working on XE7 as it did in previous versions of Rad Studio?

mjn
  • 36,362
  • 28
  • 176
  • 378
ThisGuy
  • 1,405
  • 1
  • 24
  • 51
  • Which Android barcode reader is it? (the class name TAndroidBarcodeReader does not indicate if it is zbar or zxing based) – mjn Oct 08 '14 at 16:02
  • http://www.fmxexpress.com/wp-content/uploads/2014/03/TKRBarCodeSanner.zip is the third link where it lists resources for the article to get going on the scanning – ThisGuy Oct 08 '14 at 16:20
  • The TTKRBarCodeScanner class in the zip uses `com.google.zxing.client.android.SCAN`, so I assume it is ZXing based. Actually you could use the lines shown in my answer instead of the component / class TAndroidBarcodeScanner (which seems to be in the 20.3 MB download at http://cc.embarcadero.com/Download.aspx?id=29699 if I understand correctly) – mjn Oct 08 '14 at 16:25

4 Answers4

6

Andrea Magni has a more elegant solution than the timer on his blog based on event handling.

I would comment to send the link but I don't have enough reputation. The link to his blog is :

http://blog.delphiedintorni.it/2014/10/leggere-e-produrre-barcode-con-delphi.html

Maybe this can help you. The blog is in Italian though but the sources are provided and are explaining by themselves.

etherel
  • 216
  • 1
  • 3
2

There is a source code fragment on http://john.whitham.me.uk/xe5/ which looks useable (based on Zxing):

 intent := tjintent.Create;     
 intent.setAction(stringtojstring('com.google.zxing.client.android.SCAN'));
 sharedactivity.startActivityForResult(intent,0);

The code in the linked article also shows how to receive the Intent result. (I don't work with Delphi on Android so I am not sure if that part uses a best practice - TTKRBarCodeScanner uses a workaround with a Timer and the Clipboard).

I would try this as an alternative to see if works.

mjn
  • 36,362
  • 28
  • 176
  • 378
  • Thank you for the responses. +1 for the timer suggestion - with a timer is works. My question was directed towards more specifically, WHY the used unit won't work now on XE7, but if I don't get a direct answer then I will reward you the bounty. Thanks ! – ThisGuy Oct 09 '14 at 20:03
1

This code works to me!

Set timer enabled to true when you run your scan code

procedure Tform.Timer1Timer(Sender: TObject);
begin
if  (ClipService.GetClipboard.ToString <> '')  then
  begin
     timer1.Enabled:=false;
      zSearch.Text := ClipService.GetClipboard.ToString;
     //Do what you need
  end;

end;
Gianluca Colombo
  • 717
  • 17
  • 38
1

This code to me works fine!

in andorid.BarcodeScanner

function TAndroidBarcodeScanner.HandleAppEvent(AAppEvent: TApplicationEvent;
  AContext: TObject): Boolean;
var
  aeBecameActive : TApplicationEvent;
begin
  aeBecameActive := TApplicationEvent.BecameActive;
  if FMonitorClipboard and (AAppEvent = aeBecameActive) then
  begin
    GetBarcodeValue;
  end;
end;