B4A Library Oh no! - another Barcode scanner (100% embedded in B4A)

This is a wrap for this Github project. You will need to download ZxingLatest.jar from the link below and copy it to your additional library folder:
https://drive.google.com/file/d/0B4g9tud5lvjgOGxENXZZcjVrZDA/view?usp=sharing

Extract LibRes.zip and copy the folder and its contents to be on the same folder level as that of the B4A project's /Files and /Objects folders.

Copy the other B4A library files (attached) to your additional library folder.

Nothing of interest happening in the B4A manifest file.

Have done this B4A V7.30 and on a KitKat device. It should work with earlier versions of B4A (and in all probability with Android versions earlier and later than KitKat)

Once the app started - click on the centre of the black square
Scan a code
To scan another code - click on the previously scanned/displayed barcode

Have tested it with:
Aztex codes
QR codes
PDF417
EAN13
DataMatrix

Sure a whole number of other barcode types can also be scanned as the decoding is done by the Zxing engine.

1.png


2.png


3.png


Sample Code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: b4aCodeScanner
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalRes: ..\LibRes

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private cs1 As CodeScannerView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
 
    cs1.MaskColor = Colors.ARGB(100, 0, 0, 200)
    cs1.FrameColor = Colors.Yellow
    cs1.FrameWidth = 5.0
    cs1.SquareFrame = True
    cs1.FrameCornersSize = 80
    cs1.ShowToastMessage = True

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
   cs1.stopPreview

End Sub


Sub cs1_scan_result(result As String)
 
    Log("B4A = " & result)
 
End Sub

Library:
CodeScanner
Author:
Github: Yuriy Budiyev, Wrapped by: Johan Schoeman
Version: 1
  • CodeScannerView
    Events:
    • scan_result (result As String)
    Fields:
    • ba As BA
    Methods:
    • BringToFront
    • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
    • Initialize (EventName As String)
    • Invalidate
    • Invalidate2 (arg0 As Rect)
    • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • IsInitialized As Boolean
    • RemoveView
    • RequestFocus As Boolean
    • SendToBack
    • SetBackgroundImageNew (arg0 As Bitmap) As BitmapDrawable
    • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
    • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
    • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
    • stopPreview
    Permissions:
    • android.permission.CAMERA
    Properties:
    • Background As Drawable
    • Color As Int [write only]
    • Enabled As Boolean
    • FrameColor As Int [write only]
      Set color of the frame
    • FrameCornersSize As Int [write only]
      Set length on the frame corners
    • FrameWidth As Int [write only]
      Set frame width
    • Height As Int
    • Left As Int
    • MaskColor As Int [write only]
      Set color of the space outside of the framing rect
    • Padding()() As Int
    • Parent As Object [read only]
    • ShowToastMessage As Boolean [write only]
      Show or dont show the Toast Message
      Default value is set to True
    • SquareFrame As Boolean [write only]
      Set whether frame is square or a rectangle
    • Tag As Object
    • Top As Int
    • Visible As Boolean
    • Width As Int
 

Attachments

  • LibRes.zip
    427 bytes · Views: 408
  • b4aCodeScanner.zip
    8 KB · Views: 412
  • b4aLibFiles.zip
    27.9 KB · Views: 405
  • TheJavaCode.zip
    18.1 KB · Views: 369
Last edited:

hf

Member
Licensed User
Logger连接到: 7850ce5b7d83
--------- beginning of crash
--------- beginning of system
--------- beginning of main
PackageAdded: package:JHS.CodeScanner
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:532)
at android.hardware.Camera.open(Camera.java:361)
at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:379)
** Activity (main) Pause, UserClosed = true **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:532)
at android.hardware.Camera.open(Camera.java:361)
at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:379)
** Activity (main) Pause, UserClosed = true **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:532)
at android.hardware.Camera.open(Camera.java:361)
at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:379)
** Activity (main) Pause, UserClosed = true **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:532)
at android.hardware.Camera.open(Camera.java:361)
at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:379)
** Activity (main) Pause, UserClosed = true **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:532)
at android.hardware.Camera.open(Camera.java:361)
at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:379)
** Activity (main) Pause, UserClosed = true **


have errors? Fail to connect to camera service!!!
 

Johan Schoeman

Expert
Licensed User
Longtime User
This type of error usually occurs if the camera is already occupied by another app. Restart your device and see if the native camera starts up. If it does start, then close the camera and then try the scanner again.
 

hf

Member
Licensed User
This type of error usually occurs if the camera is already occupied by another app. Restart your device and see if the native camera starts up. If it does start, then close the camera and then try the scanner again.


after restart, the error occur yet.
it's best npt despend "the camera is already occupied by another app"
 

f0raster0

Well-Known Member
Licensed User
Longtime User
Hi @Johan Schoeman,
I got this error in Android 9

Edit: It works fine with my old Smartphone Android 4.4
B4X:
Logger connected to:  samsung SM-A305GN
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
--------- beginning of crash
java.lang.RuntimeException: Fail to connect to camera service
    at android.hardware.Camera.<init>(Camera.java:562)
    at android.hardware.Camera.open(Camera.java:399)
    at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:379)
** Activity (main) Pause, UserClosed = true **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Service (starter) Destroy (ignored)**
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
java.lang.RuntimeException: Fail to connect to camera service
    at android.hardware.Camera.<init>(Camera.java:562)
    at android.hardware.Camera.open(Camera.java:399)
    at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:379)
** Activity (main) Pause, UserClosed = true **
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: Fail to connect to camera service
    at android.hardware.Camera.<init>(Camera.java:562)
    at android.hardware.Camera.open(Camera.java:399)
    at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:379)

I'm looking for a reader for barcode EAN13 and QR
I have been trying various of your BarCode readers: https://www.b4x.com/android/forum/threads/johans-overview.56611/
What Reader do you recomend to use?
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi @Johan Schoeman,
I got this error in Android 9

Edit: It works fine with my old Smartphone Android 4.4
B4X:
Logger connected to:  samsung SM-A305GN
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
--------- beginning of crash
java.lang.RuntimeException: Fail to connect to camera service
    at android.hardware.Camera.<init>(Camera.java:562)
    at android.hardware.Camera.open(Camera.java:399)
    at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:379)
** Activity (main) Pause, UserClosed = true **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Service (starter) Destroy (ignored)**
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
java.lang.RuntimeException: Fail to connect to camera service
    at android.hardware.Camera.<init>(Camera.java:562)
    at android.hardware.Camera.open(Camera.java:399)
    at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:379)
** Activity (main) Pause, UserClosed = true **
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: Fail to connect to camera service
    at android.hardware.Camera.<init>(Camera.java:562)
    at android.hardware.Camera.open(Camera.java:399)
    at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:379)

I'm looking for a reader for barcode EAN13 and QR
I have been trying various of your BarCode readers: https://www.b4x.com/android/forum/threads/johans-overview.56611/
What Reader do you recomend to use?

Have updated the library. Have tested it on Android 9 (Samsung S10) with Target SDK = 28
The library has a new name in the B4A IDE = CodeScannerView (v1.00). Make sure you enable it in the B4A IDE libs tab
Extract and copy the B4A lib files to your additional libs folder.
Library needs to be jetified = Tools->Jetifier (AndroidX)
See post#1 for what to do with LibRes

Sample Code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: b4aCodeScannerView
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalRes: ..\LibRes

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim rp As RuntimePermissions

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    
    
    Private Button1 As Button
    Private Button2 As Button
    Private cs1 As CodeScannerView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    
    Activity.LoadLayout("main")
    
    

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
  
 
End Sub


Sub cs1_scan_result(result As String)
    
    Log("B4A = " & result)
    
End Sub

Sub Button1_Click

    Dim Result As Boolean = True                                                                       
    If Not(rp.Check(rp.PERMISSION_CAMERA)) Then                                                       
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)                                                       
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)                   
    End If                                                                                             
    If Result Then

        cs1.MaskColor = Colors.ARGB(100, 0, 0, 200)
        cs1.FrameColor = Colors.Yellow
        cs1.FrameWidth = 5.0
        cs1.SquareFrame = True
        cs1.FrameCornersSize = 80
        cs1.ShowToastMessage = True
        cs1.CameraToUse = cs1.CAMERA_BACK    'This must be the last statement when you create the scanner!
                                                                                
        cs1.startPreview
    End If
    
End Sub

Sub Button2_Click
    
    cs1.stopPreview
    
End Sub
 

Attachments

  • LibRes.zip
    427 bytes · Views: 248
  • b4aCodeScannerView.zip
    8.7 KB · Views: 252
  • b4aLibFiles.zip
    54 KB · Views: 259

Sberla

Active Member
Licensed User
Longtime User
Have updated the library. Have tested it on Android 9 (Samsung S10) with Target SDK = 28
The library has a new name in the B4A IDE = CodeScannerView (v1.00). Make sure you enable it in the B4A IDE libs tab
Extract and copy the B4A lib files to your additional libs folder.
Library needs to be jetified = Tools->Jetifier (AndroidX)
See post#1 for what to do with LibRes

Sample Code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: b4aCodeScannerView
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalRes: ..\LibRes

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim rp As RuntimePermissions

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

  
  
    Private Button1 As Button
    Private Button2 As Button
    Private cs1 As CodeScannerView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
  
    Activity.LoadLayout("main")
  
  

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
 
End Sub


Sub cs1_scan_result(result As String)
  
    Log("B4A = " & result)
  
End Sub

Sub Button1_Click

    Dim Result As Boolean = True                                                                     
    If Not(rp.Check(rp.PERMISSION_CAMERA)) Then                                                     
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)                                                     
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)                 
    End If                                                                                           
    If Result Then

        cs1.MaskColor = Colors.ARGB(100, 0, 0, 200)
        cs1.FrameColor = Colors.Yellow
        cs1.FrameWidth = 5.0
        cs1.SquareFrame = True
        cs1.FrameCornersSize = 80
        cs1.ShowToastMessage = True
        cs1.CameraToUse = cs1.CAMERA_BACK    'This must be the last statement when you create the scanner!
                                                                              
        cs1.startPreview
    End If
  
End Sub

Sub Button2_Click
  
    cs1.stopPreview
  
End Sub


I'm testing it with android 9 and SDK28 but it gives me this error:

B4A Version: 9.30
Java Version: 8
Code Analysis. (0.00s)
Building folders structure. (0.12s)
Code compilation. (0.02s)
Compiling the layout code (0.02s)
Bookstore Organization. (0.00s)
(AndroidX SDK)
Generating R. files (2.97s)
Compiling the debugger code. (0.83s)
Compiling the produced Java code. (5.37s)
Byte code conversion - dex optimization. error
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcodescannerwrapper / codescannerWrapper;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcodescannerwrapper / codescannerWrapper $ 1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcodescannerwrapper / codescannerWrapper $ 1 $ 1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom / budiyev / android / codescanner / CodeScanner;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom / budiyev / android / codescanner / CodeScanner $ 1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom / budiyev / android / codescanner / CodeScanner $ AutoFocusCallback;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom / budiyev / android / codescanner / CodeScanner $ AutoFocusTask;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom / budiyev / android / codescanner / CodeScanner $ DecoderStateListener;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom / budiyev / android / codescanner / CodeScanner $ FinishInitializationTask;
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Translation has been interrupted
at com.android.dx.command.dexer.Main.processAllFiles (Main.java:696)
at com.android.dx.command.dexer.Main.runMonoDex (Main.java:315)
at com.android.dx.command.dexer.Main.runDx (Main.java:293)
at com.android.dx.command.dexer.Main.main (Main.java:249)
at com.android.dx.command.Main.main (Main.java:94)
Caused by: java.lang.InterruptedException: Too many errors
at com.android.dx.command.dexer.Main.processAllFiles (Main.java:688)
... 4 more

I also updated with these files. What else can I do?
 
Top