Camera X - Accessing both Front and Back Cameras simultaneously using Camera X API - android-camera

Trying to preview both cameras (front and back) concurrently using Android X API -
Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector, preview); //Back-Camera
Camera camera2 = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector2, preview2); //Front-Camera
With the above code-snippet, only front camera comes up. If I change the order above, back camera shows up as expected.
Tried acquiring the instances of Camera feature (cameraProvider = ProcessCameraProvider.getInstance(this)) twice, however I found strange observation by mapping one camera per one provider. Upon home press and launching app again, either one of the preview (Back or Front) shows up and there is no pattern found.
Can anyone throw more lights on this? Is it anything to do with the target device i.e. device incompatibility? The target device I am using is OnePlus 5.

CameraX doesn't support opening more than 1 camera at a time, which is why when you attempt to open 2 cameras by calling ProcessCameraProvider.bindToLifecycle() twice, only the second camera is opened.
ProcessCameraProvider provides access to the cameras on the device, and as its name suggests, it has the scope of the process/application, i.e it's a Singleton, once it's initialized, you'll get the same instance with each consequent call to ProcessCameraProvider.getInstance().

Related

Camera plugin captures images with wrong orientation

Im using camera plugin to capture images, but it produces images in landscape orientation even though I keep the phone in portrait orientation.
I tried rotating the images after capturing using a different package, but that was very inefficient as the process takes too long to complete.
Can someone tell why is it happening and is there any solution.
How I initialize the controller
controller = CameraController(
widget.cameras[1],
ResolutionPreset.medium,
imageFormatGroup: ImageFormatGroup.jpeg,
);
and I listen to the controllers imageStream.
Device: Mi Poco F1 |
Android version: 10
I need to capture a minimum of 10 frames per second and push it to a web socket continuously for 30 seconds.
So, even though the solutions in the first 2 comments actually work, they are inefficient for my requirement as they cause the UI to freeze.

Turn off camera after use in iOS 14

With the updates in iOS 14, there is a green dot that appears in the top corner when the camera is in use. My app sometimes uses the camera for AR, but it isn't needed for other aspects of the app. I'd like to make the green dot go away while the camera isn't needed so users don't think we're doing something with the camera in the background, but I can't seem to find a way to programmatically stop using the camera. Is there a way to do this?
It sounds strange – you're using AR app but do not need an AR camera. I suppose you understand that RGB sensor (as well as IMU sensors) must be active during the time when ARSession is running. ARSession is based on data coming from RGB sensor. So if you turn it off – nothing good happens.
At first let's discuss how to start and stop AR tracking. You can start running ARSession using standard ARKit procedure:
let config = ARWorldTrackingConfiguration()
sceneView.session.run(config, options: someOptions)
...and you can stop running ARSession using:
sceneView.session.pause()
After stopping ARSession, a heavy CPU/GPU processing and enormous battery energy consuming will be eliminated. However, if a session was stopped, next time you'll run it, it must begin tracking from scratch.
Solution
So the best solution in this case is to use a pre-saved scene data stored in ARWorldMap. Just track a surrounding environment, save ARWorldMap, and then stop running session. Then do whatever you want (with no active ARSession), and after it run a session again and just load ARWorldMap data. Simple.

How to make video captured by front camera not being inverse Android?

I recording video using MediaRecorder.When using back-camera,it working fine,but when using front camera,the video captured is being flipped/inverse.Means that the item in right,will appear on the left.The camera preview is working fine,just final captured video flipped.
Here is the camera preview looks like
But the final video appear like this(all the item in left hand side,appear on right hand side)
What I tried so far:
I tried to apply the matrix when prepare recorder,but it seems does change anything.
private boolean prepareRecorder(int cameraId){
//# Create a new instance of MediaRecorder
mRecorder = new MediaRecorder();
setCameraDisplayOrientation(this,cameraId,mCamera);
int angle = getVideoOrientationAngle(this,cameraId);
mRecorder.setOrientationHint(angle);
if(cameraId == Camera.CameraInfo.CAMERA_FACING_FRONT){
Matrix matrix = new Matrix();
matrix.preScale(1.0f,-1.0f);
}
//all other code to prepare recorder here
}
I already read for all this question below,but all this seems didnt solve my problem.For information,I using SurfaceView for the camera preview,so this question here doesn't help.
1) Android flip front camera mirror flipped video
2) How to keep android from inverting the image from the front facing camera?
3) Prevent flipping of the front facing camera
So my question is :
1) How to capture a video by front camera which the video not being inverse(exactly the same with camera preview)?
2) How to achieve this when the Camera preview is using SurfaceView but not TextureView ? (cause all the question I mention above,tell about using TextureView)
All possible solution is mostly welcome..Tq
EDIT
I made 2 short video clip to clarify the problem,please download and take a look
1) The video during camera preview of recording
2) The video of the final product of recording
So, if the system camera app produces video similar to your app, you didn't do something wrong. Now it's time to understand what happens to front-facing camera video recording.
The front facing camera is not different from the rear facing camera in the way it captures still pictures or video. There is a difference how the phone displays camera preview on the screen. To make it look more natural to the user, Android (and all other systems) mirrors the preview, so that you can see yourself as if in a mirror.
It is important to understand that this only applies to the way the preview is presented to you. If you pick up any video conferencing app, connect two devices that you hold in two hands, and look at yourself, you will see to your surprise that the two instances of yourself are flipped.
This is not a bug, this is the natural way to present the video to the other party.
See the sketch:
This is how you see the scene:
This is how your peer sees the same scene
Normally, recording of a video is done from the point if view of your peer, as in the second picture. This is the natural setup for, e.g., video conferencing.
But Snapchat and some other social apps choose to store the front-facing video clip as if you record it from the mirror (as if the recorder is in your hand on the first picture). Some people like this feature, others hate it (see https://forums.androidcentral.com/general-help-how/664539-front-camera-pics-mirrored-reversed-only-snapchat.html and https://www.reddit.com/r/nexus6/comments/3846ay/has_anyone_found_a_fix_for_snapchat_flipping)
You cannot use MediaRecorder for that. You can use the lower-level API of MediaCodec to record processed frames. You need to flip each frame 'manually', and this may be a significant performance hit, because normally the MediaRecorder 'connects' the camera to hardware encoder in a very efficient way, without need even to copy the pixels to user memory. This answer shows how you can manipulate the way camera is rendered to texture.
You can achieve this by recording video manually from surface view.
In such case preview and recording will match exactly.
I've been using this library for this purpose:
https://github.com/spaceLenny/recordablesurfaceview
Here is the guide how to use it (not with camera but with OpenGL drawing): https://withintent.uncorkedstudios.com/recording-screen-video-on-android-with-recordablesurfaceview-451c9daa213e

How to stop video when target is lost in augmented reality ?

I have made an AR application which play video when the target is detected. But problem is that even when I place camera not in-front of the target image (No Target) its still keep playing until I go again an pause the video by clicking on the target.
If using Vuforia, there is a callback function, OnTrackingLost(), indicating that the tracker has been lost. You can stop the video in the body of this function.
If using another technology and you have to implement such a function by yourself, the obvious solution would be to use a timer. If the target image (previously recognized and tracked) is not detected for a given period of time, the tracker is lost. Again, you stop the video when you realize that the tracked image is lost.

barcodereader sample application

I am using the barcodereader sample application provided in the cascades samples to embed a QRCode scanner into my application.
As it stands the sample is great, but I want the scanner to open as soon as the user navigates to my screen and I want to get rid of the opening slider images that are in the sample.
Firstly, I have tried removing the images and their animations and adding the action:
onCreationCompleted: {
camera.open()
}
to the Page. This opens the camera perfectly as expected, but for some reason, the bacrode just doesn't scan.
So, I wound back a step, and this time I just put the code in exactly as is and just changed the code to read:
onCreationCompleted: {
startupAnimation.play()
}
As expected, the screen open, plays the annimation, but still it fails to read barcodes, however, if I invoke the animation again (by tapping the screen), the animation plays again and the scanner reads the barcode without any issues at all.
All I can think of is that this is a timing issue and that I need some sort of delay after the screen has been created before the camera can be started as a barcode reader?
Anyone able to help?
Thanks,
Douglas
To get scanning right away at application launch, you need to make sure the camera is actually set up and initialized.
Basically, in onCreationComplete, open the camera. In onCameraOpened, start the viewfinder. In onViewfinderStarted, set the barcode detector camera to be the camera.