Tutorial: barcode scanning

Uwe Kubosch edited this page Dec 24, 2013 · 15 revisions

Our goal

We want to scan barcodes and display their content.

We will be using ZXing and ZXingLib as an Android utility project:

Prerequisites

  • You should have completed the Getting started with Ruboto.
  • You need a device with camera connected with USB debugging.
  • Internet access to download the jar.

This tutorial has been tested with the following setups:

ruboto RubotoCore ZXing ZXingLib Device Android Android SDK Tester
1.0.0 0.5.6 2.3.0 1.0 Samsung Galaxy S3 4.1.2 22.3 donv

Setup environment

Our app will use Android 4.1, api level 16, and the zxinglib uses api level 13.

ruboto setup -y -t 13 -t 15

Create your project

ruboto gen app -t 15 --package org.ruboto.example.barcode_scanning
cd barcode_scanning

Add the ZXing core jar to your project

Download https://android-zxinglib.googlecode.com/files/android-zxinglib-1.0.zip, unpack it and copy android-zxinglib-1.0/ZXingLib/libs/core.jar to the libs directory in your project.

Add the zxinglib utility project

Copy the android-zxinglib-1.0/ZXingLib directory besides your project.

Link the ZXingLib utility project by adding this line to your project.properties

android.library.reference.1=../ZXingLib

Add the primary activity

Edit src/barcode_scanning_activity.rb to this:

require 'ruboto/widget'
require 'ruboto/util/toast'

ruboto_import_widgets :Button, :LinearLayout, :TextView

# http://code.google.com/p/zxing/
# http://code.google.com/p/android-zxinglib/
import 'jim.h.common.android.lib.zxing.config.ZXingLibConfig'
import 'jim.h.common.android.lib.zxing.integrator.IntentIntegrator'
import 'jim.h.common.android.lib.zxing.integrator.IntentResult'

class BarcodeScanningActivity
  def onCreate(bundle)
    super
    set_title 'Domo arigato, Mr Ruboto!'

    @zxing_config = ZXingLibConfig.new
    @use_flash = true

    self.content_view =
        linear_layout :orientation => :vertical do
          @scan_result = text_view :text => 'Awaiting scan...', :id => 42, 
                                 :layout => {:width => :match_parent},
                                 :gravity => :center, :text_size => 48.0
          button :text => 'Barcode scan', :layout => {:width => :match_parent},
                 :on_click_listener => proc { scan_barcode }
        end
  rescue Exception
    puts "Exception creating activity: #{$!}"
    puts $!.backtrace.join("\n")
  end

  def onCreateOptionsMenu(menu)
    puts 'onCreateOptionsMenu'
    
    menu.add('Use flash').set_checkable(true).set_checked(@use_flash).setOnMenuItemClickListener do |mi|
      mi.set_checked(@use_flash = !@use_flash)
      true
    end

    menu.add('Exit').setOnMenuItemClickListener do
      @exit_requested = true
      finish
      true
    end

    true
  end

  def onActivityResult(request_code, result_code, data)
    puts 'onActivityResult'
    super
    if request_code == IntentIntegrator::REQUEST_CODE
      scan_result = IntentIntegrator.parseActivityResult(request_code, result_code, data)
      return unless scan_result

      if scan_content = scan_result.contents
        run_on_ui_thread { @scan_result.text = scan_content }
      end
    end
  end

  private

  def scan_barcode
    @zxing_config.useFlash = @use_flash
    IntentIntegrator.initiateScan(self, @zxing_config)
  end

end

Add Activity, Feature, and Permission declarations to the manifest

Edit AndroidManifest.xml.

Add this activity declaration inside the applicationtag:

<activity android:name="jim.h.common.android.lib.zxing.CaptureActivity"
    android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:windowSoftInputMode="stateAlwaysHidden" />        

Add these feature and permission declarations just below the uses-sdktag:

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />

Go!

Update the scripts and restart the application:

rake install start

You should see a progress dialog and then a button that will start the barcode scanning.

Enjoy!

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.