zxing-android-embedded
zxing-android-embedded copied to clipboard
BarcodeView scan only QRCode but we need scan BarCode also
Description of the problem:
I create a Barcodeview, then class can scan QRCode, but when i try scan a BarCode the barcoderesult callback don't called.
I need scan all barcode in same BarCodeView.
Library version is
implementation "com.google.zxing:core:3.3.0"
implementation ("com.journeyapps:zxing-android-embedded:3.5.0") { transitive = false }
The problem occurs on all devices.
My code for create BarcodeView is:
package br.com.testApp.ui.flutter.codeScanner
import android.app.Activity
import android.view.View
import com.google.zxing.BarcodeFormat
import com.google.zxing.ResultPoint
import com.journeyapps.barcodescanner.BarcodeCallback
import com.journeyapps.barcodescanner.BarcodeResult
import com.journeyapps.barcodescanner.BarcodeView
import com.journeyapps.barcodescanner.DefaultDecoderFactory
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.platform.PlatformView
import timber.log.Timber
class CodeScannerView(private val registrar: PluginRegistry.Registrar, viewId: Int) : PlatformView {
private val mMethodChannel: MethodChannel by lazy {
MethodChannel(registrar.messenger(), METHOD_CHANNEL_NAME)
}
private val mBarcodeView: BarcodeView by lazy {
BarcodeView(registrar.activity()).also { barcodeView ->
barcodeView.decodeContinuous(
object : BarcodeCallback {
override fun barcodeResult(result: BarcodeResult) {
mMethodChannel.invokeMethod(CODE_READ_EVENT, mapOf("viewId" to viewId, "code" to result.text))
}
override fun possibleResultPoints(resultPoints: List<ResultPoint>) {}
}
)
}
}
init {
registrar.activity().application.registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
override fun onActivityPaused(activity: Activity?) {
Timber.d("Pausing the preview")
if (activity == registrar.activity() && mBarcodeView.isPreviewActive) {
mBarcodeView.pause()
}
}
override fun onActivityResumed(activity: Activity?) {
Timber.d("Resuming the preview")
if (activity == registrar.activity() && !mBarcodeView.isPreviewActive) {
mBarcodeView.resume()
}
}
override fun onActivityDestroyed(activity: Activity?) {
super.onActivityDestroyed(activity)
mBarcodeView.pause()
}
})
}
override fun getView(): View {
if (!mBarcodeView.isPreviewActive) {
mBarcodeView.resume()
}
return mBarcodeView
}
override fun dispose() {
mBarcodeView.pause()
}
companion object {
private const val METHOD_CHANNEL_NAME = "br.com.testApp/code-scanner-view"
private const val CODE_READ_EVENT = "onCodeScanned"
}
}
I add this code and now can read barcode, but my app is in portrait mode and the barcode only reader in landscape mode.
val formats = arrayListOf(
BarcodeFormat.AZTEC,
BarcodeFormat.CODABAR,
BarcodeFormat.CODE_39,
BarcodeFormat.CODE_93,
BarcodeFormat.CODE_128,
BarcodeFormat.ITF,
BarcodeFormat.QR_CODE
)
barcodeView.decoderFactory = DefaultDecoderFactory(formats)
I need scan barcode using BarcodeView with portrait mode.
try this to scan using portrait mode https://stackoverflow.com/a/35465968/3922705