Rotating a bmp and displaying it right-side-up
-
25-09-2019 - |
Question
I have an app that snaps a picture and displays the picture on an Imageview. The problem is, I can only snap a pic in landscape mode in order for the bmp to be displayed right side up - Is there a way I can rotate it to right side up if the pic is taken in portrait mode/
Thanks!
Here is the code I use to place the image in the img view -
private void processCameraImage(Intent intent) {
setContentView(R.layout.detectlayout);
((Button) findViewById(R.id.detect_face)).setOnClickListener(btnClick);
ImageView imageView = (ImageView) findViewById(R.id.image_view);
cameraBitmap = (Bitmap) intent.getExtras().get("data");
imageView.setImageBitmap(cameraBitmap);
There is a detect faces button that will detect if there are faces present.
Solution
This is how you would rotate a bmp:
private Bitmap rotateImage(Bitmap b, float angle)
{
//create a new empty bitmap to hold rotated image
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(returnBitmap);
//move rotation point to center of image
g.TranslateTransform((float)b.Width/2,(float)b.Height / 2);
//rotate
g.RotateTransform(angle);
//move image back
g.TranslateTransform(-(float)b.Width/2,-(float)b.Height / 2);
}
OTHER TIPS
Here is the code I used onActivityResult() in my activity. The intent returned was for picking an image of the type image/*. Works well for me!
Uri imageUri = intent.getData();
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = managedQuery(imageUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow