Camera.setPreviewDisplay() throws Exception [duplicate] - android

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android Camera will not work. startPreview fails
I am trying to set a camera preview in a custom SurfaceView but I get an exception each time I execute the initialization method.
Below is the code for camera preview initialization:
private void init(Context context)
{
setFocusable(true);
mRecording = false;
fileRW = new FileReaderWriter();
frameCount = 0;
if(mCamera == null)
{
mCamera = Camera.open();
}
Parameters parameters = mCamera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
mCamera.setParameters(parameters);
try {
mCamera.setPreviewDisplay(surfaceHolder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.startPreview();
}
the line mCamera.setPreviewDisplay(surfaceHolder); throws an exception (setPreviewDisplay failed) each time I try to execute the method.
Does anyone know what could be wrong? I would really appreciate any of your help.
Thanks!`

I completely agree with Jon Bright
I couldn't figure out what was going on for a week, I ignored the setType on the surface holder because the SDK said it was deprecated, ie.
"This method is deprecated. this is ignored, this value is set automatically when needed."
But if you don't do that, it will crash on setPreview. This is running 1.5 SDK (I need it to be backwards compatible to that) on a Galaxy S with 2.1. So make sure you set the type. Not quite as automatic as the documentation makes it sound.

The best place to call setPreviewDisplay() is in surfaceChanged() If the surface is just created, surfaceChanged will be called at least once and you can startPreview() and setPreviewDisplay there. If the surface changes and the preview already starts, you can stopPreview/setPreviewDisplay/startPreview there. Even if your app does not change the size of the surface, the framework may still unexpectedly call surfaceChanged() when the app starts or exits due to orientation changes. So your app really needs to handle surfaceChanged properly. You can trace the source code of camera application in Android for reference.
The code snippet in another answer works if surfaceChanged() is only called once in the app lifecycle.

Related

in foreground service, check if any other app is using camera?

I am using camera to take pictures in the foreground service, which can be used long term like 5 to 10 hours. The problem is when another app starts using camera while the foreground service is running it stop taking pictures and user have to manually restart the service.
I am taking pictures in a loop ( I initializes the camera instance takes a picture then releases the camera and repeat). The solution I see here, is that at the start of each loop iteration I should add a check to see if the camera is currently in use.
How exactly can I check if the camera is in use by any other application or process on device? or is there a better approach to my problem?
If you are using camera2 api, checkout CameraManager.AvailabilityCallback callback.
it will inform you via callback when a cameraID is available/unavailable.
This worked for me!!
if returns true, means camera is free
public boolean isCameraFree() {
Camera camera = null;
try {
camera = Camera.open();
} catch (RuntimeException e) {
return false;
} finally {
if (camera != null) camera.release();
}
return true;
}

Android MediaRecorder saving empty file and weird camera behaviours

I am having a headache over the Camera API 1 for android. After reading all of the Internet content, I made some sample app that works OK. It creates a service, which then is used to operate with the camera in the background, so there is no preview or activity enabled. To achieve this I use a dummy SurfaceHolder, like this:
protected class MySurfaceHolder implements SurfaceHolder {
private final Surface surface;
private final SurfaceTexture surfaceTexture;
public MySurfaceHolder () {
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
if (textures.length > 0) {
this.surfaceTexture = new SurfaceTexture(textures[0]);
this.surface = new Surface(this.surfaceTexture);
} else {
this.surface = null;
this.surfaceTexture = null;
}
}
[...]
}
and then I use it like this
// simplified version of my code
try {
initializeCamera(); // open camera and set Camera.Parameters
camera.setPreviewDisplay(new MySurfaceHolder());
camera.startPreview();
camera.unlock();
initializeMediaRecorder(); // create MediaRecorder, set video/audio parameters
mediaRecorder.prepare();
mediaRecorder.start();
// wait until recording finish and exit
} finally {
stopRecording();
}
the Camera and MediaRecorder initialization methods are just like the documentation states they should be (and they work).
Everything works and operates as it should. Almost everything - sometimes, under unknown circumstances the MediaRecorder creates empty files, like 32kB containing only headers and info about the video - no frames. The longer I record like this, the bigger is the file (few kB every few seconds). After 1 minute, the file weights about 80kB. Funny thing is I know that the camera is working and capturing frames (I debugged it a little showing preview frames), but the frames are not written into the output file.
Also when it happens I am not able to record in FHD (1920x1080) - I get the "start failed" message - at this time camera is not capturing frames. The same thing could happen when I use wrong (not supported) video size. I suppose in this case the message is thrown at the mediaRecorder.start(); line, and stopRecording(); is invoked but I am not sure.
After some time or after unknown action the problem is suddenly gone (I don't know when, I don't know how). It happens for sure on Android 5.1, but may happen on other versions as well.
Could this bug be related to my custom surface code?
What could cause the MediaRecorder to not write frames into a file?
Why I am not able to record in FHD, but in the same time I am able to record in HD (1280x720)?
Is there any alternative for MediaRecorder, so I can avoid these bugs?
May it happen when another app is trying to get Camera object, thus distrupting current recording? If so, how to regain access to the Camera object (I apparently am not able to do this now on some devices).
EDIT:
I think I might have a clue. I am calling
camera.setOneShotPreviewCallback(new Camera.PreviewCallback() {
// ... get current frame
}
camera.startPreview();
to get preview frame of current recording. It appears that the bug occurs when I am using this method to get preview frame (at random times). It seems flawed, because not all devices react to this thing properly (sometimes there is no preview frame...). Is there any other, better method of handling current preview frame without the real surface?

How to disable/modify AutoFocus and AutoWhiteBalance on Android Camera using OpenCV

I'm using Android + Opencv(new to opencv) and I'm currently working with real time object detection (the object stays really close to the android device Camera) , and I noticed that the Android camera's autoFocus keeps modifying my frames (kind of 'zoom in' and 'zoom out' effect) which make it harder for me to keep tracking the object.
I need to turn the "AUTO FOCUS" off because in my case the more blurred image input I have, the better, and I also need to turn the AutoWhiteBalance off as well, or maybe set to a different value.
I would like to know how to do it through my OpenCV CameraBridgeViewBase so I could modify the camera's Focus/WhiteBalance settings.
I've trying to find a way to solve it, and I noticed that many people face the same problems.
Here, at Stack Overflow, would be a great place to find someone who have worked with that and found a good way to overcome these problems.
create your own subclass of javacameraview
public class MyJavaCameraView extends JavaCameraView {
where you can have access to mCamera;
add whatever camera access using method you are interested in
for example
// Setup the camera
public void setFlashMode(boolean flashLightON) {
Camera camera = mCamera;
if (camera != null) {
Camera.Parameters params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
and use this new class as part of the main activity
//force java camera
mOpenCvCameraView = (MyJavaCameraView) findViewById(R.id.activity_surface_view);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.enableView();

Trouble Accessing Front Camera on Android Galaxy S3

I have a small app I have been working on that uses the front camera. The way I have been obtaining use of the front camera seems to work on most phones, but users have been reporting trouble on the S3 and various other new devices. The way I have been accessing the front camera is like so:
// Find the ID of the front camera
CameraInfo cameraInfo = new CameraInfo();
for(int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if(cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
defaultCameraId = i;
mCameraFound = true;
}
}
if(!mCameraFound)
displayDialog(8);
From some of the error reporting I've added into the app, I've noticed the S3 actually finds the front camera, but users report it only shows a blank screen? I have only been able to test on the devices I have (GNex and N7). I was hoping someone here may have some experience with this or may be able to help me solve this issue. If you want to try the app out on your S3, check the link below. Thanks in advance.
https://play.google.com/store/apps/details?id=com.wckd_dev.mirror
EDIT: I created a MirrorView object which contains a TextureView used for the preview. The MirrorView object implements a SurfaceTextureListener. Within the onSurfaceTextureAvailable() method is where the preview is started. I also created a method for restarting the preview after the app has gone from hidden back to visible.
So this is called when the app is first started:
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
try {
if (mCamera != null) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.height, mPreviewSize.width);
requestLayout();
mCamera.setParameters(parameters);
mCamera.setPreviewTexture(surface);
mCamera.startPreview();
}
}
catch(RuntimeException e) {
// Log.e(TAG, "RuntimeException caused by setPreviewTexture()", exception);
}
catch (IOException e) {
// Log.e(TAG, "IOException caused by setPreviewTexture()", exception);
}
}
The restartPreview call is to an identical (but separate) method. From some of the debug data I've been collecting through users, I've noticed that the app finds two camera on the S III and selects the id matching CAMERA_FACING_FRONT. Also, this issue doesn't seem to be happening on all S III. I have users who have feedback reporting as much. The latest report from a user experiencing this issue was an AT&T S III user. Any help would be appreciated!
Got some face time with an S3 tonight that was experiencing this issue with my app. Here what was going on. The TextureView relies on 2d hardware acceleration which is supposed to on by default (from what I understood) on 4.0+ devices. It wasn't turning on (for my app at least) on his phone. The fix was as simple as adding a single line in the manifest (under application).
android:hardwareAcceleration = "true"

ColorEffect not visible on camera preview

I am trying to get a camera preview with a color effect applied to it, such as for example the NEGATIVE effect. There are no errors, and the preview is visible without problems, but independent of the ColorEffect I set - the camera preview remains unchanged. I tested if the effects I am trying to use are available to my phone by running params.getSupportedColorEffects() (also these effects also work in the built in photo app).
I have no idea what is wrong with the code - I am posting it below. Perhaps someone here has an idea what could make this work? Thanks in advance.
public class CustomCameraView extends SurfaceView{
Camera mCamera;
SurfaceHolder mHolder;
public CustomCameraView(Context context){
super(context);
mHolder = this.getHolder();
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mHolder.addCallback(mSurfaceHolderListener);
}
SurfaceHolder.Callback mSurfaceHolderListener = new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
mCamera=Camera.open();
try {
mCamera.setPreviewDisplay(mHolder);
}
catch (Exception e){ }
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
Camera.Parameters params = mCamera.getParameters();
params.setColorEffect(Camera.Parameters.EFFECT_NEGATIVE);
mCamera.setParameters(params);
mCamera.startPreview();
}
public void surfaceDestroyed(SurfaceHolder arg0)
{
mCamera.stopPreview();
mCamera.release();
}
};
}
After some testing it turned out the problem could be related to the HTC Desire I was testing on (or maybe its OS version). The code works correctly on some other Samsung phones. I haven't figured out what could be the problem on the HTC.
UPDATE:
I have managed to get the effects working, but truly by accident, and I still don't understand why. But I will give the answer here - perhaps someone will find it useful, or maybe will be able to explain why it happens this way:
I added the following line to the surfaceChanged method because I was trying to decrease the size of the preview:
previewHolder.setFixedSize(width, height-1);
This had the result of making the selected effect visible.
When I changed this line to:
previewHolder.setFixedSize(width, height);
the effect was not visible any more once again. So odd.... it works for set height being anything less than the received height parameter.
I have been struggling with this as well. I found out that the HTC Desire its camera needs a strange order of executing the setParameters, setPreviewDisplay and startPreview for the color effect to work. The order is:
Camera.Parameters parameters = camera.getParameters();
//set the parameters
camera.setParameters(parameters);
camera.startPreview();
camera.setParameters(parameters);
camera.setPreviewDisplay(surfaceHolder);
Calling startPreview before setPreviewDisplay is documented in the Android SDK as a way of initializing the camera and the surfaceView in parallel.
Regarding your update about getting the effects to work by accident, the same happend to me! I assume for the same reason, some of my code got called twice in quick succesion (in my case due to a changing database object). This caused the method to (re)set the parameters and (re)start the preview to be called twice producing the desired result. After realising this and some more experimenting the above order seemed to work on both my HTC Desire and Acer Iconia A500 and I was quite happy with it.
However I have just received a comment for my application saying it produces corrupted images on the HTC Desire HD so I would recommend not using this order of camera initialization as a default but rather as a fix for the HTC Desire.
After setting new parameters to camera and starting preview invalidate() are calling on your SurfaceView . But it only Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future. So there is no guarantees that onDraw() will be called immediately. But onDraw() are always invoking after calling onMeasure() with size differs from current. So it can be a reason of this odd behavior.
Simple answers use following type :
Camera camera = null;
camera = Camera.open();
if (camera != null) {
try {
Camera.Parameters parameters = camera.getParameters();
// Set all kind of stuffs here..
parameters.setSceneMode(Camera.Parameters.FLASH_MODE_AUTO);
parameters.setColorEffect(Camera.Parameters.EFFECT_SEPIA); // whatever effect you want
camera.setParameters(parameters);
camera.setPreviewDisplay(surface_holder);
camera.startPreview();
} catch (IOException exception) {
camera.release();
camera = null;
}
}

Categories

Resources