#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