B4A Library QRCodeReaderView - new release

It is a wrap for this Github project. It is 100% embedded in B4A. I have wrapped and posted this project before somewhere else in this forum (here) but this is the latest update of this project that seems to have some major changes. Please note the checkbox setting - it controls a Boolean variable that will determine if the scanned result will be returned to the B4A project or not.

The new Github project has two views - the one is supposed to overlay the other in order to display/highlight the result points. As I was not sure how to wrap the two views so that the one could overlay the other, I have changed the code in the original Github project in order to incorporate the one view within the other view.

It only shows the 3 or 4 most outlaying result points (based on the version of the QR code)

Posting the following:
1. B4A library files
2. B4A sample project

Note that it only scans QR Codes.

Sample code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: b4NewQRCodeReaderView
    #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

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 nqrcrv As NewQRCodeReaderView
    Private Button1 As Button
    Private torchOn As Boolean = False
  
    Private Button2 As Button
    Private Button3 As Button
  
    Private prevscan As String  = ""
    Private Label1 As Label
    Private CheckBox1 As CheckBox
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
  
    nqrcrv.QRDecodingEnabled = True
    nqrcrv.AutofocusInterval = 500
    nqrcrv.ScanNow = False
    nqrcrv.Visible = False
    nqrcrv.ResultPointColor = Colors.Red
    CheckBox1.Checked = False
  

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
  
    torchOn = False
    CheckBox1.Checked = False
    nqrcrv.stopCamera
    prevscan = ""

End Sub


Sub nqrcrv_result_found(retval As String)
  
    If prevscan = retval Then
       'Log("same scan")
       prevscan = retval
    Else
       Log("b4aResult = " & retval)
        prevscan = retval
        nativeMe.RunMethod("playTone", Null)
    End If 
  
End Sub

Sub Button1_Click
    If nqrcrv.Visible = True Then
        If torchOn = False Then
            nqrcrv.TorchEnabled = True
            torchOn = True
        Else
            nqrcrv.TorchEnabled = False
            torchOn = False
          
        End If  
    End If
  
End Sub

Sub Button2_Click
  
    nqrcrv.Visible = True
    nqrcrv.setBackCamera
    nqrcrv.startCamera
  
End Sub

Sub Button3_Click
  
    nqrcrv.Visible = False
    CheckBox1.Checked = False
    nqrcrv.stopCamera
    prevscan = ""
  
End Sub

Sub CheckBox1_CheckedChange(Checked As Boolean)
  
    If Checked = True Then
        nqrcrv.ScanNow = True
    Else
        nqrcrv.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

Library:
NewQRCodeReaderView
Author:
Github: David Lazaro, Wrapped by: Johan Schoeman
Version: 1.01
  • NewQRCodeReaderView
    Events:
    • result_found (retval As String)
    Fields:
    • ba As BA
    Methods:
    • BringToFront
    • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
    • Initialize (EventName As String)
    • Invalidate
    • Invalidate2 (arg0 As Rect)
    • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • IsInitialized As Boolean
    • RemoveView
    • RequestFocus As Boolean
    • SendToBack
    • SetBackgroundImage (arg0 As Bitmap)
    • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
    • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
    • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
    • forceAutoFocus
      Trigger an auto focus
    • setBackCamera
      Camera preview from device back camera
    • setFrontCamera
      Camera preview from device front camera
    • startCamera
      Starts camera preview and decoding
    • stopCamera
      Stop camera preview and decoding
    Permissions:
    • android.hardware.camera
    • android.hardware.camera.autofocus
    • android.permission.CAMERA
    Properties:
    • AutofocusInterval As Long [write only]
      Set Camera autofocus interval value
      default value is 5000 ms.
    • Background As Drawable
    • Color As Int [write only]
    • Enabled As Boolean
    • Height As Int
    • Left As Int
    • Padding()() As Int
    • Parent As Object [read only]
    • PreviewCameraId As Int [write only]
      Allows user to specify the camera ID, rather than determine
      it automatically based on available cameras and their orientation.
    • QRDecodingEnabled As Boolean [write only]
    • ResultPointColor As Int [write only]
    • ScanNow As Boolean [write only]
    • Tag As Object
    • Top As Int
    • TorchEnabled As Boolean [write only]
      Set Torch enabled/disabled.
      default value is false
    • Visible As Boolean
    • Width As Int

1.png
 

Attachments

  • b4aNewQRCodeReaderView.zip
    9.1 KB · Views: 1,378
  • b4aLibraryFiles.zip
    488.3 KB · Views: 1,607
Last edited:

fishwolf

Well-Known Member
Licensed User
Longtime User
is it possibile change the 4 red point with the classic square angle?
 

fishwolf

Well-Known Member
Licensed User
Longtime User
If i ONLY include the AppCompat library 3.53 into the example, i have this compile error from B4A 7.01
B4X:
B4A version: 7.01
Parsing code.    (0.00s)
Compiling code.    (0.37s)
Compiling layouts code.    (0.00s)
Organizing libraries.    (7.11s)
Generating R file.    (5.11s)
Compiling debugger engine code.    (3.73s)
Compiling generated Java code.    (3.82s)
Convert byte code - optimized dex.    Error
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/AnimRes;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/AnimatorRes;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/AnyRes;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/ArrayRes;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/AttrRes;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/BinderThread;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/BoolRes;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/CallSuper;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/CheckResult;
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Translation has been interrupted
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:689)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:313)
    at com.android.dx.command.dexer.Main.run(Main.java:279)
    at com.android.dx.command.dexer.Main.main(Main.java:247)
    at com.android.dx.command.Main.main(Main.java:106)
Caused by: java.lang.InterruptedException: Too many errors
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:681)
    ... 4 more
 

Johan Schoeman

Expert
Licensed User
Longtime User
If i ONLY include the AppCompat library 3.53 into the example, i have this compile error from B4A 7.01
B4X:
B4A version: 7.01
Parsing code.    (0.00s)
Compiling code.    (0.37s)
Compiling layouts code.    (0.00s)
Organizing libraries.    (7.11s)
Generating R file.    (5.11s)
Compiling debugger engine code.    (3.73s)
Compiling generated Java code.    (3.82s)
Convert byte code - optimized dex.    Error
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/AnimRes;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/AnimatorRes;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/AnyRes;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/ArrayRes;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/AttrRes;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/BinderThread;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/BoolRes;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/CallSuper;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/annotation/CheckResult;
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Translation has been interrupted
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:689)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:313)
    at com.android.dx.command.dexer.Main.run(Main.java:279)
    at com.android.dx.command.dexer.Main.main(Main.java:247)
    at com.android.dx.command.Main.main(Main.java:106)
Caused by: java.lang.InterruptedException: Too many errors
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:681)
    ... 4 more
Use the attached library files when you have the AppCompat library enabled
 

Attachments

  • NewQRCodeReaderView.xml
    11.5 KB · Views: 579
  • NewQRCodeReaderView.jar
    25.7 KB · Views: 550

Sandman

Expert
Licensed User
Longtime User
Hi @Johan Schoeman , sorry to revisit such an old thread, but I just realized this is a newer version than the one I'm using for my project. Have you got a changelog somewhere? Or perhaps simpler: I have not seen any problems at all with version 1.0.0, nor am I missing any wanted features. Would you still recommend me to update to 1.0.1?
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi @Johan Schoeman , sorry to revisit such an old thread, but I just realized this is a newer version than the one I'm using for my project. Have you got a changelog somewhere? Or perhaps simpler: I have not seen any problems at all with version 1.0.0, nor am I missing any wanted features. Would you still recommend me to update to 1.0.1?
If it is working for you then no need to change it.
 

Keith Yong

Active Member
Licensed User
Longtime User
Hi Johan Schoeman , I'm new to B4A.

i downloaded the sample files given above, i manage to run and work perfectly. But when i create another program and follow exactly the same (code, layout, manifest), it doesn't work at all. At first, it doesn't allow to install if "targetSdkVersion=19". After that i change it to targetSdkVersion = 26 and it can install but it given error "java.lang.RuntimeException: Fail to connect to camera service"

May i know anything that i miss out?
 

Attachments

  • Test.zip
    13.7 KB · Views: 417
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan Schoeman , I'm new to B4A.

i downloaded the sample files given above, i manage to run and work perfectly. But when i create another program and follow exactly the same (code, layout, manifest), it doesn't work at all. At first, it doesn't allow to install if "targetSdkVersion=19". After that i change it to targetSdkVersion = 26 and it can install but it given error "java.lang.RuntimeException: Fail to connect to camera service"

May i know anything that i miss out?
1. Change your B4A code to the below code
2. You will need to enable the RuntimePermission Library in the libs tab of the IDE when using target SDK = 26

B4X:
#Region  Project Attributes
    #ApplicationLabel: Golfapp
    #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

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 nqrcrv As NewQRCodeReaderView
    Private Button1 As Button
    Private torchOn As Boolean = False
    
    Private Button2 As Button
    Private Button3 As Button
    
    Private prevscan As String  = ""
    Private Label1 As Label
    Private CheckBox1 As CheckBox
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
    
    nqrcrv.QRDecodingEnabled = True
    nqrcrv.AutofocusInterval = 500
    nqrcrv.ScanNow = False
    nqrcrv.Visible = False
    nqrcrv.ResultPointColor = Colors.Red
    CheckBox1.Checked = False
    

End Sub

Sub Activity_Resume
    

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    
    torchOn = False
    CheckBox1.Checked = False
    nqrcrv.stopCamera
    prevscan = ""

End Sub


Sub nqrcrv_result_found(retval As String)
    
    If prevscan = retval Then
        'Log("same scan")
        prevscan = retval
    Else
        Log("b4aResult = " & retval)
        prevscan = retval
        nativeMe.RunMethod("playTone", Null)
    End If
    
End Sub

Sub Button1_Click
    
    If nqrcrv.Visible = True Then
        If torchOn = False Then
            nqrcrv.TorchEnabled = True
            torchOn = True
        Else
            nqrcrv.TorchEnabled = False
            torchOn = False
            
        End If
    End If
End Sub

Sub Button2_Click
    
    Dim Result As Boolean = True
    If Not(rp.Check(rp.PERMISSION_CAMERA)) Then
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    End If
    
    If Result Then
        nqrcrv.Visible = True
        nqrcrv.setBackCamera
        nqrcrv.startCamera
    End If   
    
End Sub

Sub Button3_Click
    
    nqrcrv.Visible = False
    CheckBox1.Checked = False
    nqrcrv.stopCamera
    prevscan = ""
    
End Sub

Sub CheckBox1_CheckedChange(Checked As Boolean)
    
    If Checked = True Then
        nqrcrv.ScanNow = True
    Else
        nqrcrv.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
 

Keith Yong

Active Member
Licensed User
Longtime User
1. Change your B4A code to the below code
2. You will need to enable the RuntimePermission Library in the libs tab of the IDE when using target SDK = 26

B4X:
#Region  Project Attributes
    #ApplicationLabel: Golfapp
    #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

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 nqrcrv As NewQRCodeReaderView
    Private Button1 As Button
    Private torchOn As Boolean = False
   
    Private Button2 As Button
    Private Button3 As Button
   
    Private prevscan As String  = ""
    Private Label1 As Label
    Private CheckBox1 As CheckBox
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
   
    nqrcrv.QRDecodingEnabled = True
    nqrcrv.AutofocusInterval = 500
    nqrcrv.ScanNow = False
    nqrcrv.Visible = False
    nqrcrv.ResultPointColor = Colors.Red
    CheckBox1.Checked = False
   

End Sub

Sub Activity_Resume
   

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
    torchOn = False
    CheckBox1.Checked = False
    nqrcrv.stopCamera
    prevscan = ""

End Sub


Sub nqrcrv_result_found(retval As String)
   
    If prevscan = retval Then
        'Log("same scan")
        prevscan = retval
    Else
        Log("b4aResult = " & retval)
        prevscan = retval
        nativeMe.RunMethod("playTone", Null)
    End If
   
End Sub

Sub Button1_Click
   
    If nqrcrv.Visible = True Then
        If torchOn = False Then
            nqrcrv.TorchEnabled = True
            torchOn = True
        Else
            nqrcrv.TorchEnabled = False
            torchOn = False
           
        End If
    End If
End Sub

Sub Button2_Click
   
    Dim Result As Boolean = True
    If Not(rp.Check(rp.PERMISSION_CAMERA)) Then
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    End If
   
    If Result Then
        nqrcrv.Visible = True
        nqrcrv.setBackCamera
        nqrcrv.startCamera
    End If  
   
End Sub

Sub Button3_Click
   
    nqrcrv.Visible = False
    CheckBox1.Checked = False
    nqrcrv.stopCamera
    prevscan = ""
   
End Sub

Sub CheckBox1_CheckedChange(Checked As Boolean)
   
    If Checked = True Then
        nqrcrv.ScanNow = True
    Else
        nqrcrv.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

Thanks, it works perfectly. :):)
 

Dey

Active Member
Licensed User
Longtime User
Hello
I'm trying your library in an android tablet 5.1
I get this result

B4X:
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
    at libcore.io.Posix.open(Native Method)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
    at libcore.io.IoBridge.open(IoBridge.java:442)
    ... 16 more
Copying updated assets files (1)
java.lang.RuntimeException: Unable to create service JHS.NewQRCodeReaderView.starter: java.lang.RuntimeException: java.io.FileNotFoundException: /storage/emulated/0/Android/data/JHS.NewQRCodeReaderView/files/virtual_assets/main.bal: open failed: ENOENT (No such file or directory)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2771)
    at android.app.ActivityThread.access$1800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1386)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: /storage/emulated/0/Android/data/JHS.NewQRCodeReaderView/files/virtual_assets/main.bal: open failed: ENOENT (No such file or directory)
    at anywheresoftware.b4a.shell.Shell.virtualAssets(Shell.java:169)
    at anywheresoftware.b4a.shell.Shell.start(Shell.java:102)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:92)
    at JHS.NewQRCodeReaderView.starter.onCreate(starter.java:34)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2761)
    ... 9 more
Caused by: java.io.FileNotFoundException: /storage/emulated/0/Android/data/JHS.NewQRCodeReaderView/files/virtual_assets/main.bal: open failed: ENOENT (No such file or directory)
    at libcore.io.IoBridge.open(IoBridge.java:456)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
    at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:448)
    at anywheresoftware.b4a.shell.Shell.virtualAssets(Shell.java:156)
    ... 13 more
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
    at libcore.io.Posix.open(Native Method)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
    at libcore.io.IoBridge.open(IoBridge.java:442)
    ... 16 more

Thanks for your help
 

Tasyo28

Member
Licensed User
Hi Johan,

I tried your library for scanning QR Codes, i noticed that it works and returning scan result when i used targetSdkVersion=26, but now that google play required minimum targetSdkVersion=28 to upload app so i updated my manifest. But i noticed using targetSdkVersion=28 and using android version 8 above android phones (camera works, red dots are showing but not returning scan result).

Please advise what i need to do, because i cannot use anymore targetSdkVersion=26 in uploading apps in google play.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan,

I tried your library for scanning QR Codes, i noticed that it works and returning scan result when i used targetSdkVersion=26, but now that google play required minimum targetSdkVersion=28 to upload app so i updated my manifest. But i noticed using targetSdkVersion=28 and using android version 8 above android phones (camera works, red dots are showing but not returning scan result).

Please advise what i need to do, because i cannot use anymore targetSdkVersion=26 in uploading apps in google play.

I am testing it on an Android 9 device (Samsung SM-G973F) with SDK in the manifest set to 28 and scans without a problem (B4A 9.80):

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="28"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
AddPermission(android.permission.CAMERA)

AddManifestText(<uses-feature android:name="android.hardware.camera" android:required="true" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.autofocus" android:required="true" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.flash" android:required="true" />)

'End of default text.

This is the code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: b4NewQRCodeReaderView
    #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
    Private rp As RuntimePermissions

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 nqrcrv As NewQRCodeReaderView
    
    Private Button1 As Button
    Private torchOn As Boolean = False
    
    Private Button2 As Button
    Private Button3 As Button
    
    Private prevscan As String  = ""
'    Private Label1 As Label
    Private CheckBox1 As CheckBox
    Private ImageView1 As ImageView
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
'    nqrcrv.Initialize("nqrcrv")
'    Button1.Initialize("Button1")
'    Button2.Initialize("Button2")
'    Button3.Initialize("Button3")
'    CheckBox1.Initialize("CheckBox1")
'   
'   
'    Activity.AddView(nqrcrv, 0%x, 0%y, 100%x, 100%x)
'    Activity.AddView(Button2, 2%x, 100%x + 2%y, 31%x, 15%y)
'    Activity.AddView(Button1, 35%x, 100%x + 2%y, 35%x, 15%y)
'    Activity.AddView(Button3, 72%x, 100%x + 2%y, 31%x, 15%y)
'    Activity.AddView(CheckBox1, 2%x, 100%y - 10%y, 15%x, 10%y)
'   
'    Button1.Color = Colors.Green
'    Button1.Text = "Torch"
'    Button1.TextColor = Colors.Black
'    Button1.TextSize = 15
'    Button1.Typeface = Typeface.DEFAULT_BOLD
'   
'    Button2.Color = Colors.Yellow
'    Button2.Text = "Start Scan"
'    Button2.TextColor = Colors.Black
'    Button2.TextSize = 15
'    Button2.Typeface = Typeface.DEFAULT_BOLD
'   
'    Button3.Color = Colors.Cyan
'    Button3.Text = "Stop Scan"
'    Button3.TextColor = Colors.Black
'    Button3.TextSize = 15
'    Button3.Typeface = Typeface.DEFAULT_BOLD
    

    nqrcrv.QRDecodingEnabled = True
    nqrcrv.AutofocusInterval = 500
    nqrcrv.ScanNow = False
    nqrcrv.Visible = False
    nqrcrv.ResultPointColor = Colors.Magenta
    nqrcrv.ResultPointSize = 25
    CheckBox1.Checked = False
    

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    
    torchOn = False
    CheckBox1.Checked = False
    nqrcrv.stopCamera
    prevscan = ""

End Sub


Sub nqrcrv_result_found(retval As String, obj As Object)
    
    Dim bm As Bitmap = obj
    If prevscan = retval Then
       'Log("same scan")
       prevscan = retval
    Else
       Log("b4aResult = " & retval)
        prevscan = retval
        ImageView1.Bitmap = bm
'        nativeMe.RunMethod("playTone", Null)
    End If   
    
End Sub

Sub Button1_Click
    If nqrcrv.Visible = True Then
        If torchOn = False Then
            nqrcrv.TorchEnabled = True
            torchOn = True
        Else
            nqrcrv.TorchEnabled = False
            torchOn = False
            
        End If   
    End If
    
End Sub

Sub Button2_Click

    Dim Result As Boolean = True
    If Not(rp.Check(rp.PERMISSION_CAMERA)) Then
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    End If
    If Result Then
    
        nqrcrv.Visible = True
        nqrcrv.setBackCamera
        nqrcrv.startCamera
    End If
End Sub

Sub Button3_Click
    
    nqrcrv.Visible = False
    CheckBox1.Checked = False
    nqrcrv.stopCamera
    prevscan = ""
    
End Sub

Sub CheckBox1_CheckedChange(Checked As Boolean)
    
    If Checked = True Then
        nqrcrv.ScanNow = True
    Else
        nqrcrv.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

I had a checkbox on the UI of this sample project that had to be toggled "ON" before a scan result are returned. Maybe worth while checking if you have the checkbox in your project and maybe it is not visible? I ran into this problem once when I updated my B4A version and could thereafter not see the checkbox as the app theme changed. It was there but had to change the background color to make it visible so that I could toggle it "ON".
 

marconotsopolo

Member
Licensed User
Hi Johan

I am trying to switch the cameras but not having much luck

B4X:
If btnCamera.Tag="REAR" Then
     qrScan.ScanNow=False
        qrScan.stopCamera       
        qrScan.setFrontCamera
        btnCamera.Tag="FRONT"
        qrScan.QRDecodingEnabled = True
        qrScan.AutofocusInterval = 500
        qrScan.startCamera
     qrScan.ScanNow = True
Else

Is it possible while the view is active??

Thanks
Mark
 

wonder

Expert
Licensed User
Longtime User
Top