The image was rotated after decreasing the bitmap size
My app takes a photo and then uploads it. Since the photo I will take will be large, I have to reduce the size and then upload it. So, I used the following code to reduce the size of the image captured by the camera, which is stored somewhere on the SD card. But I don't know what is wrong with the code, the image is rotated counterclockwise by 90 degrees, if it's a portrait it's even better if the image is done in landscape mode.
Below is the code I used to reduce the size.
public File getReducedImage(File mediaFile){
Bitmap b = decodeFile(mediaFile);
return getfileFromBitmap(b, mediaFile.getPath());
}
private File getfileFromBitmap(Bitmap b, String path) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(path);
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
Log.v(TAG, "returned");
return f;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v(TAG, "Exception caught");
return null;
}
//write the bytes in file
}
private Bitmap decodeFile(File f){
final int IMAGE_MAX_SIZE=400;
Bitmap b = null;
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE /
(double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
Log.v(TAG, "error in bitmap conversion");
e.printStackTrace();
}
return b;
}
Edit: Actually the problem was that the image lost its orientation information after changing it. I solved this by rotating accordingly. The following is my solution. Hope this helps someone.
public getReducedImage (mediaFile) {
Bitmap b = decodeFileWithRotationIfNecessary(mediaFile);
File f = getfileFromBitmap(b, mediaFile.getPath());
return f;
}
private File getfileFromBitmap(Bitmap b, String path) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
// you can create a new file name "test.jpg" in sdcard folder.
File f = new File(path);
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
return f;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v(TAG, "Exception caught");
return null;
}
// write the bytes in file
}
private Bitmap decodeFileWithRotationIfNecessary(File f) {
final int IMAGE_MAX_SIZE = 400;
Bitmap b = null;
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
scale = (int) Math.pow(
2,
(int) Math.round(Math.log(IMAGE_MAX_SIZE
/ (double) Math.max(o.outHeight, o.outWidth))
/ Math.log(0.5)));
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
Log.v(TAG, "error in bitmap conversion");
e.printStackTrace();
}
Bitmap bMapRotate = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
b.getHeight(), getMatrix(f), true);
return bMapRotate;
}
private Matrix getMatrix(File f) {
Matrix mat = new Matrix();
mat.postRotate(90);
try {
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, -1);
switch (orientation) {
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
Log.v(TAG, "flip horizontal");
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
Log.v(TAG, "flip vertical");
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Log.v(TAG, "rotate 180");
mat.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Log.v(TAG, "rotate 90");
break;
case ExifInterface.ORIENTATION_ROTATE_270:
Log.v(TAG, "rotate 270");
mat.postRotate(180);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
Log.v(TAG, "transpose");
break;
case ExifInterface.ORIENTATION_UNDEFINED:
Log.v(TAG, "undefined");
break;
case ExifInterface.ORIENTATION_NORMAL:
Log.v(TAG, "normal");
mat.postRotate(270);
break;
default:
Log.v(TAG, "default");
// mat.postRotate(0);
break;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v(TAG, "error in finding exif information");
}
return mat;
}
source to share
One of the reasons may be that you have not saved the EXIF data:
ExifInterface ei = new ExifInterface(<file-path>);
int o = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
//--decode -- encode save--
ExifInterface ei2 = new ExifInterface(<new-file-path>);
ei2.setAttribute(ExifInterface.TAG_ORIENTATION,o);
ei2.saveAttributes();
The default camera app will save with the correct orientation. If you are using your own code to accept / change the image, you must save the image with the correct orientation.
source to share