Advertisement
BankUpload Join!

Saturday, February 7, 2015

Barcode Scanner on Android and iOS - Read and produce Barcode with Delphi XE7

In a previous blog post , we saw how it is possible to control third-party applications via Intent(Barcode Scanner) to easily implement the functionality of reading (and production) of a bar code from your Delphi application (XE5) for Android. 
I decided, after some requests received via email, to publish this update that takes advantage of the support (introduced in version XE6 and illustrated by Brian Long in his blog post ) to handle the event onActivityResult . 
Motivations

In the old version of this example (for XE5), in fact, sfruttavo the system clipboard and event management application to understand when, once started Barcode Scanner in read mode, the user had actually concluded scanning a barcode. 
Thanks to the fact that Barcode Scanner scanned copy the code to the clipboard, so I could get in my application Delphi string with the text corresponding to the bar code. 


The approach, a bit 'primitive, was due to the fact that the Intent was launched calling startActivity(a kind of ShellExecute in the Android world) which does not provide a notification mechanism launched at the end of the Activity (in our case, at the end scanning a bar code or when the user cancels the operation). 
New approach

From XE6 on, instead, you can call startActivityForResult that launches the Activity and asks to be notified of the outcome of the Activity (a bit 'as ShellExecuteEx allows you to wait until the end of the process launched in the Windows world). This notification is via the messaging system of RTL Delphi (System.Messaging unit) and then makes it easy to record our method(HandleActivityMessage in my code) as message-listener a specific message(TMessageResultNotification, here's the list of messages supported ) and run our application code at the appropriate time and with a lot of access to any and all output parameters Activity performed (in our case, for example, the value and the type of bar code scanned). 
The code for the subscribe TMessageResultNotification and method HandleActivityMessage

The management of onActivityResult, with the extraction of SCAN_RESULT

Scanning result: code and type

Conclusions and material

Certainly, compared to the version XE5: 

  • not likely interference in the management of the clipboard or if other applications are started (it was enough for the arrival of a call and the previous mechanism stopped working);
  • you get access to the parameters "accessories" as the type of bar code scanned effectively.

Moreover, this approach is general and can be used with any application and with any Activity / Intent supported (not just so for Barcode Scanner!) 

Link: Source Code Delphi XE7 
Link: Demo APK (to be installed on your Android device) 

Good luck and see you soon, 

No comments:

Post a Comment