I have a simple piece of code here, but for some reason it is not storing the data. I'm sure it's something stupid, but I can't seem to figure it out. Any help would be greatly appreciated.
private byte[] picture;
public void takePicture(){
Camera camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.set("camera-id",2);
camera.setParameters(parameters);
parameters.set("gps-timestamp", "1233744883");
camera.setParameters(parameters);
Log.i("method", "in takePicture()");
camera.takePicture(null, rawCallback, null);
camera.release();
}
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Do something with the image RAW data.
Log.i("rawcallback", "in rawcallback");
picture = data;
}
};
When I run the code the Log in the call back is not being called, nor is "picture" saving "data". Any thoughts? I'm trying to take a picture with the front facing camera, could I need something special for that, which I'm not doing?
I also have these permissions
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:n开发者_如何学Pythoname="android.hardware.camera" />
The last parameter in Camera.takePicture()
corresponds to JPEG callback. So you should add the parameters.setPictureFormat(PixelFormat.JPEG);
line before camera.setParameters() call. Or use camera.takePicture(null, rawCallback, null);
instead.
You should also ensure you have the following lines in AndroidManifest.xml:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
From my comment:
The API for accessing font facing cameras only appeared in Android 2.3 (API version 9). In the earlier version you should use vendor specific techniques. Take a look at this working solution for Galaxy S:
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("camera-id", 2);
parameters.setPreviewSize(640, 480); // or (800,480) this is supported front camera preview size @ Samsung Galaxy S
mCamera.setParameters(parameters);
I hope it will help you. If not, try to play with setPreviewSize
parameters above.
I got a similar problem with the JPEG callback. It was not always called after taking a picture. This might be because you release the Camera too soon.
You should wait that all your callbacks are returned before releasing your Camera or exiting your app.
The Camera class is not thread-safe. Callbacks will be invoked on the event thread open(int)
was called from. (see the doc)
I managed to get it called more often by reducing the JPEG quality with the setJpegQuality()
function to get less data to compute. But a nicer way should be to wait until the callbacks are returned.
According to Android code documentation, you also need to pass a SurfaceHolder to the Camera and start a preview before taking a picture. For my purpose, I wanted to take a picture without any preview on the user screen. I used a local SurfaceView, but you can use another one.
I suggest something like :
Camera.PictureCallback myPictureCallback_jpeg = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
//save your data
}
};
SurfaceView mySurfaceView = new SurfaceView(this);
SurfaceHolder mySurfaceHolder = mySurfaceView.getHolder();
Camera myCamera = Camera.open();
try {
myCamera.setPreviewDisplay(mySurfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
Camera.Parameters p = myCamera.getParameters();
p.setJpegQuality(70);//a value between 1 and 100
myCamera.setParameters(p);
myCamera.startPreview();
myCamera.takePicture(null, null, null, myPictureCallback_jpeg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
myCamera.release();
精彩评论