This is my code that I use to save the image to a Bitmap
. This piece of code is based on the code from CyanogenMod\'s camera app so I would assume it would work
Try this
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
imv.setImageBitmap(bmp);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 70, bos);
Did you try to put null at your Options? Try that:
@Override
public void onPictureTaken(final byte[] data, Camera camera) {
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length, null);
try {
FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/image.jpg");
bm.compress(Bitmap.CompressFormat.JPEG, 95, out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I would suggest not setting the picture size in code and let the camera return the best picture it can capture. Check the code given in the answer here. https://stackoverflow.com/a/9745780/2107118
The height and width aren't supported for that camera. That's why it looks like that and why the 1280x720 setting works. It's the same as if you set your starcraft to run at a resolution not supported by your monitor.
Are you setting the parameters from the back camera to the front camera? Another thing to test is for the front camera, first try not setting any parameter, just use the default settings to see if it will work. I ran into similar problems on Samsung devices but that was for back camera. Also you may need to rotate the images taken with front camera(for Samsung devices back camera needs that too)
public static Bitmap rotate(Bitmap bitmap, int degree) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(degree);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
I should probably also mention that saving the data directly using a FileOutputStream was creating a proper JPEG image. So the problem must be with the BitmapFactory and the way I create the Bitmap.
Camera outputs fully processed JPEG data (hopefully), and you can write it to disk. Now what you should try:
BitmapFactory
process the original file and file saved by Image editor, see if you get a bad bitmap in both. If image is valid JPEG and BitmapFactory
still produces wrong Bitmap, the problem is with BitmapFactory
(less likely), if file output by Camera is invalid JPEG, problem is with Camera (more likely).