B4A Library LiveEdgeDetection

Johan Schoeman

Expert
Licensed User
This is a shortcut wrap for this Github project. It will scan a "document" and bring back a bitmap of the scanned "document" to the B4A project via event xxx_scan_result. You need to download the 2 x jar, 1 x xml, and 1x aar from the following link and copy them to your additional library folder:

https://drive.google.com/drive/folders/1Xr7EW7gANjwzP5urGkmvneokKHvXH1fw

Attached the following:
1. The zipped B4A project - b4aLiveEdgeDetection.zip
2. LibRes.zip - extract it and copy the folder with its contents to be on the same folder level as that of the /Files and /Objects folders of the B4A project
3. openCVres.zip - extract it and copy the folder with its contents to be on the same folder level as that of the /Files and /Objects folders of the B4A project
4. resource.zip - extract it and copy the folder with its contents to be on the same folder level as that of the /Files and /Objects folders of the B4A project

What does it do?
1. It detects the edges of a "document" if the background is sufficiently dark
2. It will guide you (while the scanner is active) to position the camera correctly
3. It will automatically take a picture of the "document" once it is satisfied with the orientation/background/distance
4. Once the pic has been taken, you can crop the image - use the handles
5. When you click on the "tick mark" it will return the bitmap to your B4A project. You need to handle the bitmap from here onwards (eg save it to a png file)

1.png


2.png


5.png



4.png



It was quite challenging to get this lib compiled and to be able to "kick start" it from within B4A. The ConstraintLayout kept me busy for some time...

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

#AdditionalRes: ..\resource
#AdditionalRes: ..\openCVres
#AdditionalRes: ..\LibRes



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

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 Button1 As Button
    Private led As LiveEdgeDetection
 
    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")
    led.Initialize("led")
 
    ImageView1.Visible = False

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button1_Click
 
    led.startDetection
 
End Sub

Sub led_scan_result(result As Object)
 
    Dim mb As Bitmap = result
    ImageView1.Bitmap = mb
    ImageView1.Visible = True
 
End Sub
B4A Manifest:
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="22"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)

SetApplicationAttribute(android:theme, "@style/MyAppTheme")

AddApplicationText(<activity android:name="com.adityaarora.liveedgedetection.activity.ScanActivity">
        </activity>)

'End of default text.
Library:
LiveEdgeDetection
Author:
Github: Aditya Arora, Wrapped by: Johan Schoeman
Version: 1
  • LiveEdgeDetection
    Events:
    • scan_result (image As Object)
    Methods:
    • startDetection As void
    • IsInitialized As boolean
    • Initialize (paramBA As anywheresoftware.b4a.BA, paramString As java.lang.String) As void
    Permissions:
    • android.hardware.camera
    • android.permission.CAMERA
    • android.permission.READ_EXTERNAL_STORAGE
    • android.permission.WRITE_EXTERNAL_STORAGE

Hope I have posted all that will be required....:rolleyes:
 

Attachments

Last edited:

moster67

Expert
Licensed User
Tested and it works fine on my Samsung S8. Good job. :)

PS: I did at first not read that it was for "documents" and it had difficulties to detect objects but once I understood that, it detected the very first document I threw at it.
 

Johan Schoeman

Expert
Licensed User
Tested and it works fine on my Samsung S8. Good job. :)

PS: I did at first not read that it was for "documents" and it had difficulties to detect objects but once I understood that, it detected the very first document I threw at it.
It was the closest thing that I had that looks like a "page of a document". Just about A4 size....lol
 

Max Koopman

Member
Licensed User
First of all, thank you very much for your work on this (and other ) library. I have been testing with this library for some time already and found it to be working absolutely great on my tablet. Today I was trying out the same but used my phone and recieved the error below. Long story short, I tried to compile the example app and this also gave the same issue as below.
The tablet is android 7, phone is android 6.0.1. I have trying out a lot but can't seem to find what is so different between the two devices that would give this error, I'm stuck. Could this be a thing with the rom used on my phone?


** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1492)
at android.hardware.Camera.takePicture(Camera.java:1436)
at com.adityaarora.liveedgedetection.view.ScanSurfaceView.autoCapture(ScanSurfaceView.java:313)
at com.adityaarora.liveedgedetection.view.ScanSurfaceView.access$500(ScanSurfaceView.java:43)
at com.adityaarora.liveedgedetection.view.ScanSurfaceView$3.onTick(ScanSurfaceView.java:296)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:133)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 

Johan Schoeman

Expert
Licensed User
First of all, thank you very much for your work on this (and other ) library. I have been testing with this library for some time already and found it to be working absolutely great on my tablet. Today I was trying out the same but used my phone and recieved the error below. Long story short, I tried to compile the example app and this also gave the same issue as below.
The tablet is android 7, phone is android 6.0.1. I have trying out a lot but can't seem to find what is so different between the two devices that would give this error, I'm stuck. Could this be a thing with the rom used on my phone?


** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1492)
at android.hardware.Camera.takePicture(Camera.java:1436)
at com.adityaarora.liveedgedetection.view.ScanSurfaceView.autoCapture(ScanSurfaceView.java:313)
at com.adityaarora.liveedgedetection.view.ScanSurfaceView.access$500(ScanSurfaceView.java:43)
at com.adityaarora.liveedgedetection.view.ScanSurfaceView$3.onTick(ScanSurfaceView.java:296)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:133)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I have 4 Samsung devices:
7.0
5.1
6.01
4.4.2

The camera starts on all 4 of them without a problem but only the 7.0 and 4.4.2 devices have good cameras that are able to detect the edge of a document. I see the same issue as what you are having has been reported here:

https://github.com/adityaarora1/LiveEdgeDetection/issues/3

I have not looked into it to see what a possible solution might be.
 
Last edited:

Max Koopman

Member
Licensed User
Thanks for looking into the issue. Good to see that i'm not the only one with the issue.
Unfortunately it seems that the library creator will not update the library anytime soon, according to your link.

Lucky for me, I do know a lot about opencv and with the b4a opencv library and the source code, I should be able to recreate some functionality into native b4a.
 

Mike1970

Well-Known Member
Licensed User
it remains locked on "use darker background"
So i put the project in Debug mode and now it not install anymore
 

Cableguy

Expert
Licensed User
Hi Johan,

I know this is quite an old project but just came across it when searching for this exact functionality...
I followed all your (reduced) indications and istalled into my T5 Huawey tablet...
The App does not start and I get the following logs:
B4X:
Logger connected to:  HUAWEI AGS2-W09
--------- beginning of main
--------- beginning of system
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/adityaarora/liveedgedetection/activity/ScanActivity;
    at liveedgedetectionwrapper.liveedgedetectionWrapper.Initialize(liveedgedetectionWrapper.java:76)
    at JHS.LiveEdgeDetection.main._activity_create(main.java:395)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at JHS.LiveEdgeDetection.main.afterFirstLayout(main.java:105)
    at JHS.LiveEdgeDetection.main.access$000(main.java:17)
    at JHS.LiveEdgeDetection.main$WaitForLayout.run(main.java:83)
    at android.os.Handler.handleCallback(Handler.java:808)
    at android.os.Handler.dispatchMessage(Handler.java:101)
    at android.os.Looper.loop(Looper.java:166)
    at android.app.ActivityThread.main(ActivityThread.java:7529)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: java.lang.ClassNotFoundException: com.adityaarora.liveedgedetection.activity.ScanActivity
    at java.lang.VMClassLoader.findLoadedClass(Native Method)
    at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    ... 18 more
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/app/AppCompatActivity;
    ... 18 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.app.AppCompatActivity" on path: DexPathList[[zip file "/data/app/JHS.LiveEdgeDetection-eQrTAT8__Je9quR0PNlS_Q==/base.apk"],nativeLibraryDirectories=[/data/app/JHS.LiveEdgeDetection-eQrTAT8__Je9quR0PNlS_Q==/lib/arm, /system/fake-libs, /data/app/JHS.LiveEdgeDetection-eQrTAT8__Je9quR0PNlS_Q==/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib, /product/lib, /preavs/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    ... 18 more
** Activity (main) Resume **

[EDIT]
I went to the GitHub of this project and downloaded the available apk...
The app starts and "behaves" as expected, but no image is shown, being it in preview nor after...
Your help would be much apreciated!
 
Last edited:
Top