2023-11-16 20:16:28 +03:00
|
|
|
package org.amnezia.vpn
|
2023-02-17 16:38:44 +03:00
|
|
|
|
|
|
|
|
import android.Manifest
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
|
import android.content.pm.PackageManager
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.util.Log
|
2023-03-25 16:56:17 +01:00
|
|
|
import android.view.MotionEvent
|
|
|
|
|
import android.view.View
|
2023-02-17 16:38:44 +03:00
|
|
|
import android.widget.Toast
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
|
import androidx.camera.core.*
|
|
|
|
|
import androidx.camera.lifecycle.ProcessCameraProvider
|
|
|
|
|
import androidx.camera.view.PreviewView
|
|
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
|
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
|
|
|
|
|
import com.google.mlkit.vision.barcode.BarcodeScanning
|
|
|
|
|
import com.google.mlkit.vision.barcode.common.Barcode
|
|
|
|
|
import com.google.mlkit.vision.common.InputImage
|
2023-03-25 16:56:17 +01:00
|
|
|
import org.amnezia.vpn.R
|
2023-02-17 16:38:44 +03:00
|
|
|
import java.util.concurrent.ExecutorService
|
|
|
|
|
import java.util.concurrent.Executors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CameraActivity : AppCompatActivity() {
|
|
|
|
|
|
|
|
|
|
private val CAMERA_REQUEST = 100
|
|
|
|
|
|
|
|
|
|
private lateinit var cameraExecutor: ExecutorService
|
|
|
|
|
private lateinit var analyzerExecutor: ExecutorService
|
|
|
|
|
|
|
|
|
|
private lateinit var viewFinder: PreviewView
|
|
|
|
|
|
2023-02-22 14:22:03 +03:00
|
|
|
companion object {
|
|
|
|
|
private lateinit var instance: CameraActivity
|
|
|
|
|
|
|
|
|
|
@JvmStatic fun getInstance(): CameraActivity {
|
|
|
|
|
return instance
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@JvmStatic fun stopQrCodeReader() {
|
|
|
|
|
CameraActivity.getInstance().finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
external fun passDataToDecoder(data: String)
|
|
|
|
|
|
2023-02-17 16:38:44 +03:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
setContentView(R.layout.activity_camera)
|
|
|
|
|
|
|
|
|
|
viewFinder = findViewById(R.id.viewFinder)
|
|
|
|
|
|
|
|
|
|
cameraExecutor = Executors.newSingleThreadExecutor()
|
|
|
|
|
analyzerExecutor = Executors.newSingleThreadExecutor()
|
|
|
|
|
|
2023-02-22 14:22:03 +03:00
|
|
|
instance = this
|
|
|
|
|
|
2023-02-17 16:38:44 +03:00
|
|
|
checkPermissions()
|
|
|
|
|
|
|
|
|
|
configureVideoPreview()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun checkPermissions() {
|
|
|
|
|
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
requestPermissions(arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
|
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
|
|
|
if (requestCode == CAMERA_REQUEST) {
|
|
|
|
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
Toast.makeText(this, "CameraX permission granted", Toast.LENGTH_SHORT).show();
|
|
|
|
|
} else {
|
|
|
|
|
Toast.makeText(this, "CameraX permission denied", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-25 16:56:17 +01:00
|
|
|
@SuppressLint("UnsafeOptInUsageError", "ClickableViewAccessibility")
|
2023-02-17 16:38:44 +03:00
|
|
|
private fun configureVideoPreview() {
|
|
|
|
|
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
|
|
|
|
|
val imageCapture = ImageCapture.Builder().build()
|
|
|
|
|
|
|
|
|
|
cameraProviderFuture.addListener({
|
|
|
|
|
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
|
|
|
|
|
|
|
|
|
|
val preview = Preview.Builder().build()
|
|
|
|
|
|
|
|
|
|
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
|
|
|
|
|
|
2023-02-22 14:22:03 +03:00
|
|
|
val imageAnalyzer = BarCodeAnalyzer()
|
2023-02-17 16:38:44 +03:00
|
|
|
|
|
|
|
|
val analysisUseCase = ImageAnalysis.Builder()
|
|
|
|
|
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
|
|
|
|
|
.build()
|
|
|
|
|
|
|
|
|
|
analysisUseCase.setAnalyzer(analyzerExecutor, imageAnalyzer)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
preview.setSurfaceProvider(viewFinder.surfaceProvider)
|
|
|
|
|
cameraProvider.unbindAll()
|
2023-03-25 16:56:17 +01:00
|
|
|
val camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture, analysisUseCase)
|
|
|
|
|
viewFinder.setOnTouchListener(View.OnTouchListener { view: View, motionEvent: MotionEvent ->
|
|
|
|
|
when (motionEvent.action) {
|
|
|
|
|
MotionEvent.ACTION_DOWN -> return@OnTouchListener true
|
|
|
|
|
MotionEvent.ACTION_UP -> {
|
|
|
|
|
val factory = viewFinder.meteringPointFactory
|
|
|
|
|
val point = factory.createPoint(motionEvent.x, motionEvent.y)
|
|
|
|
|
val action = FocusMeteringAction.Builder(point).build()
|
|
|
|
|
camera.cameraControl.startFocusAndMetering(action)
|
|
|
|
|
return@OnTouchListener true
|
|
|
|
|
}
|
|
|
|
|
else -> return@OnTouchListener false
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-02-17 16:38:44 +03:00
|
|
|
} catch(exc: Exception) {
|
|
|
|
|
Log.e("WUTT", "Use case binding failed", exc)
|
|
|
|
|
}
|
|
|
|
|
}, ContextCompat.getMainExecutor(this))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
|
cameraExecutor.shutdown()
|
|
|
|
|
analyzerExecutor.shutdown()
|
|
|
|
|
|
|
|
|
|
super.onDestroy()
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 14:22:03 +03:00
|
|
|
val barcodesSet = mutableSetOf<String>()
|
|
|
|
|
|
|
|
|
|
private inner class BarCodeAnalyzer(): ImageAnalysis.Analyzer {
|
2023-02-17 16:38:44 +03:00
|
|
|
|
|
|
|
|
private val options = BarcodeScannerOptions.Builder()
|
|
|
|
|
.setBarcodeFormats(Barcode.FORMAT_QR_CODE)
|
|
|
|
|
.build()
|
|
|
|
|
|
|
|
|
|
private val scanner = BarcodeScanning.getClient(options)
|
|
|
|
|
|
|
|
|
|
@SuppressLint("UnsafeOptInUsageError")
|
|
|
|
|
override fun analyze(imageProxy: ImageProxy) {
|
|
|
|
|
val mediaImage = imageProxy.image
|
|
|
|
|
|
|
|
|
|
if (mediaImage != null) {
|
|
|
|
|
val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
|
|
|
|
|
|
|
|
|
|
scanner.process(image)
|
|
|
|
|
.addOnSuccessListener { barcodes ->
|
|
|
|
|
if (barcodes.isNotEmpty()) {
|
2023-02-22 14:22:03 +03:00
|
|
|
val barcode = barcodes[0]
|
|
|
|
|
if (barcode != null) {
|
|
|
|
|
val str = barcode?.displayValue ?: ""
|
|
|
|
|
if (str.isNotEmpty()) {
|
|
|
|
|
val isAdded = barcodesSet.add(str)
|
|
|
|
|
if (isAdded) {
|
|
|
|
|
passDataToDecoder(str)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-17 16:38:44 +03:00
|
|
|
}
|
|
|
|
|
imageProxy.close()
|
|
|
|
|
}
|
|
|
|
|
.addOnFailureListener {
|
|
|
|
|
imageProxy.close()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|