Skip to content

Instantly share code, notes, and snippets.

@NikitiJ
Created January 20, 2020 11:00
Show Gist options
  • Save NikitiJ/5de41162a62ed4e999ca6f8c40545a2b to your computer and use it in GitHub Desktop.
Save NikitiJ/5de41162a62ed4e999ca6f8c40545a2b to your computer and use it in GitHub Desktop.
SurfaceViewTrouble
class QRCodesScannerActivity : AppCompatActivity() {
private val TAG = "CameraPermission"
private val CAMERA_REQUEST_CODE = 101
private lateinit var barcodeDetector: BarcodeDetector
private lateinit var cameraSource: CameraSource
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setup(R.layout.activity_qrcodes_scanner, getString(R.string.scaning_qr_code))
surfaceViewScanArea.setZOrderMediaOverlay(true)
setupPermission()
}
private fun initScanArea() {
barcodeDetector = BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE).build()
cameraSource = CameraSource.Builder(this, barcodeDetector).setRequestedPreviewSize(640, 480)
.build()
surfaceViewScanArea.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?) {
cameraSource.start(holder)
}
})
barcodeDetector.setProcessor(object: Detector.Processor<Barcode> {
override fun release() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun receiveDetections(detections: Detector.Detections<Barcode>?) {
val qrCodes = detections?.detectedItems
if (qrCodes != null && qrCodes.size() != 0) {
qrCodes.forEach { key, value ->
value.cornerPoints
}
showQrCodeOverlay()
Toast.makeText(this@QRCodesScannerActivity, "SCANNED!", Toast.LENGTH_SHORT).show()
}
}
})
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
when (requestCode) {
CAMERA_REQUEST_CODE -> {
if (grantResults.isEmpty() || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
showAlert(getString(R.string.camera_permission_required),
getString(R.string.alert_error_title), true)
}
else {
initScanArea()
}
}
}
}
private fun setupPermission() {
val permission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
if (permission != PackageManager.PERMISSION_GRANTED) {
makeRequestCameraPermission()
}
else {
initScanArea()
}
}
private fun makeRequestCameraPermission() {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA),
CAMERA_REQUEST_CODE)
}
private fun showQrCodeOverlay() {
/*val layoutParams = viewQrCodeOverlay.layoutParams as ConstraintLayout.LayoutParams
layoutParams.setMargins(50, 15, 15, 15)
viewQrCodeOverlay.layoutParams = layoutParams*/
surfaceViewScanArea.setZOrderMediaOverlay(true)
viewQrCodeOverlay.visibility = View.INVISIBLE
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment