Skip to content

furkanturkn/camerax-mlkit-pack

Repository files navigation

Contributors Forks Stargazers Issues LinkedIn

CameraX ML Kit Pack

An awsome library that contains ML Kit and Camrax implementation! You don't need ZXing anymore.
Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Built With
  3. Getting Started
  4. Implementation
  5. CameraxManager functionalities
  6. Suggestions for Reader Formats

About The Project

No need to waste time on CameraX and ML Kit app anymore. This library will speed up your development 100%

Features:

  • CameraX image capturing
  • Ml-Kit barcode scanning with accuracy algorithm.
  • Flash, tap to focus, switch back-front camera.
  • Stop-start ML Kit barcode reading and camera.
  • Run without google play services

(back to top)

Built With

  • Next
  • Logo
  • CameraX
  • Kotlin coroutine

(back to top)

Getting Started

  1. Add maven-jitpack into project level gradle file.
    repositories {
         ...
         maven { url 'https://jitpack.io' }
     }
  2. Add this into dependencies which is in module level gradle file.
    dependencies {
        ...
        implementation 'com.github.furkanturkn:camerax-mlkit-pack:1.1.2'
    }

(back to top)

Implementation

  1. Get camera permission.
  2. Init CameraxManager for activity
    cameraxManager = CameraxManager.getInstance(
            this,
            null,
            previewView, 
            focusRing, //(ImageView) image that appears with focus animation when clicked on the screen.
            1 //(Int)(optional) start with FRONT[0] or BACK[1] camera. Default = BACK[1].
        )
    Init CameraxManager for fragment
    cameraxManager = CameraxManager.getInstance(
            context,
            this,
            previewView, 
            focusRing, //(ImageView) image that appears with focus animation when clicked on the screen.
            1 //(Int)(optional) start with FRONT[0] or BACK[1] camera. Default = BACK[1].
        )
  3. Destroy references.
    override fun onDestroy() {
        super.onDestroy()
        cameraxManager?.destroyReferences()
    }

(back to top)

CameraxManager functionalities

  1. How to start camera?

     cameraxManager.startCamera()
  2. How to start barcode reading?

     cameraxManager?.setReaderFormats(
             ReaderType.FORMAT_QR_CODE.value,
             ReaderType.FORMAT_EAN_8.value,
             ReaderType.FORMAT_EAN_13.value,
             .
             .
             .
         )
     cameraxManager?.startReading()
  3. How to stop reading and camera?

     cameraxManager?.stopReading()
     cameraxManager?.stopCamera()
  4. How to change flash status?

     cameraxManager.changeFlashStatus()
  5. How to change camera type (front-back)?

     cameraxManager.changeCameraType()
  6. How to capture photo?

     cameraxManager.capturePhoto()
  7. How to change accuracy level of barcode reading?

      cameraxManager.setReadingAccuracyLevel(levelOfAccuracy)

    levelOfAccuracy = 1 (Less accurate, fastest level. If you select only QR type the accuracy level is automatically set to 1.)
    levelOfAccuracy = 2 (Minimum requirement for correct barcode reading.)
    levelOfAccuracy = 3 (Recommended and default accuracy level.)

(back to top)

Suggestions for Reader Formats

For product barcodes:
  ReaderType.FORMAT_EAN_8.value
  ReaderType.FORMAT_EAN_13.value
  ReaderType.FORMAT_UPC_E.value
  ReaderType.FORMAT_UPC_A.value

For QR codes:
  ReaderType.FORMAT_QR_CODE.value

(back to top)

Callbacks example usage

  cameraxManager?.apply {
      setQrReadSuccessListener { result ->
          println("QR RESULT ----------> $result")
          tvReadResult.text = result
      }

      setFlashStatusChangedListener { status ->
          when (status) {
              FlashStatus.ENABLED -> {
                  btnFlash.setBackgroundResource(R.drawable.baseline_flash_on_24)
              }
              FlashStatus.DISABLED -> {
                  btnFlash.setBackgroundResource(R.drawable.baseline_flash_off_24)
              }
          }
      }

      setPhotoCaptureResultListener { capturedBitmap ->
          runOnUiThread {
              ivCapturePreview.setImageBitmap(capturedBitmap)
          }
      }
  }

(back to top)