Android Question Problems with decoding QRCodeReader v1.02

FernandoGuedes

Member
Licensed User
Longtime User
Hello everybody.

I've successfully used @Johan Schoeman excellent library for a while. But I'm having an unusual problem when trying to decode a QRCode in particular (This one is attached). The reading through the scanner is perfect and brings the information perfectly. But when I try to decode it the result is "null" in the result of the attempt.

Logger conectado a: Xiaomi Redmi Note 8
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
path = /storage/emulated/0/DDCZJG02.jpg
bMap = android.graphics.Bitmap@f6185c7
Decoded = null
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **


This is the first time this error occurs and as I'm still a beginner in B4A, I would like to understand a be able to predict and avoid this error.

Has this ever happened to anyone?

Thank you for your attention!
 

Attachments

  • DDCZJG02.jpg
    DDCZJG02.jpg
    5.6 KB · Views: 187
Last edited:

drgottjr

Expert
Licensed User
Longtime User
not all qr codes can be read. there are a number of reasons for this.
in your case, i would say the example you posted does not have the required "quiet" area surrounding the code. think of it as a white border around the code.
the "null" reported by the reader means it did not recognize the image as a qr code. it could, in fact, be an incorrectly produced qr code, but i think the issue is the missing frame.

if i give it a nice little frame, i have no trouble reading it.
 

Attachments

  • quiet.png
    quiet.png
    10 KB · Views: 185
Last edited:
Upvote 0

delgadol

Member
Hello everybody.

I've successfully used @Johan Schoeman excellent library for a while. But I'm having an unusual problem when trying to decode a QRCode in particular (This one is attached). The reading through the scanner is perfect and brings the information perfectly. But when I try to decode it the result is "null" in the result of the attempt.

Logger conectado a: Xiaomi Redmi Note 8
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
path = /storage/emulated/0/DDCZJG02.jpg
bMap = android.graphics.Bitmap@f6185c7
Decoded = null
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **


This is the first time this error occurs and as I'm still a beginner in B4A, I would like to understand a be able to predict and avoid this error.

Has this ever happened to anyone?

Thank you for your attention!

Thanks

I have use this lib, and works ok in almost the devices; the QR reader fail in some devices; (or it is very .. very slow); devices with focal distance camera over 0.5m; (basic alcatel devices in my case, android moly based devices(cheap asia phone) ); one time i have a very big problem, i try to implements a QR with 244 char info, and it was impossible. the best QR info content is over to 30 char (mixed and ANSI)
 
Upvote 0

FernandoGuedes

Member
Licensed User
Longtime User
How should we know with this less Information/no code shown?

Honestly i did not understand what the problem is actually.

Upload a small project showing the Problem.
Thank you very much for your attention @DonManfred . I didn't upload the complete code because it was a very specific error when decoding the QRCode.
 
Upvote 0

FernandoGuedes

Member
Licensed User
Longtime User
nem todos os códigos qr podem ser lidos. há uma série de razões para isso.
no seu caso, eu diria que o exemplo que você postou não tem a área "silenciosa" necessária ao redor do código. pense nisso como uma borda branca ao redor do código.
o "null" relatado pelo leitor significa que não reconheceu a imagem como um código qr. poderia, de fato, ser um código qr produzido incorretamente, mas acho que o problema é o quadro ausente.

se eu lhe der uma bela moldura, não tenho problemas em lê-la.
Yes @drgottjr , there are many reasons not to be read. The structure of encoding and decoding algorithms for creating and assembling 2D code (QRCode) is quite complex. I use an option in my app (images below) that allows reading the QRCode content of the decoded image. But I understand that the problem is really in the decoding method. I will look for some other more up-to-date image decoding method. I tested the image you sent me with the frame and the result is still "Null". Thank you very much for your effort and attention in helping me to solve this problem.
 

Attachments

  • Decoding Fail.jpg
    Decoding Fail.jpg
    169.4 KB · Views: 171
  • Decoding Rigth.jpg
    Decoding Rigth.jpg
    206.8 KB · Views: 179
Upvote 0

FernandoGuedes

Member
Licensed User
Longtime User
I have use this lib, and works ok in almost the devices; the QR reader fail in some devices; (or it is very .. very slow); devices with focal distance camera over 0.5m; (basic alcatel devices in my case, android moly based devices(cheap asia phone) ); one time i have a very big problem, i try to implements a QR with 244 char info, and it was impossible. the best QR info content is over to 30 char (mixed and ANSI)
Got it @delgadol thanks so much for sharing your experience. I will study this QRCode structure a little more deeply to find an objective solution to this problem ;)?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
Yes @drgottjr , there are many reasons not to be read. The structure of encoding and decoding algorithms for creating and assembling 2D code (QRCode) is quite complex. I use an option in my app (images below) that allows reading the QRCode content of the decoded image. But I understand that the problem is really in the decoding method. I will look for some other more up-to-date image decoding method. I tested the image you sent me with the frame and the result is still "Null". Thank you very much for your effort and attention in helping me to solve this problem

i have no trouble reading your left and right images. export your app and upload it. all the qr code libraries here (even google's mobile vision) are zxing-based. i can read your code with several libraries. as was suggested by @DonManfred, upload your code.
 

Attachments

  • left.png
    left.png
    118.5 KB · Views: 181
  • right.png
    right.png
    121.9 KB · Views: 177
Upvote 0

FernandoGuedes

Member
Licensed User
Longtime User
i have no trouble reading your left and right images. export your app and upload it. all the qr code libraries here (even google's mobile vision) are zxing-based. i can read your code with several libraries. as was suggested by @DonManfred, upload your code.
Dear @drgottjr !!! Thank you very much for your attention!!! This problem still didn't help me to know the structure of QRCodes. The problem was really in the QRCode structure. I learned that QRCodes can have 4 error correction levels and for each level up to 40 versions!!! The file "QR_Null.png" is at correction level 1 (low) and version 1, and the decoder was not recognizing it. I generated the file "QR_Nv2_OK.jpg" at correction level 2 (medium) and version 1. If you read the QRCodes through the camera, you will get absolutely the same content, but the decoder doesn't understand it that way. Below is the code I use, as well as the error and success images. Once again thank you very much for your help.

PS: The generator I used for the tests was the SimpleCodeGenerator .

QR Decoder:
#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, "quiet.png")      'QRCode gerado erradamente pelo Lib Clarion
    bm.Initialize(File.DirRootExternal, "e_4_96695.jpg")  'QRCode gerado pelo SimpleCodeGenerator. Resultado: Geração correta !!!
    ImageView1.Bitmap = bm
   
'    texto = 002||||FUPRESA|SALA DE LIMPEZA|5|59358|Extintor|PQS|4 Kg||2023|11/2022|06/2000
'    Dim path As String = File.DirRootExternal & "/quiet.png"      'QRCode gerado erradamente pela Lib Clarion
    Dim path As String = File.DirRootExternal & "/e_4_96695.jpg"      'QRCode gerado pelo SimpleCodeGenerator. Resultado: Geração correta !!!
    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
 

Attachments

  • Code_QRCodeResult_Null.jpg
    Code_QRCodeResult_Null.jpg
    157 KB · Views: 169
  • Code_QRCodeResult_OK.jpg
    Code_QRCodeResult_OK.jpg
    353.5 KB · Views: 165
  • QR_Null.png
    QR_Null.png
    10 KB · Views: 166
  • QR_Nv2_OK.jpg
    QR_Nv2_OK.jpg
    86.8 KB · Views: 166
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
your problem has nothing to do with QR code structure or error correction levels.

your problem is that you are using File.DirDefaultExternal. this is not allowed with recent sdk's

for the purposes of testing the reader, store your generated images in DirAssets. you say you are a newcomer, so i don't want to complicate things for you, but there are a number of other ways to test your generator with your reader. you should research them instead of studying qr code structure.
so, either:
1) put the images in DirAssets and build the reader
or
2) set the targetSDK to 19 for your reader and continue to use DirDefaultExternal. i think will need external storage permission if you go higher (you will also need to study about storage permissions). when you start a new b4a project, the targetSDK is automatically set at 29 or 30. you can't use DirDefaultExternal. if you lower the targetSDK, it was allowed. you can experiment, but i think 19 should work. this is for testing purposes, ok? at some point, you will have to learn about android storage access.
 
Upvote 0
Top