Android:I can't take a photo with the front facing camera on SurfaceView (Samsung Galaxy S GT-i9000 )
I get a green screen with the front camera from samsung galaxy s, but the preview is correct. With the Back-Camera can I make photos.
Where is the problem?
This is my PictureCallback:
public void onIvButtonShutterClick(View view) {
PictureCallback pictureCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
File picture = new File(My_Camera.this.getFilesDir()
+ "/bild.jpg");
FileOutputStream pictureOut = new FileOutputStream(picture);
pictureOut.write(data);
pictureOut.flush();
pictureOut.close();
Toast.makeText(
My_Camera.this,
getResources().getString(
R.string.tx_my_camera_save)
+ "\n" + picture.getAbsolutePath(),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
//mCamera.startPreview(); // Preview wird weiter ausgeführt
}
};
mCamera.takePicture(null, null, pictureCallback);
}
I access on the front camera with:
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
mParameters = mCamera.getParameters();
mParameters.set("camera-id", 2);
mParameters.setPictureFormat(PixelFormat.JPEG);
mCamera.setDisplayOrientation(270);
mCamera.s开发者_StackOverflowetParameters(mParameters);
mCamera.startPreview();
}
I had the same problem but never found a real solution.
What I ended up using was to grab frames from the camera preview. This has some issues – like, pictures are much more likely to be blurry.
The relevant method for grabbing preview frames is Camera.setOneShotPreviewCallback
.
I think the problem might be the processing of the result. I'm guessing you are sending the originally captured byte array containing the raw image data to a file with jpg extension, but the byte array is not in jpg data (i.e. missing headers and with uncompressed content). Here's a relevant piece from some code I have (the MeToo project that you saw), imageData being the raw content:
Bitmap backCameraImage = BitmapFactory.decodeByteArray(imageData,
0, imageData.length).copy(Bitmap.Config.RGB_565, true);
FileOutputStream out = null;
out = new FileOutputStream(file);
backCameraImage.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
Hope this helps...
Did you try to set this parameter:
mParameters.setPreviewFormat(PixelFormat.JPEG);
It helped me, but on Motorola Droid causing problems.
精彩评论