B4A Library [class] Google Code Scanner - no permission, very simple to use, barcode scanning

This solution is based on ML Kit (https://developers.google.com/ml-kit/vision/barcode-scanning/code-scanner).

Instructions:
1. Add to main module:
B4X:
#AdditionalJar: com.google.android.gms:play-services-code-scanner
#MultiDex: True
2. Add to manifest editor:
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase) 'add if not already there

'******* google code scanner
AddApplicationText(
<meta-data
      android:name="com.google.mlkit.vision.DEPENDENCIES"
      android:value="barcode_ui"/>
 <activity
            android:name="com.google.mlkit.vision.codescanner.internal.GmsBarcodeScanningDelegateActivity"
            android:exported="false"
            android:screenOrientation="portrait"
           >
  </activity>
)
'*****************

'*********  ML kit **********
AddApplicationText(
 <activity
            android:name="com.google.mlkit.vision.documentscanner.internal.GmsDocumentScanningDelegateActivity"
            android:exported="false"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
             >
        </activity>
         <provider
            android:name="com.google.mlkit.common.internal.MlKitInitProvider"
            android:authorities="${applicationId}.mlkitinitprovider"
            android:exported="false"
            android:initOrder="99" />

        <service
            android:name="com.google.mlkit.common.internal.MlKitComponentDiscoveryService"
            android:directBootAware="true"
            android:exported="false"
             >
            <meta-data
                android:name="com.google.firebase.components:com.google.mlkit.common.internal.CommonComponentRegistrar"
                android:value="com.google.firebase.components.ComponentRegistrar" />
        </service>
)
'******************************

Usage:
B4X:
Private Sub Button1_Click
    Dim formats As List = Array(Scanner.FORMAT_ALL_FORMATS) 'For better performance pass the specific formats needed.
    Wait For (Scanner.Scan(formats)) Complete (Result As ScannerResult)
    If Result.Success Then
        Log(Result.Value)
    End If
End Sub

See attached project.
 

Attachments

  • GoogleCodeScanner.zip
    15.4 KB · Views: 260
Last edited:

drgottjr

Expert
Licensed User
Longtime User
mea culpa
 

Ryan Pigeon

Member
This solution is based on ML Kit (https://developers.google.com/ml-kit/vision/barcode-scanning/code-scanner).

Instructions:
1. Add to main module:
B4X:
#AdditionalJar: com.google.android.gms:play-services-code-scanner
#MultiDex: True
2. Add to manifest editor:
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase) 'add if not already there

'******* google code scanner
AddApplicationText(
<meta-data
      android:name="com.google.mlkit.vision.DEPENDENCIES"
      android:value="barcode_ui"/>
 <activity
            android:name="com.google.mlkit.vision.codescanner.internal.GmsBarcodeScanningDelegateActivity"
            android:exported="false"
            android:screenOrientation="portrait"
           >
  </activity>
)
'*****************

'*********  ML kit **********
AddApplicationText(
 <activity
            android:name="com.google.mlkit.vision.documentscanner.internal.GmsDocumentScanningDelegateActivity"
            android:exported="false"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
             >
        </activity>
         <provider
            android:name="com.google.mlkit.common.internal.MlKitInitProvider"
            android:authorities="${applicationId}.mlkitinitprovider"
            android:exported="false"
            android:initOrder="99" />

        <service
            android:name="com.google.mlkit.common.internal.MlKitComponentDiscoveryService"
            android:directBootAware="true"
            android:exported="false"
             >
            <meta-data
                android:name="com.google.firebase.components:com.google.mlkit.common.internal.CommonComponentRegistrar"
                android:value="com.google.firebase.components.ComponentRegistrar" />
        </service>
)
'******************************

Usage:
B4X:
Private Sub Button1_Click
    Dim formats As List = Array(Scanner.FORMAT_ALL_FORMATS) 'For better performance pass the specific formats needed.
    Wait For (Scanner.Scan(formats)) Complete (Result As ScannerResult)
    If Result.Success Then
        Log(Result.Value)
    End If
End Sub

See attached project.
Error:

** Activity (main) Pause event (activity is not paused). **
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/arch/core/executor/ArchTaskExecutor;
at androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.java:322)
at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:178)
at androidx.activity.ComponentActivity.<init>(ComponentActivity.java:277)
at com.google.mlkit.vision.codescanner.internal.GmsBarcodeScanningDelegateActivity.<init>(com.google.android.gms:play-services-code-scanner@@16.1.0:1)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at android.app.Instrumentation.newActivity(Instrumentation.java:1378)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4042)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4330)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2693)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:230)
at android.os.Looper.loop(Looper.java:319)
at android.app.ActivityThread.main(ActivityThread.java:9063)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:588)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
Caused by: java.lang.ClassNotFoundException: androidx.arch.core.executor.ArchTaskExecutor
... 20 more
 
Top