zxing-android-embedded icon indicating copy to clipboard operation
zxing-android-embedded copied to clipboard

BarcodeView scan only QRCode but we need scan BarCode also

Open Thuran opened this issue 5 years ago • 2 comments

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"
    }
}

Thuran avatar May 20 '20 15:05 Thuran

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.

Thuran avatar May 20 '20 16:05 Thuran

try this to scan using portrait mode https://stackoverflow.com/a/35465968/3922705

kaling852 avatar May 20 '20 18:05 kaling852