B4A Library LiveEdgeDetection

Discussion in 'Additional libraries, classes and official updates' started by Johan Schoeman, Apr 27, 2018.

  1. Johan Schoeman

    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:


    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)





    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:
    #Region  Project Attributes
    #ApplicationLabel: b4aLiveEdgeDetection
    #VersionCode: 1
    '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:
        ImageView1.Visible = 

    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    Sub Button1_Click
    End Sub

    Sub led_scan_result(result As Object)
    Dim mb As Bitmap = result
        ImageView1.Bitmap = mb
        ImageView1.Visible = 
    End Sub
    B4A Manifest:
    '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
    <uses-sdk android:minSdkVersion=
    "5" android:targetSdkVersion="22"/>
    <supports-screens android:largeScreens=
    CreateResourceFromFile(Macro, Themes.DarkTheme)


    activity android:name="com.adityaarora.liveedgedetection.activity.ScanActivity">

    'End of default text.
    Github: Aditya Arora, Wrapped by: Johan Schoeman
    Version: 1
    • LiveEdgeDetection
      • scan_result (image As Object)
      • startDetection As void
      • IsInitialized As boolean
      • Initialize (paramBA As anywheresoftware.b4a.BA, paramString As java.lang.String) As void
      • 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:

    Attached Files:

    Last edited: Apr 27, 2018
    stanks, David Meier, hibrid0 and 18 others like this.
  2. moster67

    moster67 Expert Licensed User

    Looks really nice Johan.
    Will test it this evening.
    Johan Schoeman likes this.
  3. Mashiane

    Mashiane Expert Licensed User

    My fav whiskey..
    Johan Schoeman likes this.
  4. moster67

    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 likes this.
  5. Johan Schoeman

    Johan Schoeman Expert Licensed User

    It was the closest thing that I had that looks like a "page of a document". Just about A4 size....lol
    Johan Hormaza and DonManfred like this.
  6. Max Koopman

    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)
  7. Johan Schoeman

    Johan Schoeman Expert Licensed User

    I have 4 Samsung devices:

    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:


    I have not looked into it to see what a possible solution might be.
    Last edited: May 26, 2018
  8. Max Koopman

    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.
    hibrid0 and Johan Schoeman like this.
  9. Mike1970

    Mike1970 Active Member Licensed User

    it remains locked on "use darker background"
    So i put the project in Debug mode and now it not install anymore
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice