I'm using Exif interface to get the desired orientation of the image but the bitmap I get is just a part of the original picture zoomed out to fit the imageView.How do I get the whole picture(Image Resolution - 2048 x 1152)?
bitmap1 = BitmapFactory.decodeFile(selectedImagePath);
int rotate = 0;
try {
ExifInterface exif = new ExifInterface(selectedImagePath);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
bitmap1 = Bitmap.createBitmap(bitmap1, 0, 0, bitmap1.getWidth(),bitmap1.getHeight(), matrix, true);
Related
I captured image from camera and display image into imageview. It will display perfect but the captured image rotated 90 degrees automatically in some devices. I searched a lot about it but could not get proper solution. Please tell me the solution for it. Thanks in advance.
try this code
public Bitmap adjustImageOrientation(Bitmap image, File f) {
int rotate = 0;
try {
ExifInterface exif = new ExifInterface(f.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_NORMAL:
rotate = 0;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
if (rotate != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
image = Bitmap.createBitmap(image, 0, 0, image.getWidth(),
image.getHeight(), matrix, true);
}
return image;
}
Use this method to determine image rotation.
public int getCameraPhotoOrientation(Uri imageUri, String imagePath) {
int rotate = 0;
try {
_context.getContentResolver().notifyChange(imageUri, null);
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (Exception e) {
}
return rotate;
}
And if the method wouldn't return Zero rotated the image.
int degree=getCameraPhotoOrientation(Uri.fromFile(tempFile), fPath);
if (degree!= 0) {
bitmap = tools.rotateOrientationCall(bitmap, degree);
}
Rotate your bitmap.
public Bitmap rotateOrientationCall(Bitmap src, float degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}
In my app, the image is captured from the camera and then it displays in a ImageView I've done this approach successfully, but when my image is displayed in Imageview, Image display after rotate. I want to rotate Image and then Display in ImageView. When I click image from front camera then image is display proper with the help of bellow code
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotate = Bitmap.createBitmap(scale,0,0,scale.getWidth(),scale.getHeight(),matrix,true);
displayImage.setImageBitmap(rotate);
But when I click from back camera Image will display opposite to front camera.
One more thing when I click image from camera from different angle then image will display in ImageView in different angle.
I use following code in my application.
It works for me...!!!
File mediaFile = new File(mediaPath);
Bitmap bitmap;
if (mediaFile.exists()) {
if (isImage(mediaPath)) {
Bitmap myBitmap = BitmapFactory.decodeFile(mediaFile.getAbsolutePath());
int height = (myBitmap.getHeight() * 512 / myBitmap.getWidth());
Bitmap scale = Bitmap.createScaledBitmap(myBitmap, 512, height, true);
int rotate = 0;
try {
exif = new ExifInterface(mediaFile.getAbsolutePath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
rotate = 0;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
Bitmap rotateBitmap = Bitmap.createBitmap(scale, 0, 0, scale.getWidth(),
scale.getHeight(), matrix, true);
displayImage.setImageBitmap(rotateBitmap);
}
}
public static boolean isImage(String str) {
boolean temp = false;
String[] arr = { ".jpeg", ".jpg", ".png", ".bmp", ".gif" };
for (int i = 0; i < arr.length; i++) {
temp = str.endsWith(arr[i]);
if (temp) {
break;
}
}
return temp;
}
I am working on small app where i am re sizing bitmap using Bitmap.createScaledBitmap(). I am using code below to get the rotation angle of image clicked and based on the rotation angle i am rotating and re-sizing image to. This works very well without any issue in samsung galaxy s4(Android 4.3 Jellybean) but same code is not working in Motorola Moto E(Android 4.4.4): it is not returning angle so i am not able to resize image properly.
Please guide me for this issue..How can i handle this on various devices.Thanks in advance
public int getCameraPhotoOrientation(){
int rotate = 0;
try {
this.getContentResolver().notifyChange(Uri.fromFile(outputImage), null);
ExifInterface exif = new ExifInterface(outputImage.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
// Log.i("RotateImage", "Exif orientation: " + orientation);
// Log.i("RotateImage", "Rotate value: " + rotate);
utilitycls.showCustomToastMessage(this,""+orientation,0);
} catch (Exception e) {
e.printStackTrace();
}
return rotate;}
As you suggested i have used ExifInterface.ORIENTATION_NORMAL in switch.My motorola device returns -1 Oreintation .based on the orienation i am setting width and hieght.It is working for both S4 and motorola.But still i am clueless for other devices.
rotate= getCameraPhotoOrientation(); //getOrientation(this);//
// utilitycls.showCustomToastMessage(this,"Rotation::"+rotate,1);
if(rotate == 0 || rotate == 180)
{
finalW=480;
finalH=270;
// resizedBitmap = Bitmap.createScaledBitmap(bmp, 480,270, false);
}
else if(rotate == 90 || rotate == 270)
{
bmp=RotateBitmap(bmp, rotate);
finalW=270;
finalH=480;
}
else
{
if(bitmapW >= bitmapH)
{
finalW=480;
finalH=270;
}
else
{
finalW=270;
finalH=480;
}
}
resizedBitmap = Bitmap.createScaledBitmap(bmp, finalW,finalH, false);
public int getCameraPhotoOrientation(){
int rotate = 0;
try {
this.getContentResolver().notifyChange(Uri.fromFile(outputImage), null);
// File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(outputImage.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_NORMAL:
rotate = 0;
break;
case 0:
rotate = -1;
break;
}
// Log.i("RotateImage", "Exif orientation: " + orientation);
// Log.i("RotateImage", "Rotate value: " + rotate);
utilitycls.showCustomToastMessage(this,""+orientation,0);
} catch (Exception e) {
e.printStackTrace();
}
return rotate;
}
I was facing a problem image capture from camera it was showing image in landscape even though i capture that image in portrait then i found code online. But i think there is some problem with this code it sometimes didn't work (show image in landscape). So what's wrong there
private void adjustImageOrientation()
{
ExifInterface exif;
int rotate = 0;
try
{
exif = new ExifInterface(filepath);
int exifOrientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (exifOrientation)
{
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
}
if (rotate != 0)
{
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// Setting pre rotate
Matrix mtx = new Matrix();
mtx.preRotate(rotate);
// Rotating Bitmap & convert to ARGB_8888, required by tess
try
{
bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
}
catch(OutOfMemoryError e)
{
e.printStackTrace();
//image = Bitmap.createBitmap(image, 0, 0, w/2, h/2, mtx, false);
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
// return image.copy(Bitmap.Config.ARGB_8888, true);
}
Capture by My Camera App and Open image by Galerry:
My code open:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(dt.FileName);
intent.setDataAndType(Uri.fromFile(file), "image/*");
startActivity(intent);
Capture by Standard Camera and Open image by Galerry:
I check Original Image is the same.
How rotate image when open image by Gallery the same Standard Camera?
Use this code to change the image orientation
ExifInterface exif = new ExifInterface(filepath);
int exifOrientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch (exifOrientation)
{
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
}
if (rotate != 0)
{
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.preRotate(rotate);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
}