Barcode Reader SDK for Android. Usage

This page will demonstrate how to read barcodes withih your android java application using SD-TOOLKITŪ Barcode Reader SDK for Android.

Installing the SDK

show screenshots

  1. Download the setup, unzip it to the local folder on your development computer. For example C:\SDKs\SDTBarcodeSDKForAndroid. In further descriptions this folder will be referred as [SDT_BARCODE_ANDROID_SDK_ROOT]. The setup does not require activation however activation of valid developer license is required on your development android device(s).
  2. Once you are satisfied with the SDK you have to purchase Either the Individual Developer License or Enterprise Developer License.
  3. On your development android device install the SD-TOOLKITŪ Barcode SDK for Android Licensing Activation Tool from Play Store (former Android Market). Either browse to the following URL http://play.google.com/store/apps/details?id=sdt.brc.lic from your device or search for sdt.brc.lic in the Play Store search window. Once Installed, use it to activate your purchased Developer License.

There are two ways how to use the SDK within Android application. First and more generic way is to use BarcodeReader class instance. It allow to recognize Barcode from RGB image buffer for example obtained from loaded JPEG image file. To read barcodes directly from camera use BarcodeScanDialog. This is a UI control which use callback notification mechanism to report about newelly recognized Barcode Symbols.

Reading barcodes from image file using android Bitmap object

This is a piece of code demonstrate how to recognize barcode from JPEG file stored on SD card.

        //Obtain file stored on SD Card
        String sdcardPath = Environment.getExternalStorageDirectory().toString();
        String imageFullPath = sdcardPath +  "/test/Ean13.jpg";

        //Read and decode JPEG image
        Bitmap bitmap = BitmapFactory.decodeFile(imageFullPath);
        
        //Allocate Buffer for storing RGB image data
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int rgbBuffer[] = new int[width * height];
        
        //Finally obtain RGB pixels.
        bitmap.getPixels(rgbBuffer, 0, width, 0, 0, width, height);

        //Initialize Barcode reader. Pass valid developer key as a second parameter
        BarcodeReader barcodeReader = new BarcodeReader(getApplicationContext(), "Developer license key must be to here.");
        barcodeReader.setReadInputTypes(BarcodeReader.SDTBARCODE_ALL_1D);
        barcodeReader.readRGBBufferInt(rgbBuffer, width, height) ;
        if(barcodeReader.getResultsCount() > 0) {
            //Read first recognized symbol
            BarcodeReaderResult result = barcodeReader.getResultAt(0);
            Toast.makeText(getApplicationContext(), result.getValue(), Toast.LENGTH_SHORT);
        }
				

Read Barcodes from Android camera inside your java application.

The described below sample demonstrate usage of BarcodeScanDialog class to read barcodes directly from android device camera. The application source codes are located in [SDT_BARCODE_ANDROID_SDK_ROOT]\Samples\sdt-brc-android-sample folder.

show screenshots

  1. In Eclipse create a new Android project
  2. In the Project Properties dialog go to Java Build Path -> Libraries tab and press Add External JARs... button. Browse to the SD-TOOLKITŪ Android JAR file: [SDT_BARCODE_ANDROID_SDK_ROOT]\sdt-brc-android.jar.
    Ensure that checkbox is checked in tab Order and Export for the sdt-brc-android.jar
  3. Modify project's layout\main.xml by adding TextView, ImageView and Button controls.
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TextView  
        android:id="@+id/resultsLabel"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Results:"
        />
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/resultsValues"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        </LinearLayout>
        <ImageView
        android:src="@drawable/icon"  
        android:id="@+id/resultsImage"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:text=""
        android:layout_weight="1"/>
        <Button android:text="SCAN" android:id="@+id/readBarcode" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    						
  4. Add CAMERA permission to the project manifest file before <applicaton> tag .
        <uses-permission android:name="android.permission.CAMERA" />
    						
  5. Copy [SDT_BARCODE_ANDROID_SDK_ROOT]\libs folder to the project's root. Refresh the project.
  6. Add the following members to the SDTBarcodeSampleActivity class:
        /** Barcode Read  Dialog */
        BarcodeScanDialog mCamDlg = null;
    
        /** UI */
        Button mShowCamBut = null;
    							
  7. Initialize the button within SDTBarcodeSampleActivity.onCreate() method
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            mShowCamBut = (Button)findViewById(R.id.readBarcode);
            if(mShowCamBut != null) {
                mShowCamBut.setOnClickListener(new OnClickListener() {
    
                    public void onClick(View v) {
                        showCameraDlg();
                    }
                });
            }
        }
    							
  8. Override SDTBarcodeSampleActivity.onStop() method. The onStop Method must hide the BarcodeScanDialog otherwise the Camera device will be locked.
        @Override
        protected void onStop() {
            hideCameraDialog();
            super.onStop();
        }
    							
  9. Add method SDTBarcodeSampleActivity.showCameraDlg() to show the Barcode scan dialog. The method perform the following procedures.
    1. Creates instance of BarcodeScanDialog and pass a valid license key to the constructor.
    2. Informs the BarcodeScanDialog which types of barcodes the application is interested in (BarcodeReader.SDTBARCODE_ALL_1D)
    3. Assign the callback which will be called each time the BarcodeScanDialog recognize barcode on camera stream. The callback store the result with image from camera for future demostration in main window and hide Barcode scan dialog.
        protected void showCameraDlg() {
    
            mCamDlg = new BarcodeScanDialog(this, "Purchased License key");
    
            if(mCamDlg != null) {
    
                mCamDlg.setBarcodeTypes(BarcodeReader.SDTBARCODE_ALL_1D);
    
                mCamDlg.setRecognitionListener(new BarcodeScanDialog.OnRecognitionlListener() {
    
                    public void onRecognitionResults(List results, YuvImage srcImage) {
                        // Populate results
                        LinearLayout resultsLayout = (LinearLayout)findViewById(R.id.resultsValues);
                        if(resultsLayout != null) {
                            resultsLayout.removeAllViews();
    
                            for (BarcodeReaderResult barcodeReaderResult : (List)results) {
                                String value =  "[" + barcodeReaderResult.getTypeName() + "]" + barcodeReaderResult.getValue() + "\n";
                                TextView tv = new TextView(getApplicationContext());
                                if(tv != null) {
                                    tv.setText(value);
                                    resultsLayout.addView(tv);
                                }
    
                            }
    
                        }
    
                        //Hide scan dialog
                        mCamDlg.hide();
    
                        //Show the image
                        if(srcImage != null) {
                            ImageView iv = (ImageView)findViewById(R.id.resultsImage);
                            if(iv != null) {
                                Bitmap bm = BarcodeReaderUtil.decodeImageToBitmap(srcImage);
                                iv.setImageBitmap(bm);
                            }
                        }
    
                    }
                });
    
                mCamDlg.show();
            }
        }
    							
  10. Add method showCameraDlg.hideCameraDialog() to hide the Barcode scan dialog.
        protected void hideCameraDialog() {
            //It is important to hide Scan preview dialog in order to release locked Camera resource.
            if( mCamDlg != null ) {
                mCamDlg.hide();
            }
        }
    							
  11. Debug the project as Android Application.