Reputation: 83
I'm facing an issue where I want to ensure the image is positioned exactly like Image No. 1 (F):
However, when selecting an image from the phone gallery, the image might appear in any orientation or position. How can I rotate and flip the image to match the orientation of Image No. 1 (F)?
Also the same issue is happening when I take image from the camera itself , so it's not only specific to exif data
Here's my current code:
private Bitmap getStoredImage() {
SharedPreferences sharedPreferences = requireContext().getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
String imagePath = sharedPreferences.getString("front_face_image", null);
if (imagePath != null) {
File imgFile = new File(imagePath);
if (imgFile.exists()) {
return BitmapFactory.decodeFile(imgFile.getAbsolutePath());
}
}
return null; // Return null if the image path is not found or the file does not exist
}
Upvotes: 0
Views: 52
Reputation: 48
Try this, Use this code to rotate your bitmap in specific orientation. You will get rotated bitmap in result. You need to pass imagePath (path of image to be rotated) and bitmap to this method.
public int getCameraPhotoOrientation(String imagePath, Bitmap bmp){
int rotate = 0;
try {
ExifInterface exif = new ExifInterface(imagePath);
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;
}
Matrix mat = new Matrix();
mat.postRotate((rotate)); //degree how much you rotate i rotate 270
Bitmap bMapRotate=Bitmap.createBitmap(bmp, 0,0,bmp.getWidth(),bmp.getHeight(), mat, true);
File myDir = new File(getContext().getFilesDir() + "/user_image");
if(!myDir.exists()){
myDir.mkdirs();
}
String fname = "IMG_SEARCH.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bMapRotate.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}catch (Exception e){
e.printStackTrace();
}
Log.i("RotateImage", "Exif orientation: " + orientation);
Log.i("RotateImage", "Rotate value: " + rotate);
} catch (Exception e) {
e.printStackTrace();
}
return rotate;
}
Upvotes: 0