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.
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
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.
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?
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?
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.
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.
As the error message says the camera is disabled. You cannot use the camera while the app is in the background (=screen is off). I guess that the other app you mentioned turns on the screen right before it opens the camera.
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
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.