B4A Library QRCodeReaderView - QR Code Scanner that is also 100% embedded in B4A (new B4A lib files in post #8)

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
how do I start the qr reader, on the activity create?
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Check the example code in Post #1
That's what I did, but without success. The difference is that I dont' use a layout but I create all components by code
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
It does not change anything.
1. You should always create a NEW THREAD for any issue you have.
2. You need to use Runtimepermissions as you need to switch to TargetSDK 26
3. The code like in #1 can be used even if you add all the objects by Code.
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
It does not change anything.
1. You should always create a NEW THREAD for any issue you have.
2. You need to use Runtimepermissions as you need to switch to TargetSDK 26
3. The code like in #1 can be used even if you add all the objects by Code.

1. I didnt' create a new thread, to refer directly to this library
2. I don't use targetsdk 26
3. It doesn't work
 

aeric

Expert
Licensed User
Longtime User
I tried to change targetSdkVersion = 26 but the app crashed. I try change to 17 and run it compiled success. Then I change to 26 it compiles success. Not sure what is the problem. If I stop the B4A bridge and start again then open the project again to compile in targetSdkVersion = 26 it crash again.
 

Johan Schoeman

Expert
Licensed User
Longtime User
That's what I did, but without success. The difference is that I dont' use a layout but I create all components by code
Have done a minor change to the library to add the permissions (it was not defined in the lib before)
B4X:
@Permissions(values={"android.permission.CAMERA","android.hardware.camera","android.hardware.camera.autofocus","android.permission.VIBRATE","android.permission.FLASHLIGHT"})

The attached project is working with target SDK 26 and the views are added via code (tested with B4A V8.00 and on an Android 7.0 device).
B4A Manifest:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
AddPermission("android.permission.VIBRATE")
AddPermission("android.permission.CAMERA")
AddPermission("android.permission.FLASHLIGHT")
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

Copy the attached lib files to your additional libs folder:
QRCodeReaderV102.zip (extract the jar and xml) - it should show as QRCodeReaderV102 (V1.02) in the libs tab of the B4A project
jhscore.jar

Sample B4A code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: b4aQRCodeReaderViewByCode
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False

#End Region


#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
    Dim rp As RuntimePermissions               'ADDED 8 June 2018

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.
    
    
    Private b1 As Button
    Private b2 As Button
    Private p1 As Panel
    Private l1 As Label
    Private oldString As String = ""
    
    Private b3 As Button
    

    Private cb1 As CheckBox
    
    
    
    Private qrcrv As QRCodeReaderV102
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")
    
    p1.Initialize("")
    qrcrv.Initialize("qrcrv")
    b1.Initialize("b1")
    b2.Initialize("b2")
    b3.Initialize("b3")
    l1.Initialize("")
    cb1.Initialize("cb1")
    
    Activity.AddView(p1, 5%x, 5%y, 90%x, 50%y)
    p1.AddView(qrcrv, 7%x, 7%y, p1.Width - 14%x, p1.Height - 14%y)
    Activity.AddView(b1, p1.Left, p1.Top + p1.Height + 5%y, 35%x, 10%y)
    Activity.AddView(b2, 60%x, b1.Top, 35%x, b1.Height)
    Activity.AddView(b3, b1.Left + b1.width + 1%x, b1.top, 18%x, b1.Height)
    Activity.AddView(cb1, b1.Left, b1.Top + b1.Height + 2%y, 50%x, b1.Height)
    Activity.AddView(l1, b1.left, cb1.Top + cb1.Height + 2%y, 90%x, b1.Height)
    
    b1.Text = "Start"
    b1.TextColor = Colors.Blue
    b1.TextSize = 15
    b2.Text = "Stop"
    b2.TextColor = Colors.Blue
    b2.TextSize = 15
    b3.Text = "Torch"
    b3.TextColor = Colors.Blue
    b3.TextSize = 15
    p1.Color = Colors.Yellow
    
    cb1.Checked = False
    cb1.Text = "scan/don't scan"
    cb1.TextColor = Colors.Red
    cb1.TextSize = 15

    
    nativeMe.InitializeContext
    
    
    qrcrv.Visible = False

    'Keep the order of the next 4 lines. CAMERA_BACK and CAMERA_FRONT will only contain correct values once qrcrv.BackFacingCamera and qrcrv.FrontFacingCamera has
    'been executed else they will have default values of -1
    Log(qrcrv.BackFacingCamera)    'see the popup help when entering this method - check if there is a back facing camera and return the ID of the camera
    Log(qrcrv.FrontFacingCamera)   'see the popup help when entering this method - check if there is a front facing camera and retutn the ID of the camera
    
    Log(qrcrv.CAMERA_BACK)         'see the popup help when entering this method
    Log(qrcrv.CAMERA_FRONT)           'see the popup help when entering this method
    
    'now you can use
    qrcrv.CameraId = qrcrv.CAMERA_BACK   'or qrcrv.CameraId = qrcrv.CAMERA_FRONT
    
    'qrcrv.CameraDegrees = 270  'set this to 0, 90, 180, 270
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub b1_Click
    
    Dim Result As Boolean = True                                                                       'ADDED 8 June 2018
    If Not(rp.Check(rp.PERMISSION_CAMERA)) Then                                                        'ADDED 8 June 2018
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)                                                       'ADDED 8 June 2018
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)                   'ADDED 8 June 2018
    End If                                                                                             'ADDED 8 June 2018
    If Result Then
      qrcrv.Visible = True
      Sleep(0)
      qrcrv.startScan
    End If 
      
End Sub

Sub b2_Click
    
    qrcrv.stopScan
    Sleep(0)
    qrcrv.Visible = False


End Sub

Sub qrcrv_result_found(retval As String)
    If oldString <> retval Then
      oldString = retval
      Log("B4A: " & oldString)
      l1.Text = oldString   
      nativeMe.RunMethod("playTone", Null)
      
    End If
    
End Sub

Sub b3_Click
    
    If (qrcrv.isFlashOn = True) Then
      qrcrv.setFlashOff
    Else
      qrcrv.setFlashOn
    End If 
    
End Sub

Sub cb1_CheckedChange(Checked As Boolean)
    
    If cb1.Checked = True Then
        qrcrv.ScanNow = True
    Else
        qrcrv.ScanNow = False
    End If
    
End Sub

#if Java

import android.media.ToneGenerator;
import android.media.AudioManager;

  public void playTone() {
      final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
      tg.startTone(ToneGenerator.TONE_PROP_BEEP);
  }     

#End If



Leaving it up to you to sort out the rest of the layout logic - but it is working by adding the views via code and targeting SDK 26.
 

Attachments

  • QRCodeReaderV102.zip
    32.3 KB · Views: 634
  • b4aQRCodeReaderViewByCode.zip
    8.3 KB · Views: 610
  • jhscore.jar
    492.4 KB · Views: 603

Johan Schoeman

Expert
Licensed User
Longtime User
See post #70
 

b4x-de

Active Member
Licensed User
Longtime User
Dear Johan, thank you very much for providing this library. My requirement is to take a photo of physical objects labeld with a qr code while passing by the camera. As soon as the camera recognizes a qr code and reads it data, a photo should be taken automatically. Scanning the qr code and reading the data with your library works perfectly.

My question is about the best way to take the photo. There seems to be no option in your library. Do you might consider it to be a useful feature for future releases of your library?

If not do you or anybody else have some advice for me how to switch from your library to another camera library back and forth? (The objects passing by quickly so the time for switching and refocus needs to be minimal.) Thank you for any help and advice!

Thomas
 

Johan Schoeman

Expert
Licensed User
Longtime User
Try with the attached B4A project and lib files. It will return a bitmap to the B4A project when a scan occurred. Up to you to handle the bitmap from here onwards (save it as a png, etc - search the forum for how to do it).

B4X:
Sub qrcrv_result_found(retval As String, img As Object)
    If oldString <> retval Then
      oldString = retval
      Log("B4A: " & oldString)
      l1.Text = oldString   
      Dim bm As Bitmap = img
      ImageView1.Bitmap = bm
      nativeMe.RunMethod("playTone", Null)
      
    End If
    
End Sub

 

Attachments

  • QRCodeReaderV102LibFiles.zip
    32.7 KB · Views: 577
  • b4aQRCodeReaderView.zip
    10.5 KB · Views: 531

asales

Expert
Licensed User
Longtime User
I'm testing this library (thanks @Johan Schoeman ), but I get some questions:

1 - the version 1.00 works fine, but sometimes I get this line in log: "CAMERA IS NOT NULL".
This is from the library? If yes, why?

2 - I tested the other versions (1.01 and 1.02), but they don't works like the version 1.00.
I try to read a qrcode show in the computer screen (like the Whatsapp Web).
The code is read in version 1.00, but not in the others versions (using the example).
 

Johan Schoeman

Expert
Licensed User
Longtime User
The CAMERA IS NOT NULL is probably a left over from some of the debugging that I have done.
Have you checked the checkbox? It was added subsequent to version 1 (on request). It will only return a scan if the checkbox is set or if you set in your code
qrcsv.ScanNow = True
 

asales

Expert
Licensed User
Longtime User
The CAMERA IS NOT NULL is probably a left over from some of the debugging that I have done.
OK
Have you checked the checkbox? It was added subsequent to version 1 (on request). It will only return a scan if the checkbox is set or if you set in your code qrcsv.ScanNow = True
It works to scan (version 1.02, post #74), but I get this error:
B4X:
java.lang.Exception: Sub qrcrv_result_found signature does not match expected signature.
EDITED: the code in post #70 works.
Thanks!
 

Johan Schoeman

Expert
Licensed User
Longtime User
The lib files and B4A project in post #74 go together. The signature has changed as the event raised now also returns the Bitmap of the scanned code.
 

junaidahmed

Well-Known Member
Licensed User
Longtime User
I have run above example in Android 4.It's working fine but when I run the same code in Android 6.0 and 7.0 then it's not working..

Any one has face this issue.....
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…