|
| 1 | +package io.scanbot.example.sdk.barcode |
| 2 | + |
| 3 | +import android.Manifest |
| 4 | +import android.content.pm.PackageManager |
| 5 | +import android.os.Bundle |
| 6 | +import android.view.LayoutInflater |
| 7 | +import android.view.View |
| 8 | +import android.view.ViewGroup |
| 9 | +import android.widget.ImageView |
| 10 | +import android.widget.TextView |
| 11 | +import android.widget.Toast |
| 12 | +import androidx.appcompat.app.AppCompatActivity |
| 13 | +import androidx.core.app.ActivityCompat |
| 14 | +import androidx.core.content.ContextCompat |
| 15 | +import androidx.core.view.WindowCompat |
| 16 | +import androidx.recyclerview.widget.LinearLayoutManager |
| 17 | +import androidx.recyclerview.widget.RecyclerView |
| 18 | +import io.scanbot.example.sdk.barcode.model.BarcodeTypeRepository |
| 19 | +import io.scanbot.sdk.SdkLicenseError |
| 20 | +import io.scanbot.sdk.barcode.BarcodeDetectorFrameHandler |
| 21 | +import io.scanbot.sdk.barcode.entity.BarcodeItem |
| 22 | +import io.scanbot.sdk.barcode.entity.BarcodeScanningResult |
| 23 | +import io.scanbot.sdk.barcode_scanner.ScanbotBarcodeScannerSDK |
| 24 | +import io.scanbot.sdk.camera.FrameHandlerResult |
| 25 | +import io.scanbot.sdk.camera.ScanbotCameraView |
| 26 | + |
| 27 | +class BatchQRScanActivity : AppCompatActivity(), BarcodeDetectorFrameHandler.ResultHandler { |
| 28 | + |
| 29 | + private lateinit var cameraView: ScanbotCameraView |
| 30 | + private lateinit var resultView: RecyclerView |
| 31 | + private lateinit var flash: View |
| 32 | + private var flashEnabled = false |
| 33 | + private var barcodeDetectorFrameHandler: BarcodeDetectorFrameHandler? = null |
| 34 | + private val resultAdapter by lazy { ResultAdapter(layoutInflater) } |
| 35 | + |
| 36 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 37 | + supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY) |
| 38 | + super.onCreate(savedInstanceState) |
| 39 | + setContentView(R.layout.activity_batch_qr_camera_view) |
| 40 | + |
| 41 | + cameraView = findViewById(R.id.camera) |
| 42 | + flash = findViewById(R.id.flash) |
| 43 | + flash.setOnClickListener { |
| 44 | + flashEnabled = !flashEnabled |
| 45 | + cameraView.useFlash(flashEnabled) |
| 46 | + } |
| 47 | + resultView = findViewById(R.id.resultsList) |
| 48 | + resultView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) |
| 49 | + resultView.adapter = resultAdapter |
| 50 | + |
| 51 | + cameraView.setCameraOpenCallback { |
| 52 | + cameraView.postDelayed({ |
| 53 | + cameraView.useFlash(flashEnabled) |
| 54 | + cameraView.continuousFocus() |
| 55 | + }, 300) |
| 56 | + } |
| 57 | + |
| 58 | + val barcodeDetector = ScanbotBarcodeScannerSDK(this).createBarcodeDetector() |
| 59 | + |
| 60 | + barcodeDetectorFrameHandler = BarcodeDetectorFrameHandler.attach( |
| 61 | + cameraView, |
| 62 | + barcodeDetector |
| 63 | + ) |
| 64 | + |
| 65 | + barcodeDetectorFrameHandler?.setDetectionInterval(1000) |
| 66 | + barcodeDetectorFrameHandler?.addResultHandler(this) |
| 67 | + |
| 68 | + barcodeDetector.modifyConfig { |
| 69 | + setSaveCameraPreviewFrame(false) |
| 70 | + setBarcodeFormats(BarcodeTypeRepository.selectedTypes.toList()) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + override fun onResume() { |
| 75 | + super.onResume() |
| 76 | + cameraView.onResume() |
| 77 | + if (ContextCompat.checkSelfPermission( |
| 78 | + this, |
| 79 | + Manifest.permission.CAMERA |
| 80 | + ) != PackageManager.PERMISSION_GRANTED |
| 81 | + ) { |
| 82 | + // Use onActivityResult to handle permission rejection |
| 83 | + ActivityCompat.requestPermissions( |
| 84 | + this, |
| 85 | + arrayOf(Manifest.permission.CAMERA), |
| 86 | + REQUEST_PERMISSION_CODE |
| 87 | + ) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + override fun onPause() { |
| 92 | + super.onPause() |
| 93 | + cameraView.onPause() |
| 94 | + } |
| 95 | + |
| 96 | + private fun handleSuccess(result: FrameHandlerResult.Success<BarcodeScanningResult?>) { |
| 97 | + result.value?.let { |
| 98 | + cameraView.post { |
| 99 | + resultAdapter.addBarcodeItems(it.barcodeItems) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + override fun handle(result: FrameHandlerResult<BarcodeScanningResult?, SdkLicenseError>): Boolean { |
| 105 | + if (result is FrameHandlerResult.Success) { |
| 106 | + handleSuccess(result) |
| 107 | + } else { |
| 108 | + cameraView.post { |
| 109 | + Toast.makeText( |
| 110 | + this, |
| 111 | + "License has expired!", |
| 112 | + Toast.LENGTH_LONG |
| 113 | + ).show() |
| 114 | + } |
| 115 | + } |
| 116 | + return false |
| 117 | + } |
| 118 | + |
| 119 | + companion object { |
| 120 | + private const val REQUEST_PERMISSION_CODE = 200 |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +class BarcodeViewHolder(item: View) : RecyclerView.ViewHolder(item) { |
| 125 | + val image: ImageView by lazy { item.findViewById(R.id.image) } |
| 126 | + val barcodeType: TextView by lazy { item.findViewById(R.id.barcodeFormat) } |
| 127 | + val text: TextView by lazy { item.findViewById(R.id.docText) } |
| 128 | +} |
| 129 | + |
| 130 | +class ResultAdapter(val layoutInflater: LayoutInflater) : |
| 131 | + RecyclerView.Adapter<BarcodeViewHolder>() { |
| 132 | + private val items: MutableList<BarcodeItem> = mutableListOf() |
| 133 | + |
| 134 | + fun addBarcodeItems(items: List<BarcodeItem>) { |
| 135 | + // lets check duplicates |
| 136 | + items.forEach { item -> |
| 137 | + var insertedCount = 0 |
| 138 | + if (!this.items.any { it.text == item.text }) { |
| 139 | + this.items.add(0, item) |
| 140 | + insertedCount += 1 |
| 141 | + } |
| 142 | + notifyItemRangeInserted(0, insertedCount) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BarcodeViewHolder { |
| 147 | + return BarcodeViewHolder(layoutInflater.inflate(R.layout.barcode_item, parent, false)) |
| 148 | + } |
| 149 | + |
| 150 | + override fun onBindViewHolder(holder: BarcodeViewHolder, position: Int) { |
| 151 | + val item = items.get(position) |
| 152 | + holder.text.text = item.text |
| 153 | + holder.barcodeType.text = item.barcodeFormat.name |
| 154 | + holder.image.setImageBitmap(item.image) |
| 155 | + } |
| 156 | + |
| 157 | + override fun getItemCount(): Int = items.size |
| 158 | + |
| 159 | +} |
0 commit comments