Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.apptreesoftware.barcodescan
- import android.Manifest
- import android.app.Activity
- import android.content.Intent
- import android.content.pm.PackageManager
- import android.os.Bundle
- import android.support.v4.app.ActivityCompat
- import android.support.v4.content.ContextCompat
- import android.view.Menu
- import android.view.MenuItem
- import com.google.zxing.Result
- import me.dm7.barcodescanner.zxing.ZXingScannerView
- class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
- lateinit var scannerView: me.dm7.barcodescanner.zxing.ZXingScannerView
- companion object {
- val REQUEST_TAKE_PHOTO_CAMERA_PERMISSION = 100
- }
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- title = ""
- scannerView = ZXingScannerView(this)
- scannerView.setAutoFocus(true)
- setContentView(scannerView)
- }
- override fun onResume() {
- super.onResume()
- scannerView.setResultHandler(this)
- // start camera immediately if permission is already given
- if (!requestCameraAccessIfNecessary()) {
- scannerView.startCamera()
- }
- }
- override fun onPause() {
- super.onPause()
- scannerView.stopCamera()
- }
- override fun handleResult(result: Result?) {
- val intent = Intent()
- intent.putExtra("SCAN_RESULT", result.toString())
- setResult(Activity.RESULT_OK, intent)
- finish()
- }
- fun finishWithError(errorCode: String) {
- val intent = Intent()
- intent.putExtra("ERROR_CODE", errorCode)
- setResult(Activity.RESULT_CANCELED, intent)
- finish()
- }
- private fun requestCameraAccessIfNecessary(): Boolean {
- val array = arrayOf(Manifest.permission.CAMERA)
- if (ContextCompat
- .checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(this, array,
- REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
- return true
- }
- return false
- }
- override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,grantResults: IntArray) {
- when (requestCode) {
- REQUEST_TAKE_PHOTO_CAMERA_PERMISSION -> {
- if (PermissionUtil.verifyPermissions(grantResults)) {
- scannerView.startCamera()
- } else {
- finishWithError("PERMISSION_NOT_GRANTED")
- }
- }
- else -> {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults)
- }
- }
- }
- }
- object PermissionUtil {
- /**
- * Check that all given permissions have been granted by verifying that each entry in the
- * given array is of the value [PackageManager.PERMISSION_GRANTED].
- * @see Activity.onRequestPermissionsResult
- */
- fun verifyPermissions(grantResults: IntArray): Boolean {
- // At least one result must be checked.
- if (grantResults.size < 1) {
- return false
- }
- // Verify that each required permission has been granted, otherwise return false.
- for (result in grantResults) {
- if (result != PackageManager.PERMISSION_GRANTED) {
- return false
- }
- }
- return true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement