B4A Library 1D and 2D Barcode Scanner with ZBAR - another Barcode Scanner that is 100% embedded in B4A

Discussion in 'Additional libraries, classes and official updates' started by Johan Schoeman, Feb 21, 2016.

  1. Johan Schoeman

    Johan Schoeman Expert Licensed User

    The attached project wraps the ZBAR part of this Github project. It looks very much the same as the project that I have posted here. Both projects use the same ViewFinder but they use different decoding engines (ZXING and ZBAR respectively).

    I have not tested all the barcode formats but it should scan the following types:

    BarcodeFormat.PARTIAL
    BarcodeFormat.EAN8
    BarcodeFormat.UPCE
    BarcodeFormat.ISBN10
    BarcodeFormat.UPCA
    BarcodeFormat.EAN13
    BarcodeFormat.ISBN13
    BarcodeFormat.I25
    BarcodeFormat.DATABAR
    BarcodeFormat.DATABAR_EXP
    BarcodeFormat.CODABAR
    BarcodeFormat.CODE39
    BarcodeFormat.PDF417
    BarcodeFormat.QRCODE
    BarcodeFormat.CODE93
    BarcodeFormat.CODE128

    There seems to be an issue with the scanning of PDF417 codes. I have emailed the author of the Github project (Dushyanth Maguluru) and brought it to his attention. Will update and post new library files once the issue with PDF417 codes has been resolved.

    EDIT 26 Feb 2016: See this post as far as scanning of PDF417 is concerned
    https://sourceforge.net/p/zbar/support-requests/76/

    Please take note of the xml files in the /Objects/res/layout and Objects/res/values folders of the B4A project should you start a new project from scratch.

    Posting the following:
    1. B4A project demonstrating the Barcode Scanner using the ZBAR decoder
    2. Library files - you need to download them from this link (too big to upload to this post) - unzip them and copy them to your additional library folder

    You can download and test any posting of mine in this thread but if you want to use it then you need to [​IMG]

    1.png

    Sample code:

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

    #End Region

    #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 zb1 As ZbarBarcodeScanner
        
    Private b1 As Button
        
    Private b2 As Button
        
    Private b3 As Button
    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")

        zb1.LaserColor = 
    Colors.Yellow
        zb1.MaskColor = 
    Colors.ARGB(15000200)
        zb1.BorderColor = 
    Colors.Magenta
        zb1.BorderStrokeWidth = 
    5
        zb1.BorderLineLength = 
    40
        zb1.Visible = 
    False

    End Sub

    Sub Activity_Resume

    '   zb1.startScanner

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

        zb1.stopScanner
        zb1.Visible = 
    False

    End Sub

    Sub b1_Click

        zb1.toggleFlash

    End Sub

    Sub b2_Click
        zb1.Visible = 
    True
        zb1.startScanner

    End Sub

    Sub b3_Click
        zb1.Visible = 
    False
        zb1.stopScanner

    End Sub



    Sub zb1_scan_result(scantext As String, scanformat As String)

        
    Log("B4A scantext = " & scantext)
        
    Log("B4A scanformat = " & scanformat)

    End Sub

    The library:

    ZbarBarcodeScanner
    Author:
    Github: Dushyanth Maguluru, Wrapped by: Johan Schoeman
    Version: 1
    • ZbarBarcodeScanner
      Events:
      • scan_result (scantext As String, scanformat 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
      • SetBackgroundImage (arg0 As Bitmap)
      • 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)
      • handleResult (rawResult As Result)
      • startScanner
      • stopScanner
      • toggleFlash
      Permissions:
      • android.permission.CAMERA
      • android.permission.FLASHLIGHT
      Properties:
      • Background As Drawable
      • BorderColor As Int [write only]
      • BorderLineLength As Int [write only]
      • BorderStrokeWidth As Int [write only]
      • Color As Int [write only]
      • Enabled As Boolean
      • Height As Int
      • LaserColor As Int [write only]
      • Left As Int
      • MaskColor As Int [write only]
      • Tag As Object
      • Top As Int
      • Visible As Boolean
      • Width As Int



    You can use it in portrait mode and landscape mode by changing the B4A project's attribute and the code in the Designer:
    #SupportedOrientations: portrait
     

    Attached Files:

    Last edited: May 2, 2016
  2. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Last edited: Feb 21, 2016
  3. krebsi83

    krebsi83 Member Licensed User

    I have tried the lib... It's great and very fast!
    But I have a problem... my app (also your example) don't run correctly in release-mode :,(
    IN RELEASE-MODE: After the app/lib has recognized the barcode... the picture stands still... no beep, no result
     
    Johan Schoeman likes this.
  4. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Try it with the set of new lib files at this link https://www.dropbox.com/s/y8ovcdjef0i421n/ZbarBarcodeScannerLib23Feb.zip?dl=0
    I have removed the sound for now - seems like some devices don't like it and then for some reason the "handleResult" call back in the wrapper does not fire. Will try and find a workaround for it sometime during this week but the new libs should sort out the "freezing" problem.

    Else just add your own sound in the B4A event that gets fired when the scanner returns the scanned result to the B4A project
     
  5. krebsi83

    krebsi83 Member Licensed User

    Thank you so much! You are great!
    This Version works without "freezing" in release-mode.
    I look forward to the next release. Until then I can live without sound. ;)

    By the way: The Zbar-Lib scans faster than your other two Libs. I have a lowprice HOMTOM HT7 china-phone with a bad camera... but with this Zbar-lib I get great results!
     
    Johan Schoeman likes this.
  6. incendio

    incendio Well-Known Member Licensed User

    Thanks for this library. Great library and very fast.

    I have a problem, when tried to scan same 1D barcode, over and over, around 10x, sometimes it produce double scan result. So, instead of got 10 scan results, I could got 11 or 12 results.
     
  7. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Are you first pointing the camera away from the barcode before you try to scan it again? My best guess is that a frame is passed to the ZBAR engine and decoded and the decoding takes place fast enough that it grabs another frame and is able to decode it before you have pointed the camera away from the barcode that you are scanning. If you don't want a double scan to occur then compare the new scan result with the previous scan result in Sub zb1_scan_result and discard the new result if it is the same as the previous result. You will then of course "not be able to scan the same barcode consecutively unless a different barcode is scanned inbetween" - or at least as far as the user is concerned.
     
  8. incendio

    incendio Well-Known Member Licensed User

    Yes, I have pointed camera right away after scanned.

    If I scan 2 different barcode repeatedly, barcode A, barcode B, barcode A, barcode B, and so on, i got correct result.

    Scanning same barcode consecutively is important for me, so can not just discard scan result.
     
  9. Johan Schoeman

    Johan Schoeman Expert Licensed User

    The only way that it will report a "double" scan is when the ZBAR library (NOT THE WRAPPER) was passed two frames to decode in the time that you have pointed the scanner at the barcode. It decoded a frame and before you could point it away it grabbed and passed another frame to be decoded (probably because it seems to be fast). So not sure how you want to approach the "double" scan...For how long do you want to point the scanner at a barcode without it reporting a "double" scan?

    The only way to maybe take care of this (seeing that you want to scan the same code consecutively) is to perhaps build a throttle/time delay into the original code that you can set from the B4A code.
     
    lemonisdead and DonManfred like this.
  10. Mahares

    Mahares Well Known Member Licensed User

    Johan Schoeman likes this.
  11. incendio

    incendio Well-Known Member Licensed User

    I have no knowledge of Java, so could not write time delay in the original code.

    I have tried though in B4A, to stop scanner after scan result, delay a few nano seconds
    and then start the scanner again, but performance is bad, to slow.

    Yes, i have tried this one also, same problem also for unknown reason, this one take a very slow to get a result, so didn't use it.

    The ZBAR is very fast, a pitty that has this problem, can't use it for my app.

    For now, I use third party barcode app, Barcode Scanner+ from play store, a bit slower for a batch scanning, but very accurate.

    I noticed, on Barcode Scanner+, scanning between barcodes was not instantly, there was a delay but this delay is still very acceptable, may be only 0.5 sec or less. On ZBAR, scanning was almost instantly.
     
    Last edited: Feb 27, 2016
  12. Johan Schoeman

    Johan Schoeman Expert Licensed User

    ZBAR does not have a problem....you are moving too slow for ZBAR....:) Move faster!
     
    lemonisdead and DonManfred like this.
  13. incendio

    incendio Well-Known Member Licensed User

    Wow, that was quick response, i was edited my response :)
     
    Johan Schoeman likes this.
  14. Johan Schoeman

    Johan Schoeman Expert Licensed User

    I will see if I can add a throttle/time delay to the original code.
     
    incendio likes this.
  15. incendio

    incendio Well-Known Member Licensed User

    Thanks a lot in advance my friend :)
     
  16. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Try with this set of library files:
    https://www.dropbox.com/s/2jqlzljr5rivfra/ZbarBarcodeScannerLib28Feb.zip?dl=0

    Code:
    zb1.LaserColor = Colors.Yellow
    zb1.MaskColor = 
    Colors.ARGB(15000200)
    zb1.BorderColor = 
    Colors.Magenta
    zb1.BorderStrokeWidth = 
    5
    zb1.BorderLineLength = 
    40
    zb1.Visible = 
    False
      
    zb1.ScanDelay = 
    200
    Set the delay to whatever will suite your needs.
     
  17. incendio

    incendio Well-Known Member Licensed User

    Works like charm.

    There are 2 things :
    1) I didn't add zb1.ScanDelay = 200, but it seem there is already default delay, scanning next barcode wasn't instantly, but it was enough delay so that it won't scanning twice accidentally
    2) There was no beep sound on success scan, but this is easy to solve, I added beeper

    At last, I have a working barcode scanner inside B4A, thanks to you :)
     
    Johan Schoeman likes this.
  18. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Yes, I have set the default delay as 100ms
    I have removed the sound from the library as it is simple to add it via B4A code
     
    Last edited: Feb 29, 2016
    incendio likes this.
  19. ALMRosario

    ALMRosario New Member Licensed User

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

    #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 Button1 As Button
        
    Private Button2 As Button
        
    Private Button3 As Button
        
    Private ZbarBarcodeScanner1 As ZbarBarcodeScanner
        
    Private Label1 As Label
    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("Layout1")
    Activity.LoadLayout("Main")
    ZbarBarcodeScanner1.LaserColor = 
    Colors.Yellow
        ZbarBarcodeScanner1.MaskColor = 
    Colors.ARGB(15000200)
        ZbarBarcodeScanner1.BorderColor = 
    Colors.Magenta
        ZbarBarcodeScanner1.BorderStrokeWidth = 
    5
        ZbarBarcodeScanner1.BorderLineLength = 
    40
        ZbarBarcodeScanner1.Visible = 
    False
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
    ZbarBarcodeScanner1.stopScanner
        ZbarBarcodeScanner1.Visible = 
    False
    End Sub


    Sub Button1_Click
        ZbarBarcodeScanner1.Visible=
    True
        ZbarBarcodeScanner1.startScanner
    End Sub

    Sub Button2_Click
        ZbarBarcodeScanner1.Visible=
    False
        ZbarBarcodeScanner1.stopScanner
    End Sub

    Sub Button3_Click
        ZbarBarcodeScanner1.toggleFlash
    End Sub

    Sub ZbarBarcodeScanner1_scan_result(scantext As String, scanformat As String)
        Label1.Text=scantext
    End Sub

    When I click on the button 1 is closed, what it is wrong ?
     
  20. Johan Schoeman

    Johan Schoeman Expert Licensed User

    What is the error in the Log? Please post it.
    But before you do so...read post #1 thoroughly - especially the sentence in BOLD.
     
    Last edited: Mar 19, 2016
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice