Skip to content

Instantly share code, notes, and snippets.

@ralphgabrielle
Last active October 31, 2019 05:44
Show Gist options
  • Save ralphgabrielle/2c3a5081ae4cbe615c615abfd59fad2f to your computer and use it in GitHub Desktop.
Save ralphgabrielle/2c3a5081ae4cbe615c615abfd59fad2f to your computer and use it in GitHub Desktop.
Simple QR Code
class MainActivity : BaseActivity() {
override val layoutId: Int?
get() = R.layout.activity_main
override fun viewCreated() {
bScan.setOnClickListener {
startActivityForResult<QRCodeActivity>(REQUEST_CODE_QR) // Kotlin Anko Intent Library
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CODE_QR) {
data?.getStringExtra("qr")?.let {
Log.d("QR Code Received", it)
}
}
}
}
}
class QRCodeActivity : BaseActivity(), QRCodeReaderView.OnQRCodeReadListener {
override val layoutId: Int?
get() = R.layout.activity_qrcode
override fun viewCreated() {
qrCode.setOnQRCodeReadListener(this)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)
}
override fun onResume() {
super.onResume()
qrCode.startCamera()
}
override fun onPause() {
super.onPause()
qrCode.stopCamera()
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
finish()
return true
}
}
return super.onOptionsItemSelected(item)
}
override fun onQRCodeRead(text: String?, points: Array<out PointF>?) {
qrCode.stopCamera()
val intent = Intent()
intent.putExtra("qr", text)
setResult(Activity.RESULT_OK, intent)
finish()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment