leitor código de barras

How to create QR Reader Codes and Bar Codes in TotalCross

Barcode scanning and Qr Codes scanning are proven to be essential to companies, especially in logistics operations, warehouses and more. This method has optimized processes such as product verification, increasing speed and reducing the chances of error to zero (or nearly so). In addition to controlling the entry and exit of the product in stock. The most common equipment for this type of process are data collectors, but it is also possible to use conventional smartphones in this type of operation.

Thanks to different technologies like Zxing and Scandit, it is now possible to use QR readers and bar code for smartphones. Be it android, IOS or Windows Phone. And as always, TotalCross is not left out and so we also offer import for these libraries. So let’s go to the code!

Creating a QR Code Reader and Barcode

It is quite simple, and you can split the application into three main parts: Layout, where you will need at least one Button to call the method. The Button Event, where the method will be called, calling the camera for scanning and storing the generated reading to display to the user.

Hands On

We’ll start by creating a class called ScannerSample that inherits from MainWindow. Inside it, let’s create a constant containing the key of Scandit. We’ll call it SCANDIT_KEY.
We will need a MultiEdit and a PushButtonGroup, we will call them, respectively, from edtBarCode and pbg.

And inside the constructor, we will pass the type of border, style, and color:

public ScannerSample() {
super("Scanner Sample", NO_BORDER);
Settings.uiAdjustmentsBasedOnFontHeight = true;
setBackForeColors(Color.getRGB(255, 255, 255), Color.getRGB(50, 50, 50));

Now in initUI() we will position the elements on the screen:

public void initUI() {
   try {
      pbg = new PushButtonGroup(new String[] { "SCAN 1D ZXing",
       "SCAN 2D QR codes ZXing", "SCAN Scandit" }, fmH / 2, 3);
      add(pbg, LEFT+80, AFTER+50, FILL-100, 100);
      pbg.setBackForeColors(Color.getRGB(111, 186, 255), Color.WHITE);
      add(edtBarCode = new MultiEdit(2, 1), LEFT + 80, BOTTOM - 80, FILL - 80, PREFERRED);
      add(new Label("Result:"), LEFT + 80, BEFORE);
   } catch (Exception e) {
      MessageBox.showException(e, true); exit(0);

We will also make a Toast (To see more examples using Toast, click here):

public void showToast(String message) {
   Toast.height = fmH * 3; Toast.posY = (int) (Control.BOTTOM-80);
   Toast.show(message, 2000);

If you are not already familiar with TotalCross and have been a little lost in this part, just click aqui to learn and train more.

And onEvent()…

It is in the Button event, as we have already said, that there is the process of calling the method, saving the returned value and displaying it later, or redirecting if it is a URL.

public void onEvent(Event event) {
   switch (event.type) {
      case ControlEvent.PRESSED:
         if (event.target == pbg) {
          int sel = pbg.getSelectedIndex();
          String scan;
         if (sel == 2) {
          scan = Scanner.readBarcode("scandit:" + SCANDIT_KEY);
          String mode = sel == 0 ? "1D" : sel == 1 ? "2D" : "";
          scan = Scanner.readBarcode("mode=" + mode + "&msg="
          + "Place a barcode inside the viewfinder rectangle to scan it");
         if (scan != null){
          Vm.exec("url", scan, 0, true);
          showToast("No barcode to read!");

And your Bar Code Reader and QR Codes are ready!
To see the complete example, just go to our Github.

With TotalCross and now with Scandit it is much easier to implement this functionality, which has been proving the increasingly necessary to the market.

Was the article useful? Did you learn? Do not forget to share with friends! And if you have any questions, just let us know.

Esse artigo foi útil?
[Total: 1 Average: 4]
Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Email this to someone