i have problems with this, recently i have also problem with just taking 1 photo...
do you know how to solve it? any working code?
i'm doing it like that:
mCamera = Camera.open();
Log.e(TAG, "onCreate");
mCamera.takePicture(null, mPictureCallback, mPictureCallback);
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c)
{
if (imageData != null) {
//process photo
}
}
};
in onDestroy() i release the camera
on simulator it works, on telephone - 1st time runs without problems but no picture, second and next times - 'force close' - looks like camera is not released ...
Related
I have a live wallpaper that just draws triangles, very simple, and runs smoothly all time. After testing found that when I open the camera to take pictures the phone freezes for about 10 seconds, then the camera opens and the wallpaper crashes and the message "Unfortunately, Live Wallpaper has stopped." appears. Also found that when I open any barcode scanner app the problem raises again because the scanner uses the camera too. Seems the problem raises when the camera app runs. Any ideas what's causing it?
hi #Bullet Camera is just open ones, if it open through any other apps then, you can can not access your camera, Solution is that, where you use camera please release that after no used.
main problem is that, in your app (app in that you are using camera) , so , you release camera after used.
like following :
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
when you need
private void releaseCameraAndPreview() {
if (camera != null) {
camera.release();
camera = null;
}
}
or
public void stopCamera() {
if (cameraDevice != null) {
cameraDevice.stopPreview();
cameraDevice.setPreviewCallback(null);
cameraDevice.release();
cameraDevice = null;
System.out.println("in to the stop video");
}
}
When I tried to take a single picture from camera, it works fine. And now I added little bit of modification on my code, and wanna take two consecutive picture from camera.
public void takePicture(final boolean isWithFlash) {
Camera.PictureCallback mCall = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
if(isWithFlash) {
savePhotoToDirectory(data, captureImageFileName);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
} else {
savePhotoToDirectory(data, captureImageFileName);
takePicture(true);
}
}
};
if(mCamera != null) {
if(!isWithFlash) {
Parameters param = mCamera.getParameters();
mCamera.takePicture(null, null, mCall);
} else {
Parameters param = mCamera.getParameters();
param.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(param);
mCamera.takePicture(null, null, mCall);
}
} else {
Log.d("MYLOG", "Camera is null");
}
}
What I'm trying to do is take a picture without flash, and then take an another picture with flash consecutively. However, when I tried my code, it only takes first photo, and second onPictureTaken() function is not being called.
What am I doing wrong here? Or is there any better way to take two consecutive pictures?
Any comments would be really appreciated!
You don't need to call mCamera.stopPreview() after second call. But you need to call mCamera.startPreview() after the first one. I would introduce some delay between two calls to takePicture(), e.g. bu using View.post() to take the second picture. But maybe this post is not necessary, and delay that it causes is too much for your purposes - that's for you to decide.
I'm trying to create a Glass GDK app that uses the Camera service to show a preview. Unfortunately, I currently have a bug where a RuntimeException is thrown when trying to open a Camera using Camera.open(). I only encounter this bug when opening the activity through a voice trigger, not by selecting the app from the "launcher" menu.
Is there a difference in how an Activity is launched through this menu versus the voice trigger?
Some of the relevant code is below.
#Override
public void onCreate(Bundle savedInstanceState) {
mGestureDetector = createGestureDetector(this);
super.onCreate(savedInstanceState);
ctx = this;
act = this;
setContentView(R.layout.activity_main);
preview = new Preview(this, (SurfaceView)findViewById(R.id.surfaceView));
((FrameLayout) findViewById(R.id.preview)).addView(preview);
preview.setKeepScreenOn(true);
}
#Override
protected void onResume() {
super.onResume();
try {
if (camera == null) {
Log.d(TAG, "Opening a camera on resume.");
camera = Camera.open();
preview.setCamera(camera);
camera.startPreview();
}
} catch(java.lang.RuntimeException e) {
Log.e(TAG, e.getMessage());
}
}
#Override
protected void onPause() {
if(camera != null) {
camera.stopPreview();
preview.setCamera(null);
Log.d(TAG, "Releasing a camera on pause.");
camera.release();
camera = null;
}
super.onPause();
}
#Override
protected void onDestroy() {
if(camera != null) {
camera.stopPreview();
preview.setCamera(null);
Log.d(TAG, "Releasing a camera on destory.");
camera.release();
camera = null;
}
super.onDestroy();
}
Since it doesn't work when using the voice trigger, it sounds like a possible race condition where the microphone isn't released by the time your activity is displayed on the screen.
Can you try an approach that uses exponential back-off to capture the camera? Basically try to capture the camera and if you get an exception, try again after a short amount of time, increasing the wait time slightly for a fixed number of attempts.
Please also consider filing a bug on the issue tracker, especially if you can reliably find out how much of a delay is needed before the camera/mic can be acquired.
The problem is caused by the delay between the voice recogniser closing event and the camera open event, which is causing a memory overload.
To avoid the problem when launching the app which will be triggered with voice,
pause the app for certain time (1000 Milli seconds will do) from opening the camera soon.
In the below code I am delaying my QR scanner to open from opening for 1000 Milli seconds. This works fine for me. If you want a you can increase the time interval.
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, 0);
}
};
// sleeper time
handler.sendEmptyMessageDelayed(0, 1000);
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;
}
I want to capture images through Intent
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
but the problem is that with this code the camera is started and the user has to click on the camera button to capture the image , but what i want is that the camera sholud start and take picture without any furthur interaction with the user
I want to do this using INTENT
That is the way I did it :
Declare an instance of Camera, and SurfaceHolder.
Create an Object CallBackPicture, and implements the method on PictureTaken (method launched when you want to take a picture)
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Camera.PictureCallback mCall = new Camera.PictureCallback()
{
#Override
public void onPictureTaken(byte[] data, Camera camera)
{
//DO YOUR STUFF
}
};
// Open the instance of camera
mCamera = Camera.open();
try {
// Call the preview (not sure if it is working without this call
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(mCamera == null) Log.i(TAG, "mCamera is null");
// Will call the onPictureTaken implemented above
// Look at the documentation : public final void takePicture
mCamera.takePicture(null, null, mCall);
mCamera.stopPreview();
mCamera.release();
By modifying this, you should be able to do what you want..
Don't forget to modify the Manifest.XML too, but I think you've already done it!
EDIT : sometimes there is some problem with stoppreview() and release()..
So, the thing i've done is :
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
mCamera = Camera.open();
etc...
This can't be done. There are only two options:
Invoking Camera app via Intent. The user then uses Camera app in normal way - i.e. presses the button when ready.
Use Camera class - this is much more work compared to running Camera app via Intent. But it gives you full control.
When you call the camera intent you basically "run" the camera app (or other app that registered on this intent), so basically, you can't control of how it works.
You can use the Camera API...take a look here