B4J Code Snippet [PyBridge] Create barcodes

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

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


B4X:
'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]"
 

Johan Schoeman

Expert
Licensed User
Longtime User
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

  • pyCreateBarcode.zip
    3.7 KB · Views: 12
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
....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

  • pyCreateBarcode.zip
    3.8 KB · Views: 10

Johan Schoeman

Expert
Licensed User
Longtime User
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
 

Johan Schoeman

Expert
Licensed User
Longtime User
Choose any of the following in the code to change the module shape:

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

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
 

Attachments

  • pyCreateBarcode.zip
    4 KB · Views: 8

William Lancee

Well-Known Member
Licensed User
Longtime User
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.
 

Johan Schoeman

Expert
Licensed User
Longtime User
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
 

Johan Schoeman

Expert
Licensed User
Longtime User
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

  • pyCreateBarcode.zip
    4.1 KB · Views: 3
Top