B4J Tutorial OkapiBarcode (ZINT)

It is based on this Github project. Have stripped it bare and compiled the code to a new Jar (OkapiJHS.jar) so that it will only handle DataMatrix barcodes (a pm request that I have done for a member of the forum). The library brings back X and Y positions as well as width and height of the BLACK modules that I then use to draw the modules of the DataMatrix 2D barcode on a B4J canvas.

Attached the B4J sample project (it should also work in B4A - have not tested it for B4A) and the JAR - copy the Jar to your additional library folder.

Sample code:

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

#AdditionalJar: OkapiJHS

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Canvas1 As Canvas
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")                                             'it holds the Canvas that we will draw the DataMatrix on
    MainForm.Show
       
    Dim dm As JavaObject
    dm.InitializeNewInstance("uk.org.okapibarcode.backend.DataMatrix" , Null)            'initialize a new instance of DataMatrix using B4A / B4J JavaObject
   
    '******************************************************************************************************************************************************
    'THIS IS THE STRING THAT WILL BE CONVERTED INTO A DATAMATRIX 2D BARCODE
    dm.RunMethod("setContent", Array("Johan Schoeman" & CRLF & "19 Prospecton Rd" & CRLF & "Prospecton" & CRLF & "Durban" & CRLF & "South Africa" & CRLF & "Cell: 082 783 6322" & CRLF & "email: [email protected]" & CRLF & "The World" & CRLF & "The Universe" & CRLF & "The Milky Way"))
    '*********************************************************************************************************************************************************
   
    Dim rec As List = dm.RunMethod("getRectangles", Null)                                 'get the List of rectangles that need to be drawn of the Canvas
    For i = 0 To rec.Size - 1
        Log(rec.Get(i))                                                                   'Log the entries in the list
    Next
   
    Dim maxsize As Int  = 2500                                                            'this will allow 2500 entries in the list that will hold X, Y, WIDTH, HEIGHT values
    Dim coordx(maxsize) As Int
    Dim coordy(maxsize) As Int
    Dim wdth(maxsize) As Int
    Dim hght(maxsize) As Int
   
    For i = 0 To maxsize - 1
        coordx(i) = -1
        coordy(i) = -1
        wdth(i) = -1
        hght(i) = -1
    Next
   
    For i = 0 To rec.Size - 1
        Dim str As String = rec.Get(i)
        str = str.Replace("Rectangle[", "")
        str = str.Replace("]", "")
        Dim components(4) As String = Regex.Split("," , str)
        components(0) = components(0).Replace("x=", "")
        components(1) = components(1).Replace("y=", "")
        components(2) = components(2).Replace("width=", "")
        components(3) = components(3).Replace("height=", "")
        Dim xpos, ypos, modw, modh As Int
        xpos = components(0).As(Int)
        ypos = components(1).As(Int)
        modw = components(2).As(Int)
        modh = components(3).As(Int)
       
        coordx(i) = xpos
        coordy(i) = ypos
        wdth(i) = modw
        hght(i) = modh  
    Next
   
    Dim barwidth As Double = 10dip
    Dim lft As Double = 50dip
    Dim tp As Double = 50dip
   
    For i = 0 To maxsize - 1
        If coordx(i) <> -1 Then
            lft = lft + coordx(i) * barwidth
            tp = tp + coordy(i) * barwidth
            Canvas1.DrawRect(lft, tp, barwidth * wdth(i), barwidth * hght(i), fx.Colors.Black, True, 0)
            lft = 50dip
            tp = 50dip
        End If
    Next
   
   
End Sub



1679151550067.png
 

Attachments

Last edited:
The jar is also working in B4A

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

#AdditionalJar: OkapiJHS

#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.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    Dim dm As JavaObject
    dm.InitializeNewInstance("uk.org.okapibarcode.backend.DataMatrix" , Null)            'initialize a new instance of DataMatrix using B4A / B4J JavaObject
    
    '******************************************************************************************************************************************************
    'THIS IS THE STRING THAT WILL BE CONVERTED INTO A DATAMATRIX 2D BARCODE
    dm.RunMethod("setContent", Array("Johan Schoeman" & CRLF & "19 Prospecton Rd" & CRLF & "Prospecton" & CRLF & "Durban" & CRLF & "South Africa" & CRLF & "Cell: 082 783 6322" & CRLF & "email: [email protected]" & CRLF & "The World" & CRLF & "The Universe" & CRLF & "The Milky Way"))
    '*********************************************************************************************************************************************************
    
    Dim rec As List = dm.RunMethod("getRectangles", Null)                                 'get the List of rectangles that need to be drawn of the Canvas
    For i = 0 To rec.Size - 1
        Log(rec.Get(i))                                                                   'Log the entries in the list
    Next
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

1679153876357.png
 
How do use the canvas in B4A ?
 
How do use the canvas in B4A ?
Very "primitive" sample for B4A - you can "renew" it by making use of the B4X libs.

1.png
 

Attachments

An update - it has DataMatrix, QRcode, and AztecCode. Library (jar) can be used in B4J and B4A. Copy the attached jar to your B4A and/or B4J library folder(s).

1679326246401.png


[CODE lang="b4x" title="B4J code"]#Region Project Attributes
#MainFormWidth: 800
#MainFormHeight: 800
#End Region

#AdditionalJar: OkapiJHS

Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private Canvas1 As Canvas
Private Canvas2 As Canvas
Private Canvas3 As Canvas
End Sub

Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1") 'it holds the Canvas that we will draw the DataMatrix on
MainForm.Show

'****** DATAMATRIX ************

Dim dm As JavaObject
dm.InitializeNewInstance("uk.org.okapibarcode.backend.DataMatrix" , Null) 'initialize a new instance of DataMatrix using B4A / B4J JavaObject

'******************************************************************************************************************************************************
'THIS IS THE STRING THAT WILL BE CONVERTED INTO A DATAMATRIX 2D BARCODE
dm.RunMethod("setContent", Array("Hello B4J/B4A - This is Okapi DataMatrix. It is one of the range of 1D and 2D barcodes that can be created with OkapiBarcode"))
'*********************************************************************************************************************************************************

Dim rec As List = dm.RunMethod("getRectangles", Null) 'get the List of rectangles that need to be drawn of the Canvas
For i = 0 To rec.Size - 1
Log(rec.Get(i)) 'Log the entries in the list
Next

Dim maxsize As Int = 2500 'this will allow 2500 entries in the list that will hold X, Y, WIDTH, HEIGHT values
Dim coordx(maxsize) As Int
Dim coordy(maxsize) As Int
Dim wdth(maxsize) As Int
Dim hght(maxsize) As Int

For i = 0 To maxsize - 1
coordx(i) = -1
coordy(i) = -1
wdth(i) = -1
hght(i) = -1
Next

For i = 0 To rec.Size - 1
Dim str As String = rec.Get(i)
str = str.Replace("Rectangle[", "")
str = str.Replace("]", "")
Dim components(4) As String = Regex.Split("," , str)
components(0) = components(0).Replace("x=", "")
components(1) = components(1).Replace("y=", "")
components(2) = components(2).Replace("width=", "")
components(3) = components(3).Replace("height=", "")
Dim xpos, ypos, modw, modh As Int
xpos = components(0).As(Int)
ypos = components(1).As(Int)
modw = components(2).As(Int)
modh = components(3).As(Int)

coordx(i) = xpos
coordy(i) = ypos
wdth(i) = modw
hght(i) = modh
Next

Dim barwidth As Double = 5dip
Dim lft As Double = 50dip
Dim tp As Double = 50dip

For i = 0 To maxsize - 1
If coordx(i) <> -1 Then
lft = lft + coordx(i) * barwidth
tp = tp + coordy(i) * barwidth
Canvas1.DrawRect(lft, tp, barwidth * wdth(i), barwidth * hght(i), fx.Colors.Black, True, 0)
lft = 50dip
tp = 50dip
End If
Next


'****** QRCODE ************

Dim qr As JavaObject
qr.InitializeNewInstance("uk.org.okapibarcode.backend.QrCode" , Null) 'initialize a new instance of DataMatrix using B4A / B4J JavaObject

'******************************************************************************************************************************************************
'THIS IS THE STRING THAT WILL BE CONVERTED INTO A QR 2D BARCODE
qr.RunMethod("setContent", Array("Hello B4J/B4A - This is Okapi QR Code. It is one of the range of 1D and 2D barcodes that can be created with OkapiBarcode"))
'*********************************************************************************************************************************************************

Dim rec As List = qr.RunMethod("getRectangles", Null) 'get the List of rectangles that need to be drawn of the Canvas
For i = 0 To rec.Size - 1
Log(rec.Get(i)) 'Log the entries in the list
Next

Dim maxsize As Int = 2500 'this will allow 2500 entries in the list that will hold X, Y, WIDTH, HEIGHT values
Dim coordx(maxsize) As Int
Dim coordy(maxsize) As Int
Dim wdth(maxsize) As Int
Dim hght(maxsize) As Int

For i = 0 To maxsize - 1
coordx(i) = -1
coordy(i) = -1
wdth(i) = -1
hght(i) = -1
Next

For i = 0 To rec.Size - 1
Dim str As String = rec.Get(i)
str = str.Replace("Rectangle[", "")
str = str.Replace("]", "")
Dim components(4) As String = Regex.Split("," , str)
components(0) = components(0).Replace("x=", "")
components(1) = components(1).Replace("y=", "")
components(2) = components(2).Replace("width=", "")
components(3) = components(3).Replace("height=", "")
Dim xpos, ypos, modw, modh As Int
xpos = components(0).As(Int)
ypos = components(1).As(Int)
modw = components(2).As(Int)
modh = components(3).As(Int)

coordx(i) = xpos
coordy(i) = ypos
wdth(i) = modw
hght(i) = modh
Next

Dim barwidth As Double = 5dip
Dim lft As Double = 50dip
Dim tp As Double = 50dip

For i = 0 To maxsize - 1
If coordx(i) <> -1 Then
lft = lft + coordx(i) * barwidth
tp = tp + coordy(i) * barwidth
Canvas2.DrawRect(lft, tp, barwidth * wdth(i), barwidth * hght(i), fx.Colors.Black, True, 0)
lft = 50dip
tp = 50dip
End If
Next



'****** AZTEC ************

Dim az As JavaObject
az.InitializeNewInstance("uk.org.okapibarcode.backend.AztecCode" , Null) 'initialize a new instance of DataMatrix using B4A / B4J JavaObject

'******************************************************************************************************************************************************
'THIS IS THE STRING THAT WILL BE CONVERTED INTO A AZTEC 2D BARCODE
az.RunMethod("setContent", Array("Hello B4J/B4A - This is Okapi Aztec Code. It is one of the range of 1D and 2D barcodes that can be created with OkapiBarcode"))
'*********************************************************************************************************************************************************

Dim rec As List = az.RunMethod("getRectangles", Null) 'get the List of rectangles that need to be drawn of the Canvas
For i = 0 To rec.Size - 1
Log(rec.Get(i)) 'Log the entries in the list
Next

Dim maxsize As Int = 2500 'this will allow 2500 entries in the list that will hold X, Y, WIDTH, HEIGHT values
Dim coordx(maxsize) As Int
Dim coordy(maxsize) As Int
Dim wdth(maxsize) As Int
Dim hght(maxsize) As Int

For i = 0 To maxsize - 1
coordx(i) = -1
coordy(i) = -1
wdth(i) = -1
hght(i) = -1
Next

For i = 0 To rec.Size - 1
Dim str As String = rec.Get(i)
str = str.Replace("Rectangle[", "")
str = str.Replace("]", "")
Dim components(4) As String = Regex.Split("," , str)
components(0) = components(0).Replace("x=", "")
components(1) = components(1).Replace("y=", "")
components(2) = components(2).Replace("width=", "")
components(3) = components(3).Replace("height=", "")
Dim xpos, ypos, modw, modh As Int
xpos = components(0).As(Int)
ypos = components(1).As(Int)
modw = components(2).As(Int)
modh = components(3).As(Int)

coordx(i) = xpos
coordy(i) = ypos
wdth(i) = modw
hght(i) = modh
Next

Dim barwidth As Double = 5dip
Dim lft As Double = 50dip
Dim tp As Double = 50dip

For i = 0 To maxsize - 1
If coordx(i) <> -1 Then
lft = lft + coordx(i) * barwidth
tp = tp + coordy(i) * barwidth
Canvas3.DrawRect(lft, tp, barwidth * wdth(i), barwidth * hght(i), fx.Colors.Black, True, 0)
lft = 50dip
tp = 50dip
End If
Next


End Sub
[/CODE]
 

Attachments

Fantastic work.
How can you set the setForceMode for the Datamatrix barcode to ForceMode.SQUARE from B4A ?
dm.RunMethod("setForceMode", Array(????????))
 
Fantastic work.
How can you set the setForceMode for the Datamatrix barcode to ForceMode.SQUARE from B4A ?
dm.RunMethod("setForceMode", Array(????????))
You can do it with JavaObject.

Dim fm as JavaObject
fm.initializeStatic("uk.org.okapibarcode.backend.DataMatrix.ForceMode")
dm.Runmethod("setForceMode",array(fm.getField("SQUARE")))

Something like the above. Not at my laptop now and did the above from memory. Will check sometime tomorrow.
 
Last edited:
Run the attached B4J project - you should get the below and can explore the various options that OkapiBarcode has on offer. Have compiled all of this ( Github project) into a jar and then kick-starting OkapiUi (a class in the attached jar) from the B4J project.

1. Unzip the Jar from the attached zip file and copy it to your additional library folder
2. Copy attached jcommander.jar to your B4J additional library folder.
3. B4J sample project attached to "kick-start" the demo program

NOTE: Make sure you kill the B4J process once done fiddling around with the sample project

1679391543054.png
 

Attachments

Have added EAN13 (bottom left) and ITF14 (bottom right). Copy the attached Jar to your B4J additional library folder.

1680348110136.png
 

Attachments

Back
Top