Android Question Hello, how to recognize two-dimensional(QRcode) file of image file?

sfgwl

Member
Licensed User
Longtime User
hello everyone! I am come from china,My English is poor,I am a complete beginner to B4A,How to add two-dimensional code recognition in zxscannerjohan.lib(QRCODE include Chinese characters )
Open the image file for identification
thanks!
 

Attachments

  • DDCZJG02.png
    DDCZJG02.png
    37.7 KB · Views: 229

Johan Schoeman

Expert
Licensed User
Longtime User

Attachments

  • DDCZJG02.png
    DDCZJG02.png
    6.2 KB · Views: 226
Last edited:
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
thanks

I mean :
Not through camera scanning, is app loading pictures after identification
Here you go:
1. Copy DDCZJG02.png to the "root" folder of your device (or change the path in the B4A code and then place the png file in the appropriate location)
2. Extract and copy core-3.2.1.jar to your additional library folder

You should get this:

1.png



Sample code with inline Java code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: b4aDecodeQRcode
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalJar: core-3.2.1

#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.

    Dim nativeMe As JavaObject


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 decoded As String

    Private Label1 As Label
    Private ImageView1 As ImageView

    Dim bm As Bitmap
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")

    nativeMe.InitializeContext
    bm.Initialize(File.DirRootExternal, "DDCZJG02.png")
    ImageView1.Bitmap = bm

    Dim path As String = File.DirRootExternal & "/DDCZJG02.png"
    decoded = nativeMe.RunMethod("decodeQRImage", Array(path))
    Log("Decoded = " & decoded)
    Label1.Text = decoded

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



#if Java


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;

//the inline java code is based on this link:
//https://stackoverflow.com/questions/36210537/find-qr-code-in-image-and-decode-it-using-zxing

public String decodeQRImage(String path) {
    BA.Log("path = " + path);
    Bitmap bMap = BitmapFactory.decodeFile(path);
    BA.Log("bMap = " + bMap);
    String decoded = null;

    int[] intArray = new int[bMap.getWidth() * bMap.getHeight()];
    bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(),
            bMap.getHeight());
    LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(),
            bMap.getHeight(), intArray);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    Reader reader = new QRCodeReader();
    try {
        Result result = reader.decode(bitmap);
        decoded = result.getText();
    } catch (NotFoundException e) {
        e.printStackTrace();
    } catch (ChecksumException e) {
        e.printStackTrace();
    } catch (FormatException e) {
        e.printStackTrace();
    }
    return decoded;
}


#End If

You will need at least JavaObject V2.05 to be enabled in the libs tab of the B4A IDE
 

Attachments

  • b4aDecodeQRcode.zip
    8.5 KB · Views: 277
  • core-3.2.1.zip
    483.9 KB · Views: 277
  • DDCZJG02[2].png
    DDCZJG02[2].png
    6.2 KB · Views: 240
Last edited:
Upvote 0
Top