Camera release Invalid Op on Moverio - android

I'm getting an Error on the Moverio Glasses bt200 while releasing Camera. On Nexus 10 everything works fine. The part, where I'm getting the Error looks like this:
protected void onPause() {
super.onPause();
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mPreview.getHolder().removeCallback(mPreview);
releaseCamera();
}
private void releaseCamera() {
if(mCamera != null) {
mCamera.release();
mCamera = null;
}
}
The Error-Message is:
hardware/ti/omap4xxx/camera/BaseCameraAdapter.cpp:1794 setState - Adapter state switch INTIALIZED_STATE Invalid Op! event = CAMERA_STOP_IMAGE_CAPTURE
Does anybody have any Idea, why I'm getting this Error?
Thanks in advance.

change onPause() to...
protected void onPause() {
super.onPause();
releaseCamera();
}

Related

Slow performance when using a Camera Preview in a fragment

My app contains three fragments. I need to keep the user experience fluid so I'm using a the setOffscreenPageLimit() method to keep them all alive in memory.
Problem: when I start a new activity (even empty), It loads slowly. Same when I finish it.
I know this is coming from my Camera Preview fragment because when I comment out the init of the camera, everything runs very smoothly.
Here is how I initialize my camera preview on the OnResume method:
mCamera = GetCameraInstance(currentCameraId);
//-- Set the SurfaceView
preview = (SurfaceView) view.findViewById(R.id.camera_preview);
mSurfaceHolder = preview.getHolder();
mSurfaceHolder.addCallback(this);
if (mCamera != null) {
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
}
When the new activity is started, the surfaceDestroyed method is called which destroys the Camera preview. When the new activity is terminated, the app recreate a camera view again.
#Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
The time spent on stopPreview() and release() method as seen on traceview. It takes around 700ms to destroy the camera preview.
Screenshot of Traceview
Based on #CommonsWare suggestion, here is how I did it.
First, I placed my mCamera variable in the Application class
public Camera mCamera;
Then, I placed the AsyncTask in my fragment
private class ControlCameraTask extends AsyncTask<Integer, Void, Void> {
protected Void doInBackground(Integer... urls) {
//--
if (app.mCamera != null) {
app.mCamera.stopPreview();
app.mCamera.release();
app.mCamera = null;
} else {
app.mCamera = GetCameraInstance(currentCameraId);
app.mCamera.setPreviewDisplay(app.mSurfaceHolder);
app.mCamera.startPreview();
}
return null;
}
}
Finally, I just call my AsyncTask on onResumeand onPausemethods
#Override
public void onResume() {
super.onResume();
new ControlCameraTask().execute(1);
}
#Override
public void onPause() {
super.onPause();
new ControlCameraTask().execute(1);
}
PS: I removed the trycatches for the code to be easily readable.

where should I release the camera in my android activity

I am making an android flashlight app. I it has two activities, a main activity and a settings activity where the camera led can be toggled on and off. It also has another class where all the camera changes are handled like opening it, releasing it, and turning the light on and off.
I kept getting errors when turning on the led based on shared preference settings because I was not opening and releasing the camera in the correct activity lifecycle stages. I fixed the issues by releasing the camera when onPause is called in either activity and turning the led on or off (based on shared preference settings) when onResume is called in either activity.
The problem I am having now is that if the led is on, it turns off briefly when switching from one activity to the other because I have to release the camera and then open again in the new activity. Can anyone help me figure out a way to eliminate this problem? Where should I open and release the camera? I have tried releasing it in on destroy but the led stays on when the app is minimized to the background, which is undesirable. Thanks for any suggestions.
In both main and settings activity I have:
#Override
protected void onResume() {
super.onResume();
if (sp.getBoolean("LED_TOGGLE_CB", false) == true) {
flash.turnFlashOn();
}
}
#Override
protected void onPause() {
super.onPause();
flash.killCamera();
}
In the Flash class I have:
void getCamera() {
if(camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch(RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
void turnFlashOn() {
getCamera();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
}
public void killCamera() {
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}

OnResume Camera Reinit Black Screen

I have a problem. After initializing the camera for a preview and bringing another app into focus, then back to my app: the preview shows up black. If I continue to take a picture, it takes a picture of where I point the camera normally.
Am I doing something wrong on the OnResume() override? Relative code is below:
public void ReleaseCamera()
{
if (myCamera != null)
{
myCamera.Release();
myCamera = null;
}
}
protected override void OnPause()
{
base.OnPause();
if (myButtonState == ButtonState.CameraActive)
ReleaseCamera();
}
protected override void OnResume()
{
base.OnResume();
if (myButtonState == ButtonState.CameraActive)
InitializeCamera();
}
private void InitializeCamera()
{
SurfaceView mySurfaceView = FindViewById<SurfaceView>(Resource.Id.surfaceView1);
myCamera = Android.Hardware.Camera.Open(cameraNumber);
Android.Hardware.Camera.Parameters p = myCamera.GetParameters();
myCamera.SetDisplayOrientation(90); // Portrait
myCamera.SetPreviewDisplay(mySurfaceView.Holder);
myCamera.StartPreview();
}
Thank you for your help. :)
onResume() gets called too early. You don't have the surface holder ready at this stage. You can try to introduce onPostResume() handler in your Activity, and/or handle the SurfaceHolder.Callback.surfaceChanged() event.

recreating camera object after error 100 (camera server died)

I have classic android app with camera preview (common implem that can be found in many tutorials [marakana etc.]) that is supposed to take picture in a given time interval. Threading and killing threads is done, errors such "method called after release" are handled. But sometimes the well-known error 100 occurs. I accepted the fact that it happens and tried to handle it too. I implemented ErrorCallback and its onError method where the current camera object is released and instantiated a new one as written in official documentation.
But (with no surprise) it is not enough. New camera is maybe wrongly allocated because an message "CameraDemo has been exited unexpectedly" appears now.
I've read many docs and examples in hope, that a proper proceeding will be somewhere explained but no one has such problem apparently. So I would like to ask what else should I do beside releasing and creating new camera? Here is the code:
ErrorCallback CEC = new ErrorCallback()
{
public void onError(int error, Camera camera)
{
Log.d("CameraDemo", "camera error detected");
if(error == Camera.CAMERA_ERROR_SERVER_DIED)
{
Log.d("CameraDemo", "attempting to reinstantiate new camera");
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release(); //written in documentation...
camera = null;
camera = Camera.open();
}
}
};
Shortly - if I release and recreate camera in onError callback then RuntimeException Method called after release (takePicture) is raised. So should I somehow assign the surface holder to camera again or recreate the surface holder too?
It would be enough to direct me e.g. to some forums, where it is described or solved, etc. Thanks for any help.
In my app to handle the camere i use this :
public void onResume() {
super.onResume();
if(mCamera == null)
mCamera = getCameraInstance():
}
public static Camera getCameraInstance() {
mCamera = null;
try {
mCamera = Camera.open();
Parameters parameters = mCamera.getParameters();
mCamera.cancelAutoFocus();
mCamera.setPreviewCallback(yourPreviewCb);
mCamera.startPreview();
mCamera.setParameters(parameters);
mCamera.autoFocus(yourAutoFocusCB);
} catch (Exception e) {
//TODO
}
return mCamera;
}
The mCamera = null in the getCameraInstance() is just to be sure there is no camera running at all.
I think you need to recreate a complete camera, not just open it with the
camera.open();
Set this in the onResume or in the error callback, depending on your needs.
This is how I fixed it, here is a sample of code, think you get the idea:
private Camera camera;
// code...
public Camera getCameraInstance() {
Camera camera = Camera.open();
// code...
camera.setErrorCallback(new ErrorCallback() {
#Override
public void onError(int error, Camera camera) {
if(error == Camera.CAMERA_ERROR_SERVER_DIED) {
releaseCamera();
startCamera();
}
}
});
return camera;
}
protected void startCamera() {
if(getCamera() == null)
setCamera(getCameraInstance());
refreshCamera();
}
protected void releaseCamera() {
if (getCamera() != null) {
getCamera().release();
setCamera(null);
}
}
public Camera getCamera() {
return camera;
}
public void setCamera(Camera camera) {
this.camera = camera;
}

After stop camera service, the camera does not work anymore

Is there any way to correctly stop the camera service? This does not seem to work:
public void surfaceDestroyed(SurfaceHolder holder){
camera.stopPreview();
camera = null;
}
After doing that the camera does not work, even if I use the program that is preinstalled in my handset.
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera=null;
}
This works fine for me. Make sure your surfaceDestroyed() method is getting invoked.
If you are using the the preview callback you might want to deregister it in your surfaceDestroyed
mCamera.setPreviewCallback(null);
mCamera is supposed to be your Camera object

Categories

Resources