B4A Library dgBarcode - an all-B4A barcodes library

EDIT: updated to version 0.12 (see below)

Hi all,

please find attached a barcode generation library entirely based on B4A code.
Current version is alpha so it is limited to a couple of symbologies (EAN/UPC).
I'm using it as a test-bed for a more general solution where the strict adherence to each symbology's tech specifications should be a must.
The lib interface is not final, so please read release notes when updating to a newer version.

A simple example about dgBarcode usage to display a barcode:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim bc As cls_barcode
   bc.Initialize

   Dim img1 As ImageView
   img1.Initialize("")
   Activity.AddView(img1,0dip,20dip,bc.CodeWidth(bc.Symbologies.EAN13),bc.CodeHeight(bc.Symbologies.EAN13))
   bc.DrawBarCode(bc.Symbologies.EAN13,"7617027005456",img1)
End Sub

A second example, useful to generate a barcode without displaying it :
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim bc As cls_barcode
   bc.Initialize

   Dim bm1 As Bitmap
   bm1.Initialize3(bc.GenerateBitmap(bc.Symbologies.EAN13,"7617027005456"))
end sub

And finally an example on how to use a few optional properties
B4X:
   'let's check the validity of a pair of codes
Log(bc.IsCodeValid(bc.Symbologies.UPCA,"614141012343")) 'valid
Log(bc.IsCodeValid(bc.Symbologies.UPCA,"614151012343")) 'wrong (check digit doesn't match)
'Let's compute the check digit for a given code
Log(bc.ComputeCheckDigit(bc.Symbologies.EAN13,"761702700545"))   'returns 6
'Let's modify colors to draw the code and its scale factor
bc.SetDrawingColors(Colors.Cyan,Colors.Yellow)                   'very poor contrast
bc.ScaleFactor = 2.0                                             'maximum scale factor

Feel free to suggest improvements and alternative design strategies. Enjoy.

Umberto

Version 0.12
- added method GenerateBitmap to return a bitmap ready to be saved in a file or DB

Version 0.11
- added support for UPC-A symbology
- added method IsCodeValid to check for code integrity
- added catch-all drawing for invalid codes (a light color rectangle with two crossing dark color lines)

Version 0.10
initial version; support for EAN symbology only
 

Attachments

  • dgBarcode_011.zip
    7.9 KB · Views: 434
  • dgBarcode_012.zip
    8.4 KB · Views: 602
Last edited:

udg

Expert
Licensed User
Longtime User
Library reference

dgBarCode
Author:
UDG
Version: 0.12
Last Modified: 23.05.2014
  • BarcodeType
    Fields:
    • NONE As Int
    • EAN13 As Int
    • EAN8 As Int
    • UPCA As Int
  • cls_barcode
    Fields:
    • Symbologies As BarcodeType
    Methods:
    • Initialize As String
      Initializes the object.
    • IsInitialized As Boolean
      Tests whether the object has been initialized.
    • CodeHeight (aSymbology As Int) As Float
      Returns needed height for an ImageView to contain the desidered code symbology
      aSymbology - one of the lib's recognized symbologies (see Symbologies)
    • CodeWidth (aSymbology As Int) As Float
      Returns needed width for an ImageView to contain the desidered code symbology
      aSymbology - one of the lib's recognized symbologies (see Symbologies)
    • ComputeCheckDigit (aSymbology As Int, aCode As String) As Char
      Computes the appropriate check digit for the code symbology and code value passed as parameters
      aSymbology - one of the lib's recognized symbologies (see Symbologies)
      aCode - the code value for which to calculate the appropriate check digit
    • DrawBarcode (aSymbology As Int, aCode As String, anArea As ImageViewWrapper) As String
      Draws a barcode using the specified symbology on the given view area
      aSymbology - one of the lib's recognized symbologies (see Symbologies)
      aCode - the code value to represent as a barcode
      anArea - an ImageView object to host the barcode drawing in its Bitmap property
    • GenerateBitmap (aSymbology As Int, aCode As String) As BitmapWrapper
      Returns a bitmap filled by a barcode of the appropriate symbology
      aSymbology - one of the lib's recognized symbologies (see Symbologies)
      aCode - the code value to represent as a barcode
    • IsCodeValid (aSymbology As Int, aCode As String) As Boolean
      Checks whether the given code (complete of its check digit, if any) is valid for the specified symbology
      aSymbology - one of the lib's recognized symbologies (see Symbologies)
      aCode - the code value to check for validity
    • SetDrawingColors (BarColor As Int, SpaceColor As Int) As String
      Sets the colors to draw the barcode
      BarColor - color used for dark bars
      SpaceColor - color used for light bars (spaces)
    Properties:
    • ScaleFactor As Void
      Sets the barcode scale factor.
      Must be from 0.80 To 2.0 In 0.05 steps. Default: 1.0
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Ciao Umberto,

Come me buona :cool:
 

udg

Expert
Licensed User
Longtime User
Hi GMan,
thanks for your kind comment.

I am evaluating the option to restrict the symbology type indication to a specific set-up method (along with the scale factor setting) instead of having it as a parameter in most subs.
Something like SetupCurrentProperties(CurrentSymbology, CurrentScaleFactor).
This way I could call this sub once and then have a few barcodes generated with that same symbology and scale factor; after that I could call it again to produce a different set of codes and so on.
What do you think?

Umberto
 

GMan

Well-Known Member
Licensed User
Longtime User
You mean something like this ?

B4X:
SetupCurrentProperties(CurrentSymbology, CurrentScaleFactor)
   Dim CurrentScaleFactor as ...
   Dim CurrentSymbology as ....
   CurrentScaleFactor = ...
   CurrentSymbology = ...
End Sub
 

udg

Expert
Licensed User
Longtime User
Well, that will be more or less how the new sub will be designed.
My proposal is intended as a semplification of the library interface, so to use it like this:
B4X:
Sub Activity_Create(FirstTime AsBoolean)  'or any other sub you like
 Dim bc As cls_barcode
 bc.Initialize
 'the following proposed method sets internal library values for symbology and scale factor to use
 'these stay current until an eventual subsequent call to same method with new params
 bc.SetCurrentProperties(bc.Symbologies.EAN8, 1.0) 'following lib commands will assume EAN 8 and 1.0 scale factor
 for i = 0 to 3 do
   Dim img1 AsImageView
   img1.Initialize("")
   Activity.AddView(img1,i*CodeWidth,20dip,bc.CodeWidth,bc.CodeHeight) 'CodeW and CodeH know the current symbology
   NextCodeToDraw = bcode(i)    ' or read it from DB, file, anywhere else
   bc.DrawBarCode(NextVodeToDraw,img1)
 next
 bc.SetCurrentProperties(bc.Symbologies.EAN13, 2.0) 'following commands will assume EAN 13 and 2.0 scale factor
 for i = 4 to 7 do
   Dim img1 AsImageView
   img1.Initialize("")
   Activity.AddView(img1,i*CodeWidth,20dip,bc.CodeWidth,bc.CodeHeight) 'CodeW and CodeH know the current symbology
   NextCodeToDraw = bcode(i)    ' or read it from DB, file, anywhere else
   bc.DrawBarCode(NextVodeToDraw,img1)
 next
End Sub
In the example above we assume a display large enough to show 8 barcodes side-by-side. It was intended to demostrate how the new proposed method could simplify the existing methods.

Another idea is to generate the barcodes against a Bitmap with a new method DrawOnBitmap(aCode, aBitMap) useful when you just need the barcode but don't need to show it (e.g. to record its image in a file or send it to a printer).

Umberto
 

udg

Expert
Licensed User
Longtime User
I'm almost done with version 0.12 where you'll find some other useful stuff.

Umberto
 

GMan

Well-Known Member
Licensed User
Longtime User
OK
Status: Waiting.... ;)
 

udg

Expert
Licensed User
Longtime User
I expect the new method GenerateBitmap to be useful even as a substitute for former DrawBarcode method.
In fact you can use GenerateBitmap even to display the generated barcode, like this:
B4X:
Dim bm1 As Bitmap
bm1.Initialize3(bc.GenerateBitmap(bc.Symbologies.EAN13,"7617027005456"))
Dim img2 As ImageView
img2.Initialize("")
img2.Gravity=Gravity.NO_GRAVITY
img2.Bitmap=bm1
Activity.AddView(img2,300dip,20dip,bm1.Width,bm1.Height)

Umberto
 

GMan

Well-Known Member
Licensed User
Longtime User
Still using...thought you'll bring something "better" :rolleyes:
 

udg

Expert
Licensed User
Longtime User
As anticipated in some previous posts, I'm working on the library interface (or framework if you prefer).
I'd like it to be simple and straightforward. This is why I'm not introducing new symbologies at the moment.
Currently I'm experimenting with direct printing to a LAN printer, so if successful a new method could be PrintCode(Bitmap) or something very close to it.
I'm still in doubt about the inclusion of the barcode symbology indication in every method ot the alternative to call a setting method when needed.

Umberto
 

Beja

Expert
Licensed User
Longtime User
Hi Umberto
Old post.. but are you planning to port this important lib to B4J?
 

udg

Expert
Licensed User
Longtime User
Hi @Beja ,

thanks for defining my lib as "important" ! Until today I hadn't idea that so many B4xers downloaded it..
I didn't plan to port it to B4j, but since you asked for it (and it's pure B4A code) .. why not?
To be honest I thought that most B4xers were using Johan Schoeman's libs. Anyway, stay tuned..jdgBarcode is coming soon.

Umberto
 
Last edited:

udg

Expert
Licensed User
Longtime User
Hi all,

today I published a porting of dgBarCode to B4J. It's a quick one-to-one translation.
Both B4A and B4J projects will coexist from now on.

Enjoy.
 

BjoernB

Member
Licensed User
hi @udg

i just hope you're still watching this thread

i tried to create a barcode with this lib, but all i get is a white view with a black X across it.
i wanted to show the barcode on an existing imageview, called "ivBar" for ImageViewBarcode
so now this is my code

B4X:
    Dim bc As cls_barcode
    bc.Initialize

    bc.CodeWidth(ivBar.Height)
    bc.CodeHeight(ivBar.Width)
    bc.DrawBarCode(bc.Symbologies.EAN13,"123456789",ivBar)

can you tell me where's my mistake?
 

udg

Expert
Licensed User
Longtime User
I am on my mobile now so can't check, but an EAN13 code should be 13 chars long.
 

BjoernB

Member
Licensed User
I am on my mobile now so can't check, but an EAN13 code should be 13 chars long.
i tried that and changed my "12345..." to 13 chars, but it still won't work.
whenever you have time for it, would you take a look?

sorry to bother you with this

i'm using B4A version 9.80, testing it on an HTC U11 with android 8.0.0
 

udg

Expert
Licensed User
Longtime User
Sure, no problem.
Meanwhile, did you test it with the exact code example from post #1? Or any other valid EAN13 code?
BTW, the problem could be in the wrong usage of CodeWidth/Height functions. I copy one defionition from post #2
B4X:
[LIST]
[*]CodeHeight (aSymbology As Int) As Float
Returns needed height for an ImageView to contain the desidered code symbology
 aSymbology - one of the lib's recognized symbologies (see Symbologies)
[/LIST]
So it should be
B4X:
ivBar.Height = bc.CodeHeight(bc.Symbologies.EAN13)
Same for the width
 

BjoernB

Member
Licensed User
So it should be
B4X:
ivBar.Height = bc.CodeHeight(bc.Symbologies.EAN13)
Same for the width

seems this was the problem. now it works well. Thank you!
is there a format (so not EAN13 i guess) which displays a varying amount of characters?
 
Top