Skip to content
Barcode Scanner Library by Google Mobile Vision Api with RxJava
Kotlin Java
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.
app gradle update Apr 11, 2019
.gitignore init Mar 16, 2018
LICENSE Update Feb 25, 2019
build.gradle gradle update Apr 11, 2019
gradlew init Mar 16, 2018

SimpleBarcodeScanner Android Arsenal

Barcode Scanner by Google Mobile Vision Api with RxJava

Getting Started

Setting up the dependency

The first step is to include SimpleBarcodeScanner into your project, as a Gradle dependency:

Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		maven { url '' }

Add the dependency:

implementation 'com.github.bobekos:SimpleBarcodeScanner:x.x.xx'


Include following code in your layout:


For all supported attributes see the list below

Include followin code in your activity or fragment

class MainActivity : AppCompatActivity() {

    private var mDisposable: Disposable? = null

    override fun onCreate(savedInstanceState: Bundle?) {

    override fun onStart() {

        //make sure to request camera permission before the subscription

        mDisposable = barcodeView
                        { barcode ->
                            //handle barcode object
                        { throwable ->
                            //handle exceptions like no available camera for selected facing

    override fun onStop() {



Available Options


//in xml

Which barcode format should be detected. Default value is all formats.


//in xml

Set the camera facing. Default value is back facing.


//in xml

Turn on the flash. Default value is false. (Also changeable after the subscription)


//in xml

Enable autofocus. Default value is true.

.setPreviewSize(width, height)

Set preview size for the camera source. The given preview size is calculated to the closet value from camera available sizes. Default values are the display dimensions.


Draw a overlay view over the detected barcode. Default overlay is a white rect.


//in xml

Play Beep sound at barcode detection. Default value is true. (Also changeable after the subscription)


//in xml

Vibrate at barcode detection. Default value is 500ms. (Also changeable after the subscription)

Advanced Options


Source Google:

Indicates whether the detector has all of the required dependencies available locally in order to do detection.

When an app is first installed, it may be necessary to download required files. If this returns false, those files are not yet available. Usually this download is taken care of at application install time, but this is not guaranteed. In some cases the download may have been delayed.

By default this case is handled automatic by this library. If you want to handle this case by yourself, make sure to set this function:


If this function is set, the DetectorNotReadyException will be thrown in onError if isOperational function of the detector return false.

Custom overlay

There a already two implemented overlay views (BarcodeRectOverlay and BarcodeTextOverlay). To create your own overlay, you only need to implement the "BarcodeOverlay" interface to your custom view. The method "onUpdate" passes the position of the barcode on the screen and its value. An own view looks like this, for example:

class RedRectOverlay : View, BarcodeOverlay {


    private lateinit var rect: Rect

    private val paint by lazy {
        Paint().apply {
            color = Color.RED
            style = Paint.Style.STROKE
            strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3f, context?.resources?.displayMetrics)

    init {

    override fun onUpdate(posRect: Rect, barcodeValue: String) {
        rect = posRect

    override fun onDraw(canvas: Canvas?) {

        if (::rect.isInitialized) {
            canvas?.drawRect(rect, paint)

// Activity or Fragment

If the barcode detection failed or finished the "onUpdate" method passed empty values for the position and barcode value.

CameraPreview release

The default behavior stops the barcode detection and released the camera when the subscription is disposed. Sometimes you want to hold the camera preview also after dispose. Mostly if you use for example single or maybe which disposed directly.This function guarantees that the camera preview is not released. In all cases the camera will be released when the surface gets destroyed.



You have full control of the observable which is returned from the BarcodeView. Nevertheless, I have prepared a few examples to show what is possible.

//filter the detected results

.filter { barcode ->
    barcode.displayValue == "12345"
//skip results until the raw value changed

.distinctUntilChanged { barcode1, barcode2 ->
	barcode1.rawValue == barcode2.rawValue
//get only first item

// combine your api/database/etc. observables directly

.flatMap { barcode ->
.flatMap ...

and many more...

On which thread does the detection run?

The detection runs on an background thread. Don't forget to set the correct scheduler to the "observeOn" method of the observable if you want to have the result on the main android thread for example.

It is necessary to dispose the subscription on his own?

Short answer 'yes'. Although the observable called "onComplete" when the surface is destroyed. But to avoid memory leaks you should always dispose your subscription.

Resources and Credits


Copyright 2018 Bobek Bobekos

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and 
limitations under the License.
You can’t perform that action at this time.