Show TOC Start of Content Area

This graphic is explained in the accompanying textUsing the Scanner Attributes  Locate the document in its SAP Library structure

In this example the scanner uses Scanner Aware Mode and scans a barcode after adding (and verifying) all the symbologies supported by the scanner. The program performs the following steps:

Caution

This example assumes that at least one driver has been installed. It also assumes this driver supports all the attributes that are being used.

...

       1.      Implements the constructor for the class. It opens a connection to the scanner in scan aware mode, if this mode is supported, and sets an event listener. If the scan aware mode is not supported, this method generates an Exception.

       2.      Implements the onError method from ScannerListener.

       3.      Implements the onDataReceived method from ScannerListener.

       4.      Declares and implements a method called scanBarcode. This method starts the scanning engine, waits for the application to read a barcode, and stops the scanning engine.

       5.      Declares and implements the method called activateAllSymbologies. This method tries to add all the symbologies supported by the API to the scanner. If a symbology cannot be added to the scanner a message is sent to the default output window.

       6.      Declares and implements the close method. It closes the connection to the scanner.

       7.      Creates an instance of the class and calls the activateAllSymbologies method.

       8.      Scans one barcode. The scanned barcode must have their symbology activated; otherwise the scanner will not read them.

       9.      Closes the connection to the scanner.

 

package scanner_api_examples;

 

import com.sap.ip.me.api.pios.PIOSException;

import com.sap.ip.me.api.pios.connection.*;

import com.sap.ip.me.api.pios.scanner.*;

import com.sap.ip.me.api.pios.symbology.*;

 

public class UsingAttributes implements ScannerListener {

   private ScannerConnection scannerConnection = null;

   private boolean barcodeScanned = false;

   private Exception lastException = null;

   private DriverInfo scanner = null;

 

   //-----------------------(1)-----------------------

   public UsingAttributes() throws PIOSException {

      Connector connector = Connector.getInstance();

      DriverInfo[] scanners = connector.listDrivers(ConnectionType.SCANNER);

      this.scanner = scanners[0];

      if (scanner

         .isAttributeSupported(

            ScannerConnection.Attributes.SCAN_AWARE_MODE)) {

         ScannerParameters parameters = new ScannerParameters(scanner);

         parameters.setMode(ScannerParameters.SCAN_AWARE);

         scannerConnection = (ScannerConnection) connector.open(parameters);

         scannerConnection.setEventListener(this);

      } else {

         throw new PIOSException("Unable to open connection to the scanner because Scan Aware Mode is not supported.");

      }

   }

 

   //-----------------------(2)-----------------------

   public void onError(ScannerException e) {

      lastException = e;

      barcodeScanned = true;

   }

 

   //-----------------------(3)-----------------------

   public void onDataReceived(ScannerData scannerData) {

      try {

         System.out.println("************");

         System.out.println(

            "* Data = " + new String(scannerData.toByteArray(), "ASCII"));

         System.out.println("************");

      } catch (Exception ex) {

         lastException = ex;

      }

      barcodeScanned = true;

   }

 

   //-----------------------(4)-----------------------

   public void scanBarcode() throws Exception {

      scannerConnection.startRead();

     

      while (!barcodeScanned) {

         Thread.sleep(500);

      }

     

      if (lastException != null) {

         throw lastException;

      }

     

      scannerConnection.endRead();

      barcodeScanned = false;

   }

 

   //-----------------------(5)-----------------------

   public void activateAllSymbologies() throws PIOSException {

      if (scanner

         .isAttributeSupported(ScannerConnection.Attributes.CODABAR)) {

         scannerConnection.addSymbology(new Codabar());

      } else {

         System.out.println(

            "Codabar symbology is not supported by the scanner.");

      }

      if (scanner

         .isAttributeSupported(ScannerConnection.Attributes.CODE39)) {

         scannerConnection.addSymbology(new Code39());

      } else {

         System.out.println(

            "Code 39 symbology is not supported by the scanner.");

      }

      if (scanner

         .isAttributeSupported(ScannerConnection.Attributes.CODE128)) {

         scannerConnection.addSymbology(new Code128());

      } else {

         System.out.println(

            "Code 128 symbology is not supported by the scanner.");

      }

      if (scanner.isAttributeSupported(ScannerConnection.Attributes.EAN13)) {

         scannerConnection.addSymbology(new EAN13());

      } else {

         System.out.println(

            "EAN-13 symbology is not supported by the scanner.");

      }

      if (scanner.isAttributeSupported(ScannerConnection.Attributes.EAN8)) {

         scannerConnection.addSymbology(new EAN8());

      } else {

         System.out.println(

            "EAN-8 symbology is not supported by the scanner.");

      }

      if (scanner

         .isAttributeSupported(

            ScannerConnection.Attributes.INTERLEAVED2OF5)) {

         scannerConnection.addSymbology(new Interleaved2Of5());

      } else {

         System.out.println(

            "Interleaved 2 of 5 symbology is not supported by the scanner.");

      }

      if (scanner

         .isAttributeSupported(ScannerConnection.Attributes.PDF417)) {

         scannerConnection.addSymbology(new PDF417());

      } else {

         System.out.println(

            "PDF-417 symbology is not supported by the scanner.");

      }

      if (scanner

         .isAttributeSupported(ScannerConnection.Attributes.UCCEAN128)) {

         scannerConnection.addSymbology(new UCCEAN128());

      } else {

         System.out.println(

            "UCC/EAN 128 symbology is not supported by the scanner.");

      }

      if (scanner.isAttributeSupported(ScannerConnection.Attributes.UPC_A)) {

         scannerConnection.addSymbology(new UPC_A());

      } else {

         System.out.println(

            "UPC-A symbology is not supported by the scanner.");

      }

 

   }

 

   //-----------------------(6)-----------------------

   public void close() {

      try {

         scannerConnection.close();

      } catch (Exception ex) {

         ex.printStackTrace();

      }

   }

 

   public static void main(String[] args) {

      UsingAttributes usingAttributesExamples = null;

 

      try {

         //-----------------------(7)-----------------------

         usingAttributesExamples = new UsingAttributes();

         usingAttributesExamples.activateAllSymbologies();

 

         //-----------------------(8)-----------------------

         usingAttributesExamples.scanBarcode();

 

      } catch (Exception ex) {

         ex.printStackTrace();

      } finally {

         //-----------------------(9)-----------------------

         usingAttributesExamples.close();

      }

   }

}

 

End of Content Area