Используйте этот класс:
package com.company.project.view.activities
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.google.zxing.Result
import kotlinx.android.synthetic.main.activity_scaling_scanner.*
import me.dm7.barcodescanner.zxing.ZXingScannerView
import com.company.project.R
class ScanActivity : AppCompatActivity(), ZXingScannerView.ResultHandler {
private val FLASH_STATE = "FLASH_STATE"
private val AUTOFOCUS_STATE = "AUTOFOCUS_STATE"
private var mScannerView: ZXingScannerView? = null
private var mFlash: Boolean = false
private var mAutofocus: Boolean = true
private var resultText: String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scanner)
mScannerView = ZXingScannerView(this)
content_frame.addView(mScannerView)
mScannerView?.setFlash(mFlash)
mScannerView?.setAutoFocus(true)
switchFlash.setOnClickListener({
mFlash = !mFlash
mScannerView?.setFlash(mFlash)
})
}
override fun onResume() {
super.onResume()
mScannerView?.setResultHandler(this)
mScannerView?.setAspectTolerance(0.2f)
mScannerView?.setFlash(mFlash)
mScannerView?.setAutoFocus(mAutofocus)
mScannerView?.startCamera()
switchFlash.setChecked(mFlash)
}
override fun onPause() {
super.onPause()
mScannerView?.stopCamera()
}
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState)
outState?.putBoolean(FLASH_STATE, mFlash)
outState?.putBoolean(AUTOFOCUS_STATE, mAutofocus)
}
override fun handleResult(result: Result?) {
mScannerView?.resumeCameraPreview(this)
if (result == null) {
return
}
mScannerView?.stopCamera()
resultText = result.text;
val resultIntent: Intent= Intent().putExtra("BRCode", resultText)
setResult(1, resultIntent)
finish()
}
}
этот макет просмотра:
activity_scanner.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
<LinearLayout
android:orientation="horizontal"
android:gravity="center"
android:background="#222"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Switch
android:id="@+id/switchFlash"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_weight="1"
android:text="@string/flashValue"
android:textColor="?attr/colorBackgroundFloating"
android:paddingLeft="300dp" android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
и звоните так из своей MainActivity
scanView.setOnClickListener {
scanQRCode()
}
fun scanQRCode(){
val intent = Intent(this, ScanActivity::class.java)
startActivityForResult(intent, 2)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (data == null) {
showIncorrectQRCodeDialogue()
return
}
if (data.getStringExtra("BRCode")!=null) {
val brCode: String = data.getStringExtra("BRCode")
val intent = Intent(this, NewActivity::class.java);
startActivity(intent);
}else {
showIncorrectQRCodeDialogue()
}
}
Вы сможете редактировать макет сканирования и класс активности сканирования в соответствии с вашими требованиями.
Надеюсь, это поможет вам.Удачного кодирования.