B4J Question Read barcode (zbar + b4j).

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
zbar.jR uses Andoid classes and will not work in B4J. The core of Zxing might do the trick as it does not depend on Android specific classes.
Thank you.
I don't know enough java but I will try to write something using zxing core-3.3.0.jar.
Maybe inline java is easier.
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
This was done with ZXING (using some inline java code):

1. Download core-3.3.0.jar and copy the jar to your B4J additional libs folder
2. Download javase-3.3.0.jar and copy the jar to your B4J additional libs folder

B4J sample project attached. Have tested it with (see /Files folder of attached B4J project):
1. Code 39
2. Code 93
3. EAN 13 (some EAN13 codes seem to work - others that I tried does not work)
4. PDF 417
5. Code 128
6. Datamatrix
7. Aztec
8. QR Code

See the png files included in the /Files folder of the attached B4J project (you can also pass *.jpg files). Change the image name here (in the Main module:
B4X:
b.Initialize(File.DirAssets,"ean13.png")

B4J Main module:
B4X:
Region  Project Attributes
    #MainFormWidth: 1000
    #MainFormHeight: 800
'    #AdditionalJar: sqlite-jdbc-3.7.
    #AdditionalJar: core-3.3.0.jar
    #AdditionalJar: javase-3.3.0.jar
   
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private ImageView1 As ImageView                      'original image

   
    Private b As Image
    Private dec As decoder
   
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("main") 'Load the layout file.
    MainForm.Show
    MainForm.BackColor = fx.Colors.ARGB(100,0,0,255)
'    MainForm.Initialize("", -1, -1)  
    MainForm.WindowWidth = 500
    MainForm.WindowLeft = 0
    MainForm.WindowTop = 0
    MainForm.WindowHeight = 500
   
   
    ImageView1.Visible = False

    b.Initialize(File.DirAssets,"ean13.png")
    ImageView1.SetImage(b)  
    ImageView1.Visible = True
   
    'Initialize the barcode decoder
    dec.Initialize
   
    Dim result As String = dec.decodeBarcode(b)
    Log("Decoded barcode = "  & result)
   
End Sub

B4J class decoder:
B4X:
'Class module
Sub Class_Globals
    Private fx As JFX
    Private nativeMe As JavaObject
   
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
   
    nativeMe = Me

End Sub


'Pass the image that contains the barcode to be decoded
Public Sub decodeBarcode(img As Image) As String
   
    Return nativeMe.RunMethod("decodeBarCode", Array(img))

End Sub
   

#If Java

import java.awt.image.BufferedImage;
import javafx.scene.image.Image;
import javafx.embed.swing.SwingFXUtils;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
   
   
    public String decodeBarCode(Image img) {
        String decodedText = "";
        BufferedImage bmp = SwingFXUtils.fromFXImage(img, null);  
        try {
            decodedText = decodeQRCode(bmp);
            if(decodedText == null) {
                decodedText = "No barcode found in the image";
            } else {
             }
        } catch (IOException e) {
            decodedText = "Could not decode QR Code, IOException :: " + e.getMessage();
        }
        return decodedText;
    }
   
    private String decodeQRCode(BufferedImage bufferedImage) throws IOException {
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        try {
            Result result = new MultiFormatReader().decode(bitmap);
            return result.getText();
        } catch (NotFoundException e) {
            System.out.println("There is no barcode in the image");
            return null;
        }
    }
   
#End If

Watch the result in the B4J log.

See this in the main module:
B4X:
    #AdditionalJar: core-3.3.0.jar
    #AdditionalJar: javase-3.3.0.jar

Libraries used:




Enjoy!
 

Attachments

  • decodeBarcode.zip
    107 KB · Views: 385
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…