Reputation: 111
I'm trying to make the basic ZXing functionality to scan qr codes using their basic instructions, but my camera does not open it just goes to the blank ScanActivity.
I've already added the "implementation" on module app dependency
implementation 'me.dm7.barcodescanner:zxing:1.9'
and permission on AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA"/>
I've also manually allowed its permission on the settings of the android phone i'm testing it on
My main activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tvResult = tvresult
btn.setOnClickListener {
val intent = Intent(this@MainActivity, ScanActivity::class.java)
startActivity(intent)
}
}
companion object {
var tvResult: TextView? = null
}
}
ScanActivity Class
class ScanActivity : AppCompatActivity(), ZXingScannerView.ResultHandler {
private var mScannerView: ZXingScannerView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mScannerView = ZXingScannerView(this)
setContentView(R.layout.activity_scan)
mScannerView!!.setResultHandler(this) // Register ourselves as a handler for scan results.
}
public override fun onResume() {
super.onResume()
mScannerView!!.setResultHandler(this) // Register ourselves as a handler for scan results.
mScannerView!!.startCamera() // Start camera on resume
}
public override fun onPause() {
super.onPause()
mScannerView!!.stopCamera() // Stop camera on pause
}
override fun handleResult(rawResult: Result) {
// Do something with the result here
// Log.v("tag", rawResult.getText()); // Prints scan results
// Log.v("tag", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)
MainActivity.tvResult!!.setText(rawResult.text)
onBackPressed()
// If you would like to resume scanning, call this method below:
//mScannerView.resumeCameraPreview(this);
}}
after i click on the Scan barcode button it just goes to this
(im expecting the camera will open because of the startCamera()
Upvotes: 0
Views: 2089
Reputation: 245
Late answer, by the way if someone else has this problem, you forgot to add mScannerView in your Layout
Upvotes: 0
Reputation: 41
I had a similar issue, it was permission related. Try adding the following within the onCreate(..)
if (ContextCompat.checkSelfPermission(this@MainActivity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this@MainActivity, arrayOf(Manifest.permission.CAMERA), 123)
}
Upvotes: 4