Android Example Creating 1D and 2D Barcodes using inline Java Code and the ZXING core library

The attached project has 10 classes that create the following 1D and 2D barcodes:

1. QR Code - 2D
2. Datamatrix - 2D
3. Aztec Codes - 2D
4. PDF417 - 2D
5. Code 128
6. EAN8
7. EAN13
8. Code 39
9. ITF
10. CODABAR

The classes use inline java code with calls to the ZXING core library to create and return the required bitmap. You can do the other bar codes that ZXING supports in a similar way by adding (and modifying)additional classes to the B4A project.

All the inline Java code comes from somewhere on the StackOverflow website.

It is fairly straight forward to convert it to B4J.

Posting:
1.B4A example project
2. core-3.2.1.jar (this is the ZXING core library) - copy it to your additional library folder.

Change the code in the classes to for eg pass foreground and background colors from the B4A code in the main activity (black and white has been hard coded within the inline Java code). Amend the B4A code accordingly to pass the colors to the inline Java code.

Some sample code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: BarcodeGenerator
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #AdditionalJar: core-3.2.1

#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.
  
    Dim qr As QRcode
    Dim ac As AztecCode
    Dim dm As DataMatrix
    Dim pdf As PDF417
    Dim c128 As Code128
    Dim ean_8 As EAN8
    Dim ean_13 As EAN13
    Dim c39 As Code39
    Dim itfc As ITF
    Dim cb As Codabar

  
    Dim bm As Bitmap

    Private ivqr As ImageView
    Private ivac As ImageView
    Private ivdm As ImageView
    Private ivpdf As ImageView
    Private ivc128 As ImageView
    Private ivean8 As ImageView
    Private ivean13 As ImageView
    Private ivc39 As ImageView
    Private ivitf As ImageView
    Private ivcb As ImageView
  
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")
  
    ac.Initialize
    qr.Initialize
    dm.Initialize
    pdf.Initialize
    c128.Initialize
    ean_8.Initialize
    ean_13.Initialize
    c39.Initialize
    itfc.Initialize
    cb.Initialize

    bm = qr.drawQRCode("B4A is a great tool that can be used for the development of Android apps.")
    ivqr.Bitmap = bm
  
    bm = ac.drawAztecCode("B4A is a great tool that can be used for the development of Android apps.")
    ivac.Bitmap = bm  
  
    bm = dm.drawDataMatrixCode("B4A is a great tool that can be used for the development of Android apps.")
    ivdm.Bitmap = bm  

     bm = pdf.drawPDF417("B4A is a great tool that can be used for the development of Android apps.")
    ivpdf.Bitmap = bm  
  
     bm = c128.drawCode128("123456789")
    ivc128.Bitmap = bm      
  
     bm = ean_8.drawEAN8("1234567")                  'Pass only first 7 digits - the checksum will be calculated in class EAN8
    ivean8.Bitmap = bm      
  
     bm = ean_13.drawEAN13("600106035982")           'Pass only first 12 digits - the checksum will be calculated in class EAN13
    ivean13.Bitmap = bm      
  
    bm = c39.drawCode39("60010")         
    ivc39.Bitmap = bm          
  
    bm = itfc.drawCodeITF("51834653")                'Number of digits must be even and at least 6 digits long
    ivitf.Bitmap = bm  
  
    bm = cb.drawCodabar("35754262")                
    ivcb.Bitmap = bm                                                      

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

1.png
 

Attachments

  • core-3.2.1.zip
    483.9 KB · Views: 1,626
  • b4aBarcodeGenerator.zip
    18.1 KB · Views: 1,461
Last edited:
Top