Android Camera "Error -1" wenn calling camera.takePicture - android

in my Activity, i want to take a picture with android.hardware.Camera.
The code (see below) works fine in my AVD, but it doesn't work on my Android phone - all I get is "Error -1".
AVD:
Target: Android 2.3.3
SD Card: 64 MB
WVGA800
Phone:
Samsung Galaxy S2 with Android 2.3.6
Code:
android.hardware.Camera camera = Camera.open();
camera.takePicture(null, null, mPictureCallback);
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
Manifest:
uses-feature android:name="android.hardware.camera"
uses-permission android:name="android.permission.CAMERA"
I don't think that the PictureCallback matters - when I comment everything in the onPictureTaken method, the same error is returned.
Also, I have restarted my phone, tried setting a few Camera parameters etc. - but nothing helps.
I can't find this specific error code for the Camera either.
Thanks in advance!

Without a correct assigned SurfaceView it will not work.
Even the SurfaceView must have a minimum size.

Your code have a important missing part. You need to call startPreview(), before calling takePicture(). Other important thing is that the photo taken can delay a little and java Garbage Colletor can collect your camera variable, before you have the result. So release the camera variable on the Picture Callback method.You also dont need to explicity define the setPreviewCallback(null), you can remove it from your code.
It is important to avoid the excution of startPreview() twice before the picture be taken. Disable the element on your interface and enable after (and inside) the Callback method.

Related

stopPreview/takePhoto stops freezing image after setPictureSize to camera parameters

So I am making an application where you can take some pictures.
I had everything working as it should besides of the PictureSize.
After you take a picture, the preview is supposed to freeze with the last image taken, just like TakePhoto usually do for you. This works as it should until i do parameters.setPictureSize in this method:
public void setupCameraParameters() {
Camera.Parameters parameters = mCamera.getParameters();
Camera.Size preSize = determineBestPreviewSize(parameters);
Camera.Size picSize = determineBestPictureSize(parameters);
parameters.setPreviewSize(preSize.width, preSize.height);
parameters.setPictureSize(picSize.width, picSize.height);
parameters.setRotation(90);
mCamera.setParameters(parameters);
}
This gives my pictures the correct sizes, however what happends now is that my preview doesn't freeze after TakePhoto is called, it just keeps feeding from my camera.
I have even tried doing mCamera.stopPreviewing() without any freezing images. If i release the camera, the image gets black as it should do so I really am working on the right camera-instance as well.
Is there any documentation I have missed? Have someone else entered this problem before.
EDIT:
So I got back my xperia z3 compact from repair and checked this issue again. On z3 this never is an issue, the code will take a picture and then freeze the preview like I want it to. My other phone was a HTC one (m7).

Strange android camera behaviour when torch is on

I have following android code (written here in pseudocode):
mCamera = configAndInitialize(); //all I want to do before taking picture
mCamera.startPreview();
mCamera.torchOn(); //setting parameters with flash mode torch
onClick(){
mCamera.stopPreview();
mCamera.takePicture();
mCamera.torchOff();
}
Sometimes (often when phone was recently restarted and camera wasn't in use until this app) this code ends with error 100 Camera server died. If camera took successfully picture before it usually works.
I was debugging it for great amount of time and I found out it works when I comment out lines with torch. I can see torch working in both cases when taking pictures works or not.
Code of torchOn is following:
if(mCamera != null){
mCamera.stopPreview();
Camera.Parameters p = mCamera.getParameters();
List<String> supported = p.getSupportedFlashModes();
if (supported.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(p);
mCamera.startPreview();
}
Is there any reason why taking picture could not work because of torch? I observed it happend on Motorola Razr and Samsung Galaxy SIII.
I installed on my device two version of this app (with different name and so on). And I do following:
Restart device
Tried app-with-torch
If app-with-torch does work back in point 1.
Tried app-without-torch
Tried app-with-torch
And the results are followings:
App-without-torch works always
App-with-torch in about 80% of tries doesn't work at point 2. (after restart)
App-with-torch works always at point 5. (after app-without-torch was used)
My app start working even if I add torchOff() just before taking picture.
We could call this the " Monty Python Dead Parrot Log.d answer " :-P I wish I had a solution for you - do have some suggestions though. Heisenbugs are difficult to catch.
Does the torch have an isOn() test ?
Similarly (I don't recall), does the camera have an isReady() test ?
Can you tell from the logs if the camera dies before, during, or after mCamera.torchOn() or .torchOff() ?
What would happen if you stretch out the time span between calls ? This wouldn't be useable for the real app, but might help you monitor and catch what's happening. Say something like this in pseudo code:
try {
// Log.d ("cam", "here 1") ;
mCamera = configAndInitialize();
// Log.d ("cam", "here 2");
if ( mCamera.isReady() ) { // or isConfigured / initialized
// Log.d ("cam", "here 2");
Camera.startPreview();
// Log.d ("cam", "here 2");
thisThread.setDelay (200); // millisecs. try even up to 2000 ms !
// Log.d ("cam", "here 4");
mCamera.torchOn();
// Log.d ("cam", "here 5");
thisThread.setDelay (200); // again up to 2000 ms
// Log.d ("cam", "here 6");
}
} catch (Exception e) {
Log.d ("oops!", e.toString() );
}
The other thing to monitor, or make sure of, is that the camera and torch really are ready before that onClick can fire. Sprinkle some delays like the Log.d's all around and see. It might need a call back (mCamera.isReady() then enable onClick ).
Another thing to do is see if you can dig up the camera's source code (or the torch's) and GREP for error 100 - or is that a generic android 100 ?
I'm sure you're well aware of how much stuff happens when that camera gets fired up - seems like hundreds of calls. Since some of these low level items are async (cam is hardware after all), I suspect you're getting a NPE or a insufficiently initialized object. Not all NPE's etc get trapped, so it might just die on one that would not be there if delayed or syncronized sequences are used.
( HTH - I feel your pain, ari, I had to do a lot of camera stuff recently. Debugging on Samsung SIII is prohibitively time consuming. )
[EDIT] you've probably already found this link, but just in case:
How to turn on camera flash light programmatically in Android?
I think this is related to each OEM's implementation of the Android camera HAL (Hardware Abstraction Layer). This problem has also happened to me, I'm not sure but I suspect most camera HALs' torch mode works only in video capture, as that's where it's most used anyways. Try recording a video with the torch on to check it out.
One possible workaround would be to set your camera's flash mode to FLASH_MODE_ON just before taking the picture and then returning to FLASH_MODE_TORCH after the pic is taken if you need it again.

Can Android camera intent specify pictureSize?

From my current search, Android camera intent can not specify pictureSize.
Since my app needs to be fast, I do not want to save a large size picture in sd card, then load it to a small size of Bitmap. I think it takes time. Plus, I need a gray scale image, rather than a color Bitmap. I know how to convert them, but again it takes time.
I plan to take a picture at a specified size, and directly process the Y part (gray scale) in the YUV data in the memory.
So does that mean I have to write my own camera app using camera API?
Are there any good examples?
Many examples I checked so far often do not consider autofocus.
I add features in XML file:
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
I add the auto focus mode to the camera parameter.
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
But it does not work.
So I add autofocus command immediately before camera press button.
preview.camera.autoFocus(myAutoFocusCallback);
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
But the autofocus takes some time, and until the preview becomes clear, the take pictures has been performed.
Plus, I also want it to autofocus even if I do not press the camera button.
How can I put autofocus nicely in it?
Are there any good examples?
Thanks.
The Android Developers Guide has a tutorial on making a camera app: http://developer.android.com/guide/topics/media/camera.html#custom-camera
You don't need to add both the camera and the camera.autofocus features in the manifest. The latter implies the first - though it's not really a problem.
FOCUS_MODE_AUTO does not mean it the cameras will focus continuously, just that it will use autofocus at some point (instead of manual focus) by a callback function. You'll need FOCUS_MODE_CONTINUOUS_PICTURE if you want the camera focusing by itself. It's explained in the documentation.
As for taking pictures before the camera is focused: try calling takePicture() from inside your autoFocusCallback like this:
private AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback() {
#Override
public void onAutoFocus(boolean success, Camera camera) {
if (success) {
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
}
};

Android 4 - Camera White Balancing stops after autoFocus

In my current application, I've got a class holding an instance of a Camera object and trying to do the following:
1) Wait for a specified time, e.g. nothing (this is done via a TimerTask)
2) Request to focus via autoFocus
3) In autoFocus callback, request OneShotPreviewCallback
4) In preview callback, save image
5) Repeat
While the white balancing is working fine prior to the first autoFocus, it stops after the first focussing has been done. Well, of course I looked up the API, and there is one interesting statement in the autoFocus description.
But auto-focus routine may stop auto-exposure and auto-white balance transiently during focusing.
But it seems it is non stopped only transiently, but permantly. Funny enough, with the subsequent call of autoFocus, the camera tries to ajust the whitening again, but the correct value is mostly only with the second or third autoFocus.
I also tried to set the white balancing in code, but it didn't change anything.
setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
Does anyone else know this issue, or am I missing some point ? I know that I could permanently call autoFocus to force the white balancing, but that doesn't seem the optimal way for me, because prior to the first call auf autoFocus, it works perfectly fine.
P.S.: I'm testing on a Samsung Galaxy S2 with Android 4.0.3.
I have ran into similar issue on Samsung Galaxy 2 Duos 2. In this case, the auto exposure settings have stopped working instead of the WB. I tried to cycle (on/off) the auto exposure param and it worked for me.
mCamera.autoFocus(new Camera.AutoFocusCallback() {
#Override
public void onAutoFocus(boolean b, Camera camera) {
Camera.Parameters params = camera.getParameters();
if (params.isAutoExposureLockSupported()) {
params.setAutoExposureLock(true);
camera.setParameters(params);
params = camera.getParameters();
params.setAutoExposureLock(false);
camera.setParameters(params);
}
}
});
I've got the similar problem on Samsung Galaxy Ace - after first autofocus, camera white balancing turns off and does not turn on again, no matter how much I do autofocus after.
As there are no API methods to tell camera to resume white balancing, and resetting the camera parameters in autofocus callback doesn't do the trick, my guess is that it is a bug in camera driver in Samsung phones - I've tried my application with different phones and only on this Samsung Galaxy Ace (GT-S5830; updated to Android 2.3.3), camera white balancing does not resume after autofocusing.
Maybe we should issue a bug ticket on developer.samsung.com?
It seems that
mCamera.stopPreview();
mCamera.startPreview();
in AutoFocusCallback can enable auto exposure again, but bringing a very short pause on the preview as side effect.

SurfaceView getting black when calling Camera.stopPreview()

I am doing some image processing of previews captured by the camera. It is cpu consuming task and I have to stop preview to make it faster. Before new frame is being processed I am calling Camera.stopPreview() and after Camera.startPreview().
However, I would like to have last captured frame displayed on SurfaceView after stopping the preview. It works 'out of the box' on 2.3 devices, however, SurfaceView gets black after calling Camera.stopPreview() on older versions of SDK. Does anyone know what has changed and what to do?
Yes, this was an improvement of the 2.3.
I had this problem in 2.2 as well, there was no way to work on a preview image despite the fact this was theoretically possible according to the API. To solve this I had to actually take a picture using Camera.takePicture(null, null, Camera.PictureCallback myCallback) (see info here) and then to implement a callback to handle the taken picture. The instance of the class that implements this callback is actually the parameter to pass to Camera.takePicture() and the callback method itself looks like this:
public void onPictureTaken(byte[] JPEGData, Camera camera) {
final Bitmap bitmap = createBitmapFromView(JPEGData);
// do something with the Bitmap
}
Doing that way prevents the picture to be saved on the external storage with the regular pictures taken with the camera application. Should you need to serialize the Bitmap you'll have to do it explicitely. But it doesn't prevent the camera's trigger sound from being emitted.
Camera.takePicture() has to be called wile the preview is running. stopPreview() can be called right after.
One thing to be careful with /!\:
Camera.takePicture() is not reentrant (at all). The callback must have returned before any subsequent call of Camera.takePicture(). This was freezing my phone, I had to shutdown and restart it before it to be usable again. As the action was triggered by a button, on my side, I had to shield it with a boolean:
if (!mPictureTaken) {
mPictureTaken = true; // absolutely NOT reentrant. Any double click sticks the phone otherwise.
mCameraView.takePicture(callback);
}

Categories

Resources