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: 739
Last edited:

Beja

Expert
Licensed User
Longtime User
When the app crashes there is always a stack trace in the logs. Please post it.

Strange behaviour>> Now working normally.

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
 

Beja

Expert
Licensed User
Longtime User
Hi Erel,
Today I went back to this app example and got this error:


B4A Version: 13.10
Parsing code. (0.01s)
Java Version: 8
Building folders structure. (0.01s)
Running custom action. (0.04s)
Compiling code. (0.07s)
Compiling layouts code. (0.02s)
Organizing libraries. Error
Maven artifact not found: com.google.android.gms/play-services-code-scanner
Source: com.google.android.gms:play-services-code-scanner

Any meaning appreciated
 

Jota21

New Member
Hi, I'm using this library and something very strange is happening. I used it a couple of times a few days ago and it worked perfectly on my Android device (INFINIX HOT 20 with Android 12). I used it to scan a few QR codes and everything worked fine.


Then I continued developing the rest of the app and testing, and everything else worked as expected. But when I went back to use the scanner, it just wouldn’t launch. It made a kind of movement as if it was about to open, but the scanner never actually started.


At first, I thought it might have been a mistake on my part, so I commented out the new code and tried again — same issue. Then I decided to run the example provided with the library to rule out an error in my implementation, but even the example didn’t work.


So I ran it on my emulator (MEMU PLAY with Android 9), and to my surprise, it worked perfectly there. But on my actual Android device, it still doesn’t work.


If anyone else has experienced this issue or knows how to fix it, please let me know. I’ve already restarted my phone, uninstalled the app completely, and reinstalled it, but nothing worked.


I’m attaching a video showing what’s happening on my device, a working test from the emulator, and also the page logs — though there’s not even an error showing up.


Thank you in advance!

1750741928950.png
1750742045771.png
1750742132454.png
1750744081975.png


Link of Videos:

 
Last edited:

Magma

Expert
Licensed User
Longtime User
In some devices i have the same problem - before was working but not now at all devices... is there a way to somehow reset somehow the MLKIT... Exception Not Initialize

may be is a manifest problem ?

extra for mlkit (have also ocr and qrcode - may be is better to combine them - but i don't know how):
B4X:
...

'******* 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>
)
'******************************

'****** text recognition *****
AddApplicationText(
 <meta-data
          android:name="com.google.mlkit.vision.DEPENDENCIES"
          android:value="ocr" />
)
'**********

'*********  ML kit + subject segmentation +  text recognition **********
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" />
             <meta-data
                android:name="com.google.firebase.components:com.google.mlkit.vision.segmentation.subject.internal.SubjectSegmentationRegistrar"
                android:value="com.google.firebase.components.ComponentRegistrar" />
            <meta-data
                android:name="com.google.firebase.components:com.google.mlkit.vision.text.internal.TextRegistrar"
                android:value="com.google.firebase.components.ComponentRegistrar" />
        </service>
)
'******************************

AddPermission(android.permission.CAMERA)

...
 
Last edited:

Mark Stuart

Active Member
Licensed User
Longtime User
I just downloaded the solution and ran it to my Samsung Tablet running android 9.
I didn't make any changes to the app and it crashed with the following error:

B4X:
Logger connected to:  samsung SM-T380
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
** Activity (main) Pause event (activity is not paused). **
--------- beginning of crash
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/arch/core/executor/ArchTaskExecutor;
    at androidx.lifecycle.LifecycleRegistry_androidKt.isMainThread(LifecycleRegistry.android.kt:23)
    at androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.jvm.kt:297)
    at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.jvm.kt:172)
    at androidx.activity.ComponentActivity.<init>(ComponentActivity.kt:256)
    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:69)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1219)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3053)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3305)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1993)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7266)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.arch.core.executor.ArchTaskExecutor" on path: DexPathList[[zip file "/data/app/b4a.example-BSCgg7XA6YqDf6Vxa3IRGg==/base.apk"],nativeLibraryDirectories=[/data/app/b4a.example-BSCgg7XA6YqDf6Vxa3IRGg==/lib/arm, /system/lib, /system/vendor/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    ... 20 more

What just happened?

Mark Stuart
 

Magma

Expert
Licensed User
Longtime User
Will this solution work on older tablets with android version, 6 thru 9?
Will work at android versions 7+ for sure
Download latest resources, cmd tools, sdk, b4a, suggested java...
Target sdk 35
And install all sdk have to do with vision/mlkit
 

Magma

Expert
Licensed User
Longtime User
My installation is the latest and installed on 16th September, so everything should be up-to-date as far as B4X goes.

Then why didn't it work for me on Android 9?
This doesn't answer my question why the app crashed.
Did you check sdk manager .. all *ML* vision
 

Mark Stuart

Active Member
Licensed User
Longtime User
Did you check sdk manager .. all *ML* vision
Not sure what you mean by *ML* vision.

Loaded the SDK Manager and the "sdkmanager.bat" field was empty. And below the field as a reference, is <sdk>\tools\bin\sdkmanager.bat

My installation does not have that path/folder. So I finally found the sdkmanager.bat here:
C:\Android\cmdline-tools\bin\sdkmanager.bat
I've no idea if that is correct, but it worked.

I typed in the search field *ML* vision - nothing found that matches that text.
Searched with ML and found all entries installed, nothing listed related to ML in the Recommended.

So I assume all ML stuff is installed.
 
Last edited:

Magma

Expert
Licensed User
Longtime User
Ok... there are some devices not support GMS services (play store, google play services).... for those use:


Check my code here (is ocr.. but using the same libs):


If it works ... I will share the part of barcode, qrcode scan
 

Mark Stuart

Active Member
Licensed User
Longtime User
Hi Magma,
In post #9 in the B4XOCRPage (2nd link), I found you have a project zipped there.
Is that something I can use for barcode scanning?
I looked at the ZXing project and found it is too old.
 

Magma

Expert
Licensed User
Longtime User
Hi Magma,
In post #9 in the B4XOCRPage (2nd link), I found you have a project zipped there.
Is that something I can use for barcode scanning?
I looked at the ZXing project and found it is too old.

Zxing works!

If the ocr works... will share tomorrow code with barcode n qrcode scanning
 
Top