Android Question How to automate Camera2 to take a photo every 'x' minutes.

ricprehn

Member
Dear all,

I am trying to automate the example 'Camera2' from Erel (https://www.b4x.com/android/forum/threads/camera2-still-images-and-videos.83920/#content) to make timelapses. I would like to be able to call the function TakePicture every 'x' minutes automatically. Should I use a timer? or StartServiceAt? What is the best way to archive the best functionality, less battery drain and assure that android will not kill the activity? I will appreciate if you can give me some advice, I am a newbie in B4A, trying to learn this powerful software.

Best regards,
Ricard.
 

ricprehn

Member
Dear Erel,

I started a timer in the B4XMainPage of your "camera2still images and videos example". I am calling "TakePicture" every tick. PhoneWakeState is on. This metyhod works well, but because the screen is always on, it will drain the battery very fast. Is there any way to call "takePicture" every 'x' minutes with the screen turned off?

Many thanks,
Ricard.

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("1")
    Root.LoadLayout("StillPicture")
    VideoFileDir = rp.GetSafeDirDefaultExternal("")
    VideoFileName = "1.mp4"
    cam.Initialize(pnlCamera)
    Log(cam.SupportedHardwareLevel)
    buttons = Array(btnScene, btnAutoExposure, btnEffects, btnFocus, btnMode)
    SetState(False, False, VideoMode)
    Timer1.Initialize("Timer1", 300000)
    Timer1.Enabled = True 
    Log ("timer on")
    pws.KeepAlive(True)
End Sub

B4X:
Private Sub Timer1_Tick
    TakePicture
    Log ("Timer activated")
End Sub
 
Upvote 0

ricprehn

Member
Hello,

Is there any way to take a photo using Camera2 with the screen turned off?
I was trying to pws.KeepAlive(True) before calling TakePhoto and pws.ReleaseKeepAlive after. But it doesn't work.

I will appreciate any help.
Thanks!
 
Upvote 0

emexes

Expert
Licensed User
You cannot take pictures when the screen is turned off.

Is it possible to turn the screen off while waiting for the timer to fire, and then turn it on and wait a second before taking the photo, and then repeat all that?
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Is it possible to turn the screen off while waiting for the timer to fire, and then turn it on and wait a second before taking the photo, and then repeat all that?

Hang on, isn't there something under display settings that dims or darkens the screen, separate to the sleep mode delay setting?

I was going to highlight the last couple of lines here, where it sounds like PartialLock will let the screen go dark but keep the phone alive:

1665666125406.png

1665666216948.png


BUT THEN I SAW THIS:

1665666248488.png


and maybe the solution is to set BrightScreen to False instead of True.
 
Upvote 0

ricprehn

Member
B4XPages continue to run when the screen turns off. Make sure that it isn't your code that pauses the camera while the screen turns off.
I am uploading my modified code, it is basically your CamEx2 example with a timer. When I run the code, It works for three photos. Then it fires these errors. (my screen goes off after 20 seconds).

I disabled
B4X:
Sub B4XPage_Background
    'cam.Stop
End Sub

I get same results with and without lock.PartialLock.

Regards,
Ricard.

error.jpg
 

Attachments

  • Camera2_timer_intent.zip
    18.6 KB · Views: 101
Upvote 0

ricprehn

Member
Hang on, isn't there something under display settings that dims or darkens the screen, separate to the sleep mode delay setting?

I was going to highlight the last couple of lines here, where it sounds like PartialLock will let the screen go dark but keep the phone alive:

View attachment 134754
View attachment 134755

BUT THEN I SAW THIS:

View attachment 134756

and maybe the solution is to set BrightScreen to False instead of True.
Good point Emexes, I tried KeepAlive(False) and the screen goes to the lowest dimming, but still on. Not ideal, because screen remains powered and will empty the battery very fast.
 
Upvote 0

emexes

Expert
Licensed User
Is there a longer-term timer/wakeup mechanism, eg for alarm clock functions?

I am slowly but surely - and definitely confusedly - crawling towards getting StartServiceAt working:

B4X:
Dim NextTime As Long = DateTime.Now + 10000
Log("Rebooking for " & NextTime)
StartServiceAt(PhotoTime, NextTime, True)    'PhotoTime = Project, Add New Module, Service Module

https://www.b4x.com/android/forum/threads/help-with-starting-service-with-startserviceat.67087/

https://www.b4x.com/android/forum/t...-use-startserviceat-in-a-loop.130989/#content
 
Upvote 0

ricprehn

Member
Dear all,
GeoT from spanish forum wrote a working code to automate camera2 (https://www.b4x.com/android/forum/threads/necesito-desarrollar-app-que-tome-fotografias-cada-10-min-de-manera-automática.143389/post-909578). It works great with my android 12 phone. It works even when the screen is off. The problem is that consumes a lot of battery, maybe because the camera remains opened all the time. I was trying to do a <cam.stop> between photo and photo and recreating layout and calling <cam.Initialize> before doing a new photo, but it didn't work with the screen off, the code was unable to start the camera. I will appreciate if you could take a look and see if there is any method to minimize battery draining between photo intervals.

Thanks a lot and best regards.
 
Last edited:
Upvote 0
Top