B4A Library EZcamera

Usage:

B4X:
'Activity module
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.
End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim cam As EZcamera

cam.Initialize("Cam")
cam.TakePicture(File.DirRootExternal, "TestPic.jpg")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Cam_PictureTaken (FileSaved As Boolean)
   If FileSaved = True Then ToastMessageShow("Picture Saved", True)
End Sub
 

Attachments

  • EZcamera.zip
    3.3 KB · Views: 1,377

barx

Well-Known Member
Licensed User
Longtime User
This works a bloody treat, thank you. Takes all the pain out of working with camera.
 

borofan

Member
Licensed User
Longtime User
Great library, many thanks for sharing.

Only problem is that the PictureTaken event isn't always triggered on return, it looks like the app is killed in the background and reset back to the primary screen. This maybe a specific problem with Samsung devices, I'm not sure yet.

I've tried using the activity_resume / global boolean method as detailed in this post with no success.

Does anybody have a work around or a better process for using the native camera to take images?

Any help would be greatly appreciated. Many thanks.

Thanks @Erel
 
Last edited:

yuhong

Member
Licensed User
Longtime User
I have encountered the above problem at Android 5.0. "Only problem is that the PictureTaken event isn't always triggered on return"!
 

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I have experienced the same problem with EZCamera where the PictureTaken is not invoked. I've also experimented with using an intent per NJDude's code posted long ago.

I did some further investigating.

What I'm finding is that every once in a while the default camera app will fail to save the picture (or maybe it's saving it in some random location). When this happens my app is restarted via Activity.Create with FirstTime = true. This is consistent with what Erel mentioned earlier in this thread - my Activity being destroyed and re-created.

My test phones are a Nexus 5 and a Samsung S5. This error happens infrequently, mostly on the Samsung S5.

I tried on a friends Samsung S5 (AT&T version). His phone failed every time. I noticed his phone has a ton of junk-ware running. I tried reducing the size of the snapshot image taken, 16 MB is the default, down to 1 MB. Now it worked every time. Ah Ha :D

This kind of suggests that there is not enough memory to save the picture and that is what's responsible for the problem.

Can anyone offer any insights to this observation? Maybe offer some suggestions?

Barry.
 

Douglas Farias

Expert
Licensed User
Longtime User
Samsung devices and the problems with camera :mad:
99% of problems with camera its on samsung devices :(
 

Leni Berry

Active Member
Licensed User
Longtime User
i tested on samsung note5 with B4A v6 API 23...

error log as follow :

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (imagedownloader) Create **
** Service (imagedownloader) Start **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (pengaduan) Create, isFirst = true **
** Activity (pengaduan) Resume **
MapFragment1_Ready
pengaduan_btnshoot_click (java line: 423)
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=android/com.android.internal.app.ResolverActivity clip={text/uri-list U:file:///storage/emulated/0/Android/data/com.xxx.xxx/files/TestPic.jpg} (has extras) } from ProcessRecord{8285011 10541:com.xxx.xxx/u0a31} (pid=10541, uid=10031) with revoked permission android.permission.CAMERA
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3130)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1540)
at android.app.Activity.startActivityForResult(Activity.java:4283)
at android.app.Activity.startActivityForResult(Activity.java:4230)
at anywheresoftware.b4a.BA.startActivityForResult(BA.java:487)
at anywheresoftware.b4a.BA.startActivityForResult(BA.java:475)
at net.garstangs.ezcamera.EZcamera.TakePicture(EZcamera.java:65)
at com.xxx.xxx.pengaduan._btnshoot_click(pengaduan.java:423)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10815)
at android.view.View$PerformClick.run(View.java:22534)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (imagedownloader) Create **
** Service (imagedownloader) Start **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Service (httputils2service) Start **
 

Leni Berry

Active Member
Licensed User
Longtime User
i tested on samsung note5 with B4A v6 API 23...

error log as follow :

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (imagedownloader) Create **
** Service (imagedownloader) Start **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (pengaduan) Create, isFirst = true **
** Activity (pengaduan) Resume **
MapFragment1_Ready
pengaduan_btnshoot_click (java line: 423)
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=android/com.android.internal.app.ResolverActivity clip={text/uri-list U:file:///storage/emulated/0/Android/data/com.xxx.xxx/files/TestPic.jpg} (has extras) } from ProcessRecord{8285011 10541:com.xxx.xxx/u0a31} (pid=10541, uid=10031) with revoked permission android.permission.CAMERA
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3130)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1540)
at android.app.Activity.startActivityForResult(Activity.java:4283)
at android.app.Activity.startActivityForResult(Activity.java:4230)
at anywheresoftware.b4a.BA.startActivityForResult(BA.java:487)
at anywheresoftware.b4a.BA.startActivityForResult(BA.java:475)
at net.garstangs.ezcamera.EZcamera.TakePicture(EZcamera.java:65)
at com.xxx.xxx.pengaduan._btnshoot_click(pengaduan.java:423)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10815)
at android.view.View$PerformClick.run(View.java:22534)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (imagedownloader) Create **
** Service (imagedownloader) Start **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Service (httputils2service) Start **

solved by adding runtime permission...

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim cam As EZcamera
End Sub

Sub Activity_Resume

    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_CAMERA)

End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
   If Permission = Starter.rp.PERMISSION_CAMERA Then
     If Result Then cam.Initialize("Cam")
   End If
End Sub

Sub btnshoot_Click
  

  
    cam.TakePicture(File.DirDefaultExternal, "TestPic.jpg")
End Sub

Sub Cam_PictureTaken (FileSaved As Boolean)
   If FileSaved = True Then ToastMessageShow("Picture Saved", True)
End Sub

code for starter,
B4X:
Sub Process_Globals
    Public rp As RuntimePermissions
End Sub
 
Top