I'm trying to figure out if it is possible to use a camera video stream from within a background service rather than from a normal intent.
What I had in mind is this:
start the service from my app
this service accesses the video stream and extracts features continuously; depending on the features, it sends network packets (to localhost)
user switches to another app - the service must still be running and extracting features!
Before trying to implement all that, I'd like to know if it was possible.
Thanks in advance
Nicola
yes its possible, check my answer and sample code on one similar thread
Open/Run Camera from background service in android
You have to show the fake preview over the current screen. For that you have to create the surface view with 1*1 px dimension and display the preview in that. You have to draw the surface view over other apps for that.
Check out this library that provides facility to capture image from the background even from the service.
I don't think this is possible as the camera needs a preview screen. See previous question here
Related
I am developing a Kotlin android app, when something happens in the app, I want the front-facing camera to record in the background to capture a users reaction for 5 seconds then storing the video in an object to be used in another feature, leaving the apps activity will break it so i am wondering is it possible to do this in the background?
Thanks in advance
You can use foreground services to use the camera in the background.
Foreground services keep running even after the Application is removed by the user from recents screen.
Make sure to read the restrictions applied by Android to these kinds of services:
https://developer.android.com/guide/components/foreground-services#access-restrictions
I wrote a small game. It has a front facing cam preview implemented. I want to be able to record the entire screen to a mp4. How would I do that? Anyone know a nice tutorial for recording the entire screen to mp4 (in code, so not just screenshots.. I want to enable the user to make a recording, while playing the game).
Indeed, as #Michael stated, directly accessing the screenshot and framebuffer mechanisms is not possible. That doesn't mean you're completely out of luck, though, if you want to record gaming sessions. It's just more work.
If you generate a perfect record of the various actions in your game, save that record. You can then build a service (web service, local, whatever) that accepts the record as an input. In effect, it would replay the game. You'd then reconstruct the game in such a manner that you could generate a video.
You could also replay in the Android emulator and create a video from that. Not as easily deployable, but it depends on what you want to do with it.
Apps aren't allowed to grab the screen contents (see this discussion).
[L]etting applications grab the screen's content liberally is a serious security risk, which is why the platform prevents it (taking a snapshot requires either direct physical manipulation from the user, or using a debugging tool).
And as #CommonsWare mentioned in his comment you can't use the getDrawingCache method either to grab a Bitmap of the View if you're using a SurfaceView or one of its children (e.g. VideoView). More details about that here.
I have doing a basic object detection on the camera preview screen in Android (greater than 3.2). For the devices which do not support processing on preview screen, I am buffering the preview screen, processing it and clearing the buffer. This part is working as desired.
What I now want is this app to run in the background while any other app is running in the foreground. I am using android service and am able to run a small test app in the background. However my concern is with the camera preview app.
I don't want to display the preview screen but use the preview screen information for processing. This might be too much to ask, but I wanted to know if this is even possible. I came across this link which shows some hope. Basically I want to process the video (preview) stream without displaying it on the screen. If this is doable, then I can think of putting this app in the background and some other app in the foreground.
Unfortunately I won't be able to share the code, however it is the standard logic of creating a surface view and starting the preview.
I would really appreciate any insight into this.
Check comments here.Basically he opens camera hardware, set preview callback and do startpreview without setting the previewDisplay (this might not work on every device). You can try this from your background service. All this will work if your foreground doesn't access the camera app. Please update this if it works. I am interested to know.
I am developing an application to take images from background application say using android service. i don't want any user interaction to takes images, it should completely work from background.
I have already tried -
Calling camera from service - It not always works sometime works if we have dummy surfaceview, again if Android close the related activity service stopped takings pics, I don't want to run activity for user interaction.
with some changes it working fine but all images are black.
Widget: Not getting how to call camera in widget as widget doesn't support surfaceview.
Live wallpaper: not able to make it work till now, does it supports for camera?
As per my experience till now Android camera service not designed to takes picture if no real preview available.
Is anyone developed something like this? any help will be really appreciate.
If you can ignore older versions, it may be easier to use SurfaceTexture (API 11 and higher) As vikky.rk notes in an answer to a recent question, an introduction to this technique can be found in the PanoramaActivity code of the default Android Camera App.
I was wondering whether it is possible to have 2 instances of the camera preview in android. What I mean is running 2 instances of the camera at the same time. If it is, how would one go about this, will there be need to implement an instance on a different thread? I have not used the camera API before, so I would appreciate it if I can have a heads up on the issue, so I don't waste time on it.
Thank you.
It is not possible to have two open connections to the camera - you have to lock the camera in order to get a preview and it can only be locked once. Indeed, if you have the camera locked, and your app crashes before you've unlocked it, then nobody can use the camera!
See http://developer.android.com/reference/android/hardware/Camera.html#open%28int%29
You must call release() when you are
done using the camera, otherwise it
will remain locked and be unavailable
to other applications.
...
RuntimeException: if connection to the
camera service fails (for example, if
the camera is in use by another
process).
That said, you can certainly register a preview callback and take the preview data from your single camera instance to use in multiple views. But be aware of the issues with the YUV format of the raw byte[] data provided by the preview callback: Getting frames from Video Image in Android (note that the preview data is raw from the camera driver and may vary from device to device)
Ignoring the big Why question, your best bet would be to make a service that interacts with the camera, and go from there.