Skip to content

Instantly share code, notes, and snippets.

@shishirthedev
Last active December 12, 2019 05:02
Show Gist options
  • Save shishirthedev/f3da4cf8544b562b2b1e114c1637071b to your computer and use it in GitHub Desktop.
Save shishirthedev/f3da4cf8544b562b2b1e114c1637071b to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity() {
private lateinit var surfaceView: SurfaceView
private lateinit var barCodeDetector: BarcodeDetector
private lateinit var cameraSource: CameraSource
private val REQUST_CAMERA_PERMISSION = 123
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onStart() {
super.onStart()
initBarCodeDetectorAndCameraSources()
}
private fun initBarCodeDetectorAndCameraSources() {
// Getting Surface View from Layout...
surfaceView = findViewById(R.id.surfaceView)
// Building Bar/QR Code Detector
barCodeDetector = BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.ALL_FORMATS)
.build()
// Building Camera source for scanning BAR?QR Code
cameraSource = CameraSource.Builder(this, barCodeDetector)
.setRequestedPreviewSize(1920, 1080) // 640, 480 || 1024, 768 || 1920, 1080
.setAutoFocusEnabled(true) // Obvious we should make auto focus enabled
.setRequestedFps(25f)
.build()
// Reading BAR/QR Code data
barCodeDetector.setProcessor(object : Detector.Processor<Barcode> {
override fun release() {
/** This method will call when we will release the barCodeDetector. And We must release the
barCodeDetector to prevent memory leaks */
}
override fun receiveDetections(detections: Detector.Detections<Barcode>?) {
val barCodes = detections?.detectedItems
barCodes?.let {
if (it.size() > 0) {
surfaceView.post {
showToast(it.valueAt(0).displayValue)
cameraSource.stop()
}
}
}
}
})
// Opening Camera When Surface is Created
surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {}
override fun surfaceDestroyed(holder: SurfaceHolder?) {
cameraSource.stop()
}
override fun surfaceCreated(holder: SurfaceHolder?) {
// Checking Camera Permission before starting camera for scanning
try {
if (ContextCompat.checkSelfPermission(
this@MainActivity,
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED
) {
cameraSource.start(holder)
} else {
// Requesting Camera Permission if Permission is Missing
ActivityCompat.requestPermissions(this@MainActivity, arrayOf(Manifest.permission.CAMERA), REQUST_CAMERA_PERMISSION)
}
} catch (e: IOException) {
}
}
})
}
// Handing Camera Permission for Scanning BAR/QR Code...
@SuppressLint("MissingPermission")
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUST_CAMERA_PERMISSION) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
cameraSource.start(surfaceView.holder)
} else showToast("Sorry, Scanner won't work without Camera Permission.")
}
}
override fun onStop() {
super.onStop()
// Releasing detector and Camera Source to prevent memory leaks
barCodeDetector.release()
cameraSource.stop()
cameraSource.release()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment