Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
Alistair Sykes edited this page Jun 17, 2021 · 6 revisions

Getting Started

Download

Add the dependency to your build.gradle

implementation 'uk.co.brightec.kbarcode:kbarcode:$version'

For the latest version see releases or bintray

If you wish to use Android Architecture Components, see https://developer.android.com/topic/libraries/architecture for installation details.

Step by Step

Step 1 - Firebase

You will need to have Firebase setup in your project. See Firebase documentation for a guide to this.

Step 2 - BarcodeView

Add a BarcodeView into your layout

<uk.co.brightec.kbarcode.BarcodeView
  android:id="@+id/view_barcode"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />

You can use the available XML attributes to customise the behaviour. See reference for details on these.

Step 3 - Lifecycle

Add the BarcodeView as an observer to your lifecycle in onCreate()

lifecycle.addObserver(barcodeView)

Step 4 - Observer

Add an observer to the BarcodeView LiveData

barcodeView.barcodes.observe(viewLifecycleOwner, Observer { barcodes ->
  // ...
})

A good place for this is just below the line adding a lifecycle observer

Programmatically

You can also add the BarcodeView programmatically

barcodeView = BarcodeView(this)
barcodeView.setOptions(
  Options.Builder()
    .cameraFacing(CameraCharacteristics.LENS_FACING_BACK)
    .barcodeFormats(
      intArrayOf(
        Barcode.FORMAT_CODABAR, Barcode.FORMAT_EAN_13, Barcode.FORMAT_EAN_8,
        Barcode.FORMAT_ITF, Barcode.FORMAT_UPC_A, Barcode.FORMAT_UPC_E
      )
    )
    .barcodesSort(null)
    .previewScaleType(BarcodeView.CENTER_INSIDE)
    .build()
)
frameContainer.addView(barcodeView)

ProGuard

Depending on your usage/configuration you may need to add this to your proguard-rules.pro:

-keep class uk.co.brightec.kbarcode.** { *; }

Permissions

You will need to handle camera permissions within your application. Checkout this section of the wiki.