How to rotate a bitmap 90 degrees

Android has a canvas.drawBitmap instruction canvas.drawBitmap(visiblePage, 0, 0, paint);

When I add canvas.rotate(90) , there is no effect. But if I write

 canvas.rotate(90) canvas.drawBitmap(visiblePage, 0, 0, paint); 

I do not get any bitmap. So what am I not doing right?

+104
android
Jan 26 '12 at 8:16
source share
9 answers

You can also try this.

 Matrix matrix = new Matrix(); matrix.postRotate(90); Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg, width, height, true); Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true); 

Then you can use the rotated image to set in your image through

 imageView.setImageBitmap(rotatedBitmap); 
+233
Feb 01 '13 at 11:27
source share
 public static Bitmap RotateBitmap(Bitmap source, float angle) { Matrix matrix = new Matrix(); matrix.postRotate(angle); return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); } 

Get bitmap from resources:

 Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img); 
+159
Apr 25 '13 at 16:03
source share

Short extension for Kotlin

 fun Bitmap.rotate(degrees: Float): Bitmap { val matrix = Matrix().apply { postRotate(degrees) } return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true) } 

And use:

 val rotatedBitmap = bitmap.rotate(90) 
+21
Feb 09 '18 at 22:46
source share

Below is the code to rotate or resize the image in android

 public class bitmaptest extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); LinearLayout linLayout = new LinearLayout(this); // load the origial BitMap (500 x 500 px) Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.android); int width = bitmapOrg.width(); int height = bitmapOrg.height(); int newWidth = 200; int newHeight = 200; // calculate the scale - in this case = 0.4f float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // createa matrix for the manipulation Matrix matrix = new Matrix(); // resize the bit map matrix.postScale(scaleWidth, scaleHeight); // rotate the Bitmap matrix.postRotate(45); // recreate the new Bitmap Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); // make a Drawable from Bitmap to allow to set the BitMap // to the ImageView, ImageButton or what ever BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); ImageView imageView = new ImageView(this); // set the Drawable on the ImageView imageView.setImageDrawable(bmd); // center the Image imageView.setScaleType(ScaleType.CENTER); // add ImageView to the Layout linLayout.addView(imageView, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT ) ); // set LinearLayout as ContentView setContentView(linLayout); } } 

You can also check this link: http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

+11
Jan 26 '12 at 8:24
source share

The default rotation point is the Canvas point (0,0), and I assume that you can rotate it in the center. I have done this:

 protected void renderImage(Canvas canvas) { Rect dest,drawRect ; drawRect = new Rect(0,0, mImage.getWidth(), mImage.getHeight()); dest = new Rect((int) (canvas.getWidth() / 2 - mImage.getWidth() * mImageResize / 2), // left (int) (canvas.getHeight()/ 2 - mImage.getHeight()* mImageResize / 2), // top (int) (canvas.getWidth() / 2 + mImage.getWidth() * mImageResize / 2), //right (int) (canvas.getWidth() / 2 + mImage.getHeight()* mImageResize / 2));// bottom if(!mRotate) { canvas.drawBitmap(mImage, drawRect, dest, null); } else { canvas.save(Canvas.MATRIX_SAVE_FLAG); //Saving the canvas and later restoring it so only this image will be rotated. canvas.rotate(90,canvas.getWidth() / 2, canvas.getHeight()/ 2); canvas.drawBitmap(mImage, drawRect, dest, null); canvas.restore(); } } 
+6
Mar 31 '15 at 13:13
source share

If you rotate the bitmap, 90 180 270 360 is fine, but for other degrees the canvas will draw a bitmap with a different size.

So the best way is

 canvas.rotate(degree,rotateCenterPoint.x,rotateCenterPoint.y); canvas.drawBitmap(...); canvas.rotate(-degree,rotateCenterPoint.x,rotateCenterPoint.y);//rotate back 
+1
Mar 17 '16 at 2:47
source share

I would further simplify the Kotlin comm1x extension function :

 fun Bitmap.rotate(degrees: Float) = Bitmap.createBitmap(this, 0, 0, width, height, Matrix().apply { postRotate(degrees) }, true) 
+1
Nov 29 '18 at 17:18
source share

If your goal is to rotate an image in an imageView or file, you can use Exif to do this. The support library now offers the following: https://android-developers.googleblog.com/2016/12/introduction-the-exifinterface-support-library.html

The following is its use, but to achieve your goal you should check the library API documentation for this. I just wanted to give a hint that rotating a bitmap is not always the best way.

 Uri uri; // the URI you've received from the other app InputStream in; try { in = getContentResolver().openInputStream(uri); ExifInterface exifInterface = new ExifInterface(in); // Now you can extract any Exif tag you want // Assuming the image is a JPEG or supported raw format } catch (IOException e) { // Handle any errors } finally { if (in != null) { try { in.close(); } catch (IOException ignored) {} } } int rotation = 0; int orientation = exifInterface.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: rotation = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: rotation = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: rotation = 270; break; } 

dependence

compile "com.android.support:exifinterface:25.1.0"

0
Mar 20 '18 at 13:35
source share

Just be careful with the raster type from the Java platform call, like from the answers of comm1x and Gnzlt , because it can return zero . I think this is also more flexible if the parameter can be any number and use the infix for readability, depending on your coding style.

 infix fun Bitmap.rotate(degrees: Number): Bitmap? { return Bitmap.createBitmap( this, 0, 0, width, height, Matrix().apply { postRotate(degrees.toFloat()) }, true ) } 

How to use?

 bitmap rotate 90 // or bitmap.rotate(90) 
0
Apr 02 '19 at 17:13
source share



All Articles