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: 651
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:
Top