I need to upload photo from camera to server in my app. It works great on most of the devices. But a few devices are causing image to rotate by 90' which is not my desired behavior. After research, I came to know that its due to EXIF data attached with image. To strip EXIF data from the image bitmap, I tried various things like re-sizing image etc but none of them worked for me. Anybody please suggest a way to perform this task.
Try this,
public static Bitmap getImage(Context context, Uri uri)
throws FileNotFoundException, IOException {
InputStream input = context.getContentResolver().openInputStream(uri);
BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
onlyBoundsOptions.inJustDecodeBounds = true;
onlyBoundsOptions.inDither = true;// optional
onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
input.close();
if ((onlyBoundsOptions.outWidth == -1)
|| (onlyBoundsOptions.outHeight == -1))
return null;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inDither = true;
bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
input = context.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
input.close();
ExifInterface ei = new ExifInterface(uri.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
bitmap = rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
bitmap = rotateImage(bitmap, 180);
break;
}
return bitmap;
}
here uri is the uri of image taken from camera.
For understanding exif orienatation: goto http://www.impulseadventure.com/photo/exif-orientation.html
Here we use the ExifInterface object to read tags in a JPEG file and fetch the orientation attribute of image e.g.
// Variable to store the corrected bitmap.
Bitmap correctedBitMap = null;
ExifInterface exifInterface = new ExifInterface(<PATH OF YOUR PHOTO>);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Now the integer variable orientation has the info about how much is the image rotated by, which is then checked against ExifInterface constant value & respectively the changes are made.
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
correctedBitMap = rotateImage(<YOUR BITMAP OBJECT>, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
correctedBitMap = rotateImage(<YOUR BITMAP OBJECT>, 180);
break;
}
Here is the code for rotateImage method:
private Bitmap rotateImage(Bitmap source, float angle) {
Bitmap bitmap = null;
Matrix matrix = new Matrix();
matrix.postRotate(angle);
try {
bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
return bitmap;
}
Related
I have created a custom camera. When I click on the capture button in the application, image has been taken. Moreover, I am getting the data in the form of byte array in the function named as onPictureTaken.
I am converting the byte array into the bitmap using the library known as Glide.
My problem is that in Samsung device the images rotates itself. I have been researching on it for quite a while. I found the library called as metadata extraction library to get the Exif information from byte[] and rotate the image on it but it is not working on the Samsung devices. The metadata extraction library every time returns a value of 1 for portrait image which shows that image does not need rotation however, the image taken in portrait mode is always 90 degree rotated.
Whenever, the photo is taken in portrait mode it is rotated at an angle of 90 degrees for both front and back camera and meta extraction library shows a value of 1.
Is there something other then metadata extraction extraction library which extract Exif information stream data?
Note: I cannot use ExifInterface because it requires the minimum Api level of 24 whereas, I am testing on API level 22
I have tried many solution but nothing is working. Is there any solution for this?
The code is given below:
public void onPictureTaken(byte[] data, Camera camera) {
mCamera.stopPreview();
Glide.with(this).load(data)
.asBitmap().centerCrop().animate(R.anim.abc_fade_in)
.into(new SimpleTarget<Bitmap>(width, height) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
camera_view.setVisibility(View.INVISIBLE);
int w = resource.getWidth();
int h = resource.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
try {
InputStream is = new ByteArrayInputStream(data);
Metadata metadata = ImageMetadataReader.readMetadata(is);
final ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
if (exifIFD0Directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
final int exifOrientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
switch (exifOrientation) {
case 6:
mtx.postRotate(90);
break; // top left
case 3:
mtx.postRotate(180);;
break; // top right
case 8:
mtx.postRotate(270);
break; // bottom right
}
photo = Bitmap.createBitmap(resource, 0, 0, w, h, mtx, true);
/* Work on exifOrientation */
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am using Samsung J5 for the testing.
You don't need a library for this. Here is a couple methods that I wrote that should do the trick for you.
public static int getCapturedImageOrientation(Context context, Uri imageUri){
int rotate = 0;
try {
context.getContentResolver().notifyChange(imageUri, null);
File imageFile = new File(imageUri.getPath());
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;
}
Log.i("RotateImage", "Exif orientation: " + orientation);
Log.i("RotateImage", "Rotate value: " + rotate);
} catch (Exception e) {
Log.e(TAG, "Error getting rotation of image");
}
return rotate;
}
public static int GetRotateAngle(Context context, Uri imageUri) {
String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION };
Cursor cursor = context.getContentResolver().query(imageUri, columns, null, null, null);
if (cursor == null) {
//If null, it is not in the gallery, so may be temporary image
return getCapturedImageOrientation(context, imageUri);
}
cursor.moveToFirst();
int orientationColumnIndex = cursor.getColumnIndex(columns[1]);
int orientation = cursor.getInt(orientationColumnIndex);
cursor.close();
return orientation;
}
I wrap these in a class called ImageHelper. You can use it like this:
rotateImage(ImageHelper.GetRotateAngle(Context, mCropImageUri));
Then of course the rotateImage code would be:
private void rotateImage(int degrees) {
Matrix mat = new Matrix();
mat.postRotate(degrees);
mCropImage = Bitmap.createBitmap(mCropImage, 0, 0, mCropImage.getWidth(), mCropImage.getHeight(), mat, true);
setImageForCropping(mCropImage);
}
Of course i was doing all this for a photo editing, cropping and scaling app, so you can ignore some of the extras, but this should take care of ya. Goodluck.
Almost in all Samsung Devices the image rotation Issue is common ,in my case i am using Samsung Note 3 and this same issue occurs but i am using below code to solve this issue
public static Bitmap decodeFile(String path) { // this method is for avoiding the image rotation
int orientation;
try {
if (path == null) {
return null;
}
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 4;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bm = BitmapFactory.decodeFile(path, o2);
Bitmap bitmap = bm;
ExifInterface exif = new ExifInterface(path);
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Log.e("orientation", "" + orientation);
Matrix m = new Matrix();
if ((orientation == 3)) {
m.postRotate(180);
m.postScale((float) bm.getWidth(), (float) bm.getHeight());
// if(m.preRotate(90)){
Log.e("in orientation", "" + orientation);
bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
return bitmap;
} else if (orientation == 6) {
m.postRotate(90);
Log.e("in orientation", "" + orientation);
bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
return bitmap;
} else if (orientation == 8) {
m.postRotate(270);
Log.e("in orientation", "" + orientation);
bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
return bitmap;
}
return bitmap;
} catch (Exception e) {
}
return null;
}
This code is work for me so i hope this will helpful for you
This can be easily fixed by using ExifInterface provided by Google.
You can add it to your project as follows:
implementation "androidx.exifinterface:exifinterface:1.1.0"
After this, get the rotation from your image and apply it to your ImageView:
// uri of the image
val inputStream = contentResolver.openInputStream(Uri.parse(uri))
val exifInterface = ExifInterface(requireNotNull(inputStream))
var rotation = 0
when (exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)) {
ExifInterface.ORIENTATION_ROTATE_90 -> rotation = 90
ExifInterface.ORIENTATION_ROTATE_180 -> rotation = 180
ExifInterface.ORIENTATION_ROTATE_270 -> rotation = 270
}
My image rotate when I use camera Intent . After capturing picture my I send this picture to another activity by using Intent and set this pic as background of Layout but capturing picture in landscape rotate the picture.
You can solve this problem by using the ExitInterface shown below:
private void setPic(Uri contentUri) throws IOException {
// Get the dimensions of the View
int targetW = uploadedImage.getWidth();
int targetH = uploadedImage.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = calculateInSampleSize(bmOptions, targetW, targetH);
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
bitmap = rotateImageIfRequired(bitmap, contentUri);
bitmap = ThumbnailUtils.extractThumbnail(bitmap, 750, 750);
uploadedImage.setImageBitmap(bitmap);
}
This function is where the bitmap is being decoded based on the path in the phone, it is then calling the rotateImageIfRequired() which will determine which way the image should rotate before it is set to the background of your layout. In this scenario uploadedImage is your layout.
private Bitmap rotateImageIfRequired(Bitmap img, Uri contentUri) throws IOException {
ExifInterface ei = new ExifInterface(contentUri.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotateImage(img, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotateImage(img, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotateImage(img, 270);
default:
return img;
}
}
This function will determine which orientation the image is after it has been decoded and before it has been set.
private static Bitmap rotateImage(Bitmap img, int degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
img.recycle();
return rotatedImg;
}
This function rotates the image and returns the new image.
Hope this helps.
I have an app that the client has asked to add rotation support.
Below is the current code that takes the image from the camera.
camera.takePicture(null, null, new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
if (data != null) {
Matrix mtx = new Matrix();
mtx.postRotate(180);
float density = Resources.getSystem().getDisplayMetrics().density;
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, (data != null) ? data.length : 0);
Bitmap scaled = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), mtx, true);
bm = Bitmap.createScaledBitmap(scaled, (int)(480*density), (int)(320*density), true); //height and width are backwards because its portrait
previewImage(bm);
}
}
});
What I am trying to work out is the quickest way to work out what orientation the captured bitmap should be in, i.e when the user takes a picture with the device in landscape we need to flip:
Bitmap.createScaledBitmap(scaled, (int)(480*density), (int)(320*density), true);
to:
Bitmap.createScaledBitmap(scaled, (int)(320*density), (int)(480*density), true);
and also change the mtx.postRotate
My issue is working out the best way to find out what rotation the image was taken in.
Try this
private Bitmap checkForRotation(String filename, Bitmap bitmap) {
Bitmap myBitmap = bitmap;
ExifInterface ei = null;
try {
ei = new ExifInterface(filename);
new ExifInterface(filename);
} catch (IOException e) {
e.printStackTrace();
}
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
//Here you get the orientation and do what ever you want to do with it as i am rotating the image
case ExifInterface.ORIENTATION_ROTATE_90:
myBitmap = rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
myBitmap = rotateImage(bitmap, 180);
break;
}
return myBitmap;
}
hey every one I am Trying to Load Large Images to BackGround of my App Using This Code :
final Drawable drawable =new BitmapDrawable(colorResource,decodeFile(new File(LMApplication.sharedpreferences.getString(path,""))));
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
if(LMApplication.CurrentSDK < android.os.Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(drawable);
}else{
view.setBackground(drawable);
}
}
});
public Bitmap decodeFile(File input){
Bitmap bmpCompressed = null;
try {
//Decode image size
BitmapFactory.Options o11 = new BitmapFactory.Options();
o11.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(input),null,o11);
//The new size we want to scale to
final int REQUIRED_SIZE=500;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o11.outWidth/scale/2>=REQUIRED_SIZE && o11.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
//Decode with inSampleSize
BitmapFactory.Options o22 = new BitmapFactory.Options();
o22.inSampleSize=scale;
BitmapFactory.decodeStream(new FileInputStream(input), null, o22);
bmpCompressed = BitmapFactory.decodeFile(input.toString(), o22);
// FileOutputStream out = null;
try {
// out = new FileOutputStream(file);
// bmpCompressed.compress(CompressFormat.JPEG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try{
// out.close();
} catch(Throwable ignore) {}
}
} catch (FileNotFoundException e) {}
return bmpCompressed;
}
But Some Images are Rotated 90 degree to the left after the decoding, is there any Problem here or is there any way i can prevent this?
UPDATE :
Changed the Code like This and now prepared for every thing :
public Bitmap decodeFile(File input){
Bitmap bmpCompressed = null;
try {
//Decode image size
BitmapFactory.Options o11 = new BitmapFactory.Options();
o11.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(input),null,o11);
//The new size we want to scale to
// final int REQUIRED_SIZE=500;
int width = getDimensions.WIDTH;
int height = getDimensions.HEIGHT;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o11.outWidth/scale/2>=width && o11.outHeight/scale/2>=height)
scale*=2;
//Decode with inSampleSize
BitmapFactory.Options o22 = new BitmapFactory.Options();
o22.inSampleSize=scale;
BitmapFactory.decodeStream(new FileInputStream(input), null, o22);
bmpCompressed = BitmapFactory.decodeFile(input.toString(), o22);
ExifInterface exif = new ExifInterface(input.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
if(orientation != ExifInterface.ORIENTATION_NORMAL){
int rotatesize;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatesize = -90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatesize = -180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatesize = -270;
break;
default:
break;
}
Matrix matrix = new Matrix();
matrix.postRotate(90);
bmpCompressed = Bitmap.createBitmap(bmpCompressed, 0, 0, bmpCompressed.getWidth(), bmpCompressed.getHeight(), matrix, true);
}
// FileOutputStream out = null;
try {
// out = new FileOutputStream(file);
// bmpCompressed.compress(CompressFormat.JPEG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try{
// out.close();
} catch(Throwable ignore) {}
}
} catch (IOException e) {}
return bmpCompressed;
}
Use ExifInterface for rotate like this
picturePath = getIntent().getStringExtra("path");
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
ExifInterface exif = new ExifInterface(picturePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.postRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.postRotate(270);
break;
default:
break;
}
myImageView.setImageBitmap(bitmap);
bitmap.recycle();
Note : Here picturePath is selected Image's path from Gallery
I'm trying to rotate the image taken from camera and save it without scaling down without showing in an view, but when I try to load it like this
Bitmap bmp = BitmapFactory.decodeFile(tmpImageFilePath);
I get OutOfMemoryError. There is no problem for me to rotate image but how to load it and save with original size? I've already googled some techniques in order to solve this problem but none of them fits me.
And when I try this variant:
File f = new File(tmpImageFilePath);
long filesize = f.length();
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}
Matrix mat = new Matrix();
mat.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bmp = BitmapFactory.decodeFile(tmpImageFilePath, options);
Bitmap correctBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
bmp.recycle();
try {
FileOutputStream out = new FileOutputStream(f);
correctBmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
I get lesser image.