Проблема
После того, как камера сфокусируется, процессор штрих-кода обнаружит один и тот же штрих-код несколько раз, поэтому вы будете сканировать 20, а не 1 штрих-код в секунду. #
class MainActivity : AppCompatActivity() {
internal var isDetected = false
lateinit var options:FirebaseVisionBarcodeDetectorOptions
lateinit var detector:FirebaseVisionBarcodeDetector
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Dexter.withActivity(this@MainActivity)
.withPermissions(android.Manifest.permission.CAMERA,
android.Manifest.permission.RECORD_AUDIO)
.withListener(object:MultiplePermissionsListener{
override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
setupCamera()
}
override fun onPermissionRationaleShouldBeShown(
permissions: MutableList<PermissionRequest>?,
token: PermissionToken?
) {
}
}).check()
}
private fun setupCamera() {
options=FirebaseVisionBarcodeDetectorOptions.Builder()
.setBarcodeFormats(FirebaseVisionBarcode.FORMAT_QR_CODE)
.build()
detector= FirebaseVision.getInstance().getVisionBarcodeDetector(options)
btn_again.isEnabled = isDetected
btn_again.setOnClickListener{
isDetected=!isDetected
btn_again.isEnabled=isDetected
}
cameraView.setLifecycleOwner(this)
cameraView.addFrameProcessor { frame -> processImage(getVisionImageFromFrame(frame))}
}
private fun processImage(image: FirebaseVisionImage) {
if (! isDetected) {детектор.detectInImage (изображение) .addOnFailureListener {исключение -> Toast.makeText (this@MainActivity, "" + exception.message, Toast.LENGTH_LONG) .show ()} .addOnSuccessListener (firebaseVisionBarcodesult) )}}
}
private fun processResult(firebaseVisionBarcodes: List<FirebaseVisionBarcode>) {
if(firebaseVisionBarcodes.size>0)
{
isDetected=true
btn_again.isEnabled = isDetected
loop@ for(item in firebaseVisionBarcodes)
{
val value_type = item.valueType
when(value_type)
{
FirebaseVisionBarcode.TYPE_TEXT ->{
createDialog(item.rawValue)
}
FirebaseVisionBarcode.TYPE_CONTACT_INFO ->{
val info = StringBuilder("Name:")
.append(item.contactInfo!!.name!!.formattedName)
.append("/n")
.append("Address: ")
.append(item.contactInfo!!.addresses[0].addressLines[0])
.append("/n")
.append("Email:")
.append(item.contactInfo!!.emails[0].address)
.toString()
createDialog(info)
}
FirebaseVisionBarcode.TYPE_URL ->{
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(item.rawValue))
startActivity(intent)
}
}
}
}
}
private fun createDialog(text: String?) {
val builder = AlertDialog.Builder(this)
builder.setMessage(text)
.setPositiveButton("okay",{ DialogInterface, _-> DialogInterface.dismiss() })
val dialog = builder.create()
dialog.show()
}
private fun getVisionImageFromFrame(frame: Frame): FirebaseVisionImage {
val data =frame.getData<ByteArray>()
val metadata=FirebaseVisionImageMetadata.Builder()
.setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
.setHeight(frame.size.height)
.setWidth(frame.size.width)
//.setRotation(frame.rotation)
.build()
return FirebaseVisionImage.fromByteArray(data,metadata)
}
}