Advertisement
Guest User

Untitled

a guest
May 20th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. package com.apptreesoftware.barcodescan
  2.  
  3. import android.Manifest
  4. import android.app.Activity
  5. import android.content.Intent
  6. import android.content.pm.PackageManager
  7. import android.os.Bundle
  8. import android.support.v4.app.ActivityCompat
  9. import android.support.v4.content.ContextCompat
  10. import android.view.Menu
  11. import android.view.MenuItem
  12. import com.google.zxing.Result
  13. import me.dm7.barcodescanner.zxing.ZXingScannerView
  14.  
  15.  
  16. class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
  17.  
  18. lateinit var scannerView: me.dm7.barcodescanner.zxing.ZXingScannerView
  19.  
  20. companion object {
  21. val REQUEST_TAKE_PHOTO_CAMERA_PERMISSION = 100
  22.  
  23. }
  24.  
  25. override fun onCreate(savedInstanceState: Bundle?) {
  26. super.onCreate(savedInstanceState)
  27. title = ""
  28. scannerView = ZXingScannerView(this)
  29. scannerView.setAutoFocus(true)
  30. setContentView(scannerView)
  31. }
  32.  
  33. override fun onResume() {
  34. super.onResume()
  35. scannerView.setResultHandler(this)
  36. // start camera immediately if permission is already given
  37. if (!requestCameraAccessIfNecessary()) {
  38. scannerView.startCamera()
  39. }
  40. }
  41.  
  42. override fun onPause() {
  43. super.onPause()
  44. scannerView.stopCamera()
  45. }
  46.  
  47. override fun handleResult(result: Result?) {
  48. val intent = Intent()
  49. intent.putExtra("SCAN_RESULT", result.toString())
  50. setResult(Activity.RESULT_OK, intent)
  51. finish()
  52. }
  53.  
  54. fun finishWithError(errorCode: String) {
  55. val intent = Intent()
  56. intent.putExtra("ERROR_CODE", errorCode)
  57. setResult(Activity.RESULT_CANCELED, intent)
  58. finish()
  59. }
  60.  
  61. private fun requestCameraAccessIfNecessary(): Boolean {
  62. val array = arrayOf(Manifest.permission.CAMERA)
  63. if (ContextCompat
  64. .checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
  65.  
  66. ActivityCompat.requestPermissions(this, array,
  67. REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
  68. return true
  69. }
  70. return false
  71. }
  72.  
  73. override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,grantResults: IntArray) {
  74. when (requestCode) {
  75. REQUEST_TAKE_PHOTO_CAMERA_PERMISSION -> {
  76. if (PermissionUtil.verifyPermissions(grantResults)) {
  77. scannerView.startCamera()
  78. } else {
  79. finishWithError("PERMISSION_NOT_GRANTED")
  80. }
  81. }
  82. else -> {
  83. super.onRequestPermissionsResult(requestCode, permissions, grantResults)
  84. }
  85. }
  86. }
  87. }
  88.  
  89. object PermissionUtil {
  90.  
  91. /**
  92. * Check that all given permissions have been granted by verifying that each entry in the
  93. * given array is of the value [PackageManager.PERMISSION_GRANTED].
  94.  
  95. * @see Activity.onRequestPermissionsResult
  96. */
  97. fun verifyPermissions(grantResults: IntArray): Boolean {
  98. // At least one result must be checked.
  99. if (grantResults.size < 1) {
  100. return false
  101. }
  102.  
  103. // Verify that each required permission has been granted, otherwise return false.
  104. for (result in grantResults) {
  105. if (result != PackageManager.PERMISSION_GRANTED) {
  106. return false
  107. }
  108. }
  109. return true
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement