'How to use camera2 with detect barcode

Camera is now deprecated and the recommend is to use camera2, can some one guide me to have any idea how to use camera2 with detect barcode



Solution 1:[1]

There are 2 possibilities here

  1. Create the barcode scanner from scratch (which is complicated)
  2. Use existing libraries (which this answer covers)

There are some well established libraries out there that you can use

Additionally, you can refer to this question to gain more insight on using Camera2 with Mobile Vision.

Solution 2:[2]

Yes, sure ! You have to use BarcodeDetector. Something like this :

open class BarcodeFragmentInternal : Fragment(), ActivityCompat.OnRequestPermissionsResultCallback {

    protected var mSurfaceView: SurfaceView? = null

    protected var mScanListener: Listener? = null

    private var mCameraSource: CameraSource? = null

    override fun onStart() {
        super.onStart()

        // Initialize Barcode Detector
        // Used to recognize barcodes like {@link Barcode.DATA_MATRIX} and {@link Barcode.QR_CODE}
        val mBarcodeDetector = BarcodeDetector.Builder(activity)
                .setBarcodeFormats(Barcode.DATA_MATRIX or Barcode.QR_CODE)
                .build()
        if (!mBarcodeDetector.isOperational) {
            sendError("NO_BARCODE_DETECTOR")
        }

        mBarcodeDetector.setProcessor(object : Detector.Processor<Barcode> {
            override fun release() {

            }

            override fun receiveDetections(detections: Detector.Detections<Barcode>?) {
                if (detections != null && mScanListener != null) {
                    val detectedItems = detections.detectedItems
                    if (detectedItems != null) {
                        var i = 0
                        val size = detectedItems.size()
                        while (i < size) {
                            val barcode = detectedItems.valueAt(i)
                            mScanListener!!.onQRCodeAvailable(barcode.rawValue)
                            i++
                        }
                    }
                }
            }
        })

        mCameraSource = CameraSource.Builder(context!!, mBarcodeDetector).build()

        mSurfaceView!!.holder.addCallback(object : SurfaceHolder.Callback {
            override fun surfaceCreated(surfaceHolder: SurfaceHolder) {
                try {
                    if (ContextCompat.checkSelfPermission(activity!!, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        requestCameraPermission()
                        return
                    }
                    mCameraSource!!.start(mSurfaceView!!.holder)
                } catch (e: Exception) {
                    sendError("NO_CAMERA_ACCESS")
                }

            }

            override fun surfaceChanged(surfaceHolder: SurfaceHolder, format: Int, width: Int, height: Int) {

            }

            override fun surfaceDestroyed(surfaceHolder: SurfaceHolder) {
                mCameraSource!!.stop()
            }
        })
    }


    private fun requestCameraPermission() {
        if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
            ConfirmationDialog().show(childFragmentManager, FRAGMENT_DIALOG)
        } else {
            requestPermissions(arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
                                            grantResults: IntArray) {
        if (requestCode == REQUEST_CAMERA_PERMISSION) {
            if (grantResults.size != 1 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {

                sendError("NOT_AUTHORIZED")

                view!!.post {
                    ErrorDialog.newInstance(getString(R.string.request_permission))
                            .show(childFragmentManager, FRAGMENT_DIALOG)
                }
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Suhel Chakraborty
Solution 2 Bruno