B4J Code Snippet [PyBridge] Create barcodes

Based on: https://python-barcode.readthedocs.io/en/stable/getting-started.html

Usage:
Code:
Wait For (CreateBarcode("code128", "abcdef")) Complete (Result As B4XBitmap)
ImageView1.SetBitmap(Result)


Code:
'dependencies: pip install python-barcode "python-barcode[images]"
'Types: ['codabar', 'code128', 'code39', 'ean', 'ean13', 'ean13-guard', 'ean14', 'ean8', 'ean8-guard', 'gs1', 'gs1_128', 'gtin', 'isbn', 'isbn10', 'isbn13', 'issn', 'itf', 'jan', 'nw-7', 'pzn', 'upc', 'upca']
Private Sub CreateBarcode (CodeType As String, Code As String) As ResumableSub
    Dim BytesIO As PyWrapper = Py.ImportModuleFrom("io", "BytesIO")
    Dim Barcode As PyWrapper = Py.ImportModule("barcode")
    Dim ImageWriter As PyWrapper = Py.ImportModuleFrom("barcode.writer", "ImageWriter")
    Barcode.GetField("PROVIDED_BARCODES").Print2("Types:", "", False)
    Dim EAN As PyWrapper = Barcode.Run("get_barcode_class").Arg(CodeType)
    Dim rv As PyWrapper = BytesIO.Call
    EAN.Call.Arg(Code).ArgNamed("writer", ImageWriter.Call).Run("write").Arg(rv)
    Wait For (rv.Fetch) Complete (rv As PyWrapper)
    Return Py.ImageFromBytes(rv.Value)
End Sub

1739721584636.png


Ctrl + click on the Python shell link at the top of B4XMainPage and run:
pip install python-barcode "python-barcode[images]"
 
My first shot at Pybridge.. .

It is based on this web posting (could not with a quick browse find a PyBridge QR code generator on the forum):
Have added it to @Erel 's sample code above



pip install qrcode



1764404629012.png


B4X:
    ...
    ...
  
  
    Wait For (CreateBarcode("code128", "abcdef")) Complete (Result As B4XBitmap)
    ImageView1.SetBitmap(Result)
  

    Wait For (CreateQRCode("Hello Johan")) Complete (Result As B4XBitmap)
    ImageView2.SetBitmap(Result)
  
    ...
    ...
  
    Private Sub CreateBarcode (CodeType As String, Code As String) As ResumableSub
    Dim BytesIO As PyWrapper = Py.ImportModuleFrom("io", "BytesIO")
    Dim Barcode As PyWrapper = Py.ImportModule("barcode")
    Dim ImageWriter As PyWrapper = Py.ImportModuleFrom("barcode.writer", "ImageWriter")
    Barcode.GetField("PROVIDED_BARCODES").Print2("Types:", "", False)
    Dim EAN As PyWrapper = Barcode.Run("get_barcode_class").Arg(CodeType)
    Dim rv As PyWrapper = BytesIO.Call
    EAN.Call.Arg(Code).ArgNamed("writer", ImageWriter.Call).Run("write").Arg(rv)
    Wait For (rv.Fetch) Complete (rv As PyWrapper)
    Return Py.ImageFromBytes(rv.Value)
End Sub

Private Sub CreateQRCode(Code As String) As ResumableSub
    Dim BytesIO As PyWrapper = Py.ImportModuleFrom("io", "BytesIO")
    Dim QRCode As PyWrapper = Py.ImportModule("qrcode")
    Dim Img As PyWrapper = QRCode.Run("make").Arg(Code)
    Dim rv As PyWrapper = BytesIO.Call
    Img.Run("save").Arg(rv)
    Wait For (rv.Fetch) Complete (rv As PyWrapper)
    Return Py.ImageFromBytes(rv.Value)
End Sub
 

Attachments

Last edited:
....and this will allow you to set Version, Box Size, and Border. You can also change the hard coded colors for the fill_color and back_color:

B4X:
Private Sub CreateQRCode(Code As String) As ResumableSub
    Dim BytesIO As PyWrapper = Py.ImportModuleFrom("io", "BytesIO")
    Dim qrcode As PyWrapper = Py.ImportModule("qrcode")
    Dim qr As PyWrapper = qrcode.Run("QRCode").ArgNamed("version", 5).ArgNamed("box_size", 10).ArgNamed("border", 5)
    qr.Run("add_data").Arg(Code)
    Dim Img As PyWrapper = qr.Run("make_image").ArgNamed("fill_color", "white").ArgNamed("back_color" , "black")
    Dim rv As PyWrapper = BytesIO.Call
    Img.Run("save").Arg(rv)
    Wait For (rv.Fetch) Complete (rv As PyWrapper)
    Return Py.ImageFromBytes(rv.Value)
End Sub

1764413961860.png
 

Attachments

This will allow you to also set the error correction level to L, M, Q, H
B4X:
Private Sub CreateQRCode(Code As String) As ResumableSub
    Dim BytesIO As PyWrapper = Py.ImportModuleFrom("io", "BytesIO")
    Dim qrcode As PyWrapper = Py.ImportModule("qrcode")
    Dim qr As PyWrapper = qrcode.Run("QRCode").ArgNamed("version", 9).ArgNamed("box_size", 10).ArgNamed("border", 5).ArgNamed("error_correction", qrcode.GetField("ERROR_CORRECT_Q"))
    qr.Run("add_data").Arg(Code)
    Dim Img As PyWrapper = qr.Run("make_image").ArgNamed("fill_color", "white").ArgNamed("back_color" , "black")
    Dim rv As PyWrapper = BytesIO.Call
    Img.Run("save").Arg(rv)
    Wait For (rv.Fetch) Complete (rv As PyWrapper)
    Return Py.ImageFromBytes(rv.Value)
End Sub
 
Choose any of the following in the code to change the module shape:

B4X:
'RoundedModuleDrawer, VerticalBarsDrawer, HorizontalBarsDrawer, CircleModuleDrawer, GappedSquareModuleDrawer, SquareModuleDrawer

[CODE lang="b4x" title="PyBridge - qrcode"]Private Sub CreateQRCode(Code As String) As ResumableSub
Dim BytesIO As PyWrapper = Py.ImportModuleFrom("io", "BytesIO")
Dim qrcode As PyWrapper = Py.ImportModule("qrcode")
Dim styledPilIMage As PyWrapper = Py.ImportModuleFrom("qrcode.image.styledpil", "StyledPilImage")
'RoundedModuleDrawer, VerticalBarsDrawer, HorizontalBarsDrawer, CircleModuleDrawer, GappedSquareModuleDrawer, SquareModuleDrawer
Dim RoundedModuleDrawerType As PyWrapper = Py.ImportModuleFrom("qrcode.image.styles.moduledrawers.pil", "GappedSquareModuleDrawer")
Dim radialGradientColorMask As PyWrapper = Py.ImportModuleFrom("qrcode.image.styles.colormasks", "RadialGradiantColorMask")

Dim qr As PyWrapper = qrcode.Run("QRCode").ArgNamed("box_size", 10).ArgNamed("border", 4).ArgNamed("error_correction", qrcode.GetField("ERROR_CORRECT_L"))
qr.Run("add_data").Arg(Code)
qr.Run("make").ArgNamed("fit", True)
Dim Img As PyWrapper = qr.Run("make_image").ArgNamed("fill_color", "black").ArgNamed("back_color" , "white").ArgNamed("image_factory", styledPilIMage).ArgNamed("module_drawer", RoundedModuleDrawerType.Call).ArgNamed("color_mask", radialGradientColorMask.Call)
Dim rv As PyWrapper = BytesIO.Call
Img.Run("save").Arg(rv)
Wait For (rv.Fetch) Complete (rv As PyWrapper)
Return Py.ImageFromBytes(rv.Value)
End Sub[/CODE]
 

Attachments

F.Y.I. For those who are not familiar with Python - AI tools are extremely familiar with Python and its libraries, including numpy, pandas, scikit-learn, keras, tensorflow, and other machine learning modules. I have not found it hallucinating in these areas. So you can do quick and informative searches. Be specific in your questions. If you don't specify, it won't volunteer to give you details not asked for. If AI generates Python code, it is syntactically correct, but needs to be adapted to your needs - so don't copy blindly.

I fondly think back on the days of going to the book store, finding an up-to-date manual, and searching for code examples.
 
F.Y.I. For those who are not familiar with Python - AI tools are extremely familiar with Python and its libraries, including numpy, pandas, scikit-learn, keras, tensorflow, and other machine learning modules. I have not found it hallucinating in these areas. So you can do quick and informative searches. Be specific in your questions. If you don't specify, it won't volunteer to give you details not asked for. If AI generates Python code, it is syntactically correct, but needs to be adapted to your needs - so don't copy blindly.

I fondly think back on the days of going to the book store, finding an up-to-date manual, and searching for code examples.
Although it seems to be somewhat related to using JavaObject in B4J, it will take a bit of time to get used to the "constructors" when using PyBridge. AI at present probably does not have sufficient samples to draw from to come up with sensible PyBridge code to use an underlying library. Just my HO.

Rgds

Johan
 
If you want to make a QR code with RadialGradiantColorMask:

1764604279704.png

The QR code above also uses the RoundedModuleDrawer.

Others:

1764604445537.png
1764604546245.png

1764604493267.png
1764604585561.png

B4X:
Private Sub CreateQRCode(Code As String) As ResumableSub
    Dim BytesIO As PyWrapper = Py.ImportModuleFrom("io", "BytesIO")
    Dim qrcode As PyWrapper = Py.ImportModule("qrcode")
    Dim styledPilIMage As PyWrapper = Py.ImportModuleFrom("qrcode.image.styledpil", "StyledPilImage")
    'RoundedModuleDrawer, VerticalBarsDrawer, HorizontalBarsDrawer, CircleModuleDrawer, GappedSquareModuleDrawer, SquareModuleDrawer
    Dim RoundedModuleDrawerType As PyWrapper = Py.ImportModuleFrom("qrcode.image.styles.moduledrawers.pil", "VerticalBarsDrawer")
    Dim radialGradientColorMask As PyWrapper = Py.ImportModuleFrom("qrcode.image.styles.colormasks", "RadialGradiantColorMask")

    Dim qr As PyWrapper = qrcode.Run("QRCode").ArgNamed("box_size", 10).ArgNamed("border", 4).ArgNamed("error_correction", qrcode.GetField("ERROR_CORRECT_L"))
    qr.Run("add_data").Arg(Code)
    qr.Run("make").ArgNamed("fit", True)            'automatically determine the size of the QR Code to fit the supplied data
    Dim Img As PyWrapper = qr.Run("make_image").ArgNamed("fill_color", "black") _
                                               .ArgNamed("back_color" , "white") _
                                               .ArgNamed("image_factory", styledPilIMage) _
                                               .ArgNamed("module_drawer", RoundedModuleDrawerType.Call) _  '
                                               .ArgNamed("color_mask", radialGradientColorMask.Call _     'apply a radialgradientcolor to the QR code...
                                               .ArgNamed("back_color", Array As Object (255,255,255)) _   'adjust color here
                                               .ArgNamed("edge_color", Array As Object(100, 100, 0)) _    'adjust color here
                                               .ArgNamed("center_color", Array As Object(255,0, 0)))      'adjust color here
    Dim rv As PyWrapper = BytesIO.Call
    Img.Run("save").Arg(rv)
    Wait For (rv.Fetch) Complete (rv As PyWrapper)
    Return Py.ImageFromBytes(rv.Value)
End Sub

The end of my explorations of the PyBridge QR Code....
 

Attachments

This adds ability to create PDF417 codes.

pip install pdf417
pip install pillow

1764774142238.png


[CODE lang="b4x" title="PDF417"]Private Sub CreatePDF417(Code As String) As ResumableSub
Dim BytesIO As PyWrapper = Py.ImportModuleFrom("io", "BytesIO")
' Dim pdf417 As PyWrapper = Py.ImportModule("pdf417")
Dim img As PyWrapper = Py.ImportModuleFrom("PIL", "Image")

Dim encode As PyWrapper = Py.ImportModuleFrom("pdf417", "encoding")
Dim render As PyWrapper = Py.ImportModuleFrom("pdf417", "rendering")

Dim codes As PyWrapper = encode.run("encode").Arg(Code).ArgNamed("columns", 6).ArgNamed("security_level", 2)

img = render.Run("render_image").Arg(codes).ArgNamed("scale", 5) _
.ArgNamed("ratio", 3) _
.ArgNamed("padding", 5) _
.ArgNamed("fg_color", "Indigo") _
.ArgNamed("bg_color", "#ddd")

Dim rv As PyWrapper = BytesIO.Call
img.Run("save").Arg(rv).ArgNamed("format", "PNG")
Wait For (rv.Fetch) Complete (rv As PyWrapper)
Return Py.ImageFromBytes(rv.Value)
End Sub[/CODE]
 

Attachments

Last edited:
This adds DataMatrix 2D codes:

pip install pystrich

1764955412925.png

[CODE lang="b4x" title="DataMatrix"]Private Sub CreateDataMatrix(Code As String) As ResumableSub
Dim encode As PyWrapper = Py.ImportModuleFrom("pystrich.datamatrix", "DataMatrixEncoder")
encode = encode.Call.Arg(Code)
Dim data As PyWrapper = encode.Run("get_imagedata")
Wait For (data.Fetch) Complete (data As PyWrapper)
Return Py.ImageFromBytes(data.Value)
End Sub[/CODE]
 

Attachments

Last one - added Aztec Codes.

pip install aztec_code_generator

1765001369603.png


B4X:
Private Sub CreateAztecCode(Code As String) As ResumableSub  

    Dim BytesIO As PyWrapper = Py.ImportModuleFrom("io", "BytesIO")
    Dim encode As PyWrapper = Py.ImportModuleFrom("aztec_code_generator", "AztecCode")
    Dim img As PyWrapper = Py.ImportModuleFrom("PIL", "Image")
    img = encode.Call.Arg(Code)
    Dim rv As PyWrapper = BytesIO.Call
    img.Run("save").Arg(rv).ArgNamed("format", "PNG")
    Wait For (rv.Fetch) Complete (rv As PyWrapper)
    Return Py.ImageFromBytes(rv.Value)
End Sub
 

Attachments

Last edited:
Back
Top