Reputation: 227
I am developing an app to do a lot of tasks related to images and PDF files. In one of the features I am converting images to PDF. I have encountered a few problems while handling images with bitmap.
1) When I am creating PDF file with images that I clicked using my phone's camera, for some reason they get rotated 90 degrees anti-clockwise automatically. When I am clicking the image, I used portrait mode, the image gets saved in my phone's gallery in portrait mode. When I view it in my phone's gallery it gets displayed correctly but when I load it in ImageView in Android Studio, it shows me a 90 degree anti-clockwise rotated image. I am using the code below to load images in ImageView:
File file = new File(imagez.get(position));
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageView.setImageBitmap(bitmap);
Here, "imagez" is an array that contains path to all images that are selected by the user in String format. e.g., "/storage/emualted/0/pics/...." like that. This problem is only with images that I clicked using my phone's camera, other images that I downloaded from internet or from WhatsApp or Facebook all works fine.
2) My second problem is that when I scroll the PDF that I created, the images there loads slowly. The PDF creation is completed, the images should be there all time instead they load every time as I scroll up and down as if I am using an Adapter View to inflate a List View where non-visible items gets destroyed as they move out of the view and gets loaded again when I scroll back up. I am using itextpdf class for converting images to PDF.
EDIT : My question is that I want the image to be displayed in the orientation it was clicked, and when I am creating the PDF, the image should be saved as original (it should not get rotated automatically). And I also a solution to make my created PDF load the pages quickly, if it can be done. Thank you.
Upvotes: 0
Views: 158
Reputation: 157
The problem that you are having is a common issue. I would suggest you to try the following method:
int orientation = 0;
ExifInterface exif = new ExifInterface(path_to_your_image);
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
if(orientation != 0)
{
Matrix rotateMatrix = new Matrix();
if (orientation == 6)
rotateMatrix.postRotate(90);
else if (orientation == 3)
rotateMatrix.postRotate(180);
else if (orientation == 8)
rotateMatrix.postRotate(270);
else
rotateMatrix.postRotate(0);
Bitmap newBitmap = Bitmap.createBitmap(previous_bitmap, 0, 0, previous_bitmap.getWidth(), previous_bitmap.getHeight(), rotateMatrix, false);
}
else
{
Bitmap newBitmap = Bitmap.createBitmap(previous_bitmap, 0, 0, previous_bitmap.getWidth(), previous_bitmap.getHeight);
}
//After this you can save your bitmap wherever you want
As far as your second problem is concerned I guess your images are too big. Try decreasing their size or dimensions to say 1200x1200 or whatever you feel right.
Hope it helps!
Upvotes: 1