Rotate the saved bitmap in android

I save an image from a camera that was in landscape mode. therefore it is saved in landscape mode, and then I impose an overlay on it, which is also in landscape mode. I want to rotate this image and then save it. for example if i have this

enter image description here

I want to rotate clockwise 90 degrees once and do it and save it to sdcard:

enter image description here

How to do it?

+6
source share
5 answers
void rotate(float x) { Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.tedd); int width = bitmapOrg.getWidth(); int height = bitmapOrg.getHeight(); int newWidth = 200; int newHeight = 200; // calculate the scale - in this case = 0.4f float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); matrix.postRotate(x); Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,width, height, matrix, true); iv.setScaleType(ScaleType.CENTER); iv.setImageBitmap(resizedBitmap); } 
+12
source

check this

 public static Bitmap rotateImage(Bitmap src, float degree) { // create new matrix Matrix matrix = new Matrix(); // setup rotation degree matrix.postRotate(degree); Bitmap bmp = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true); return bmp; } 
+2
source

You can use the Canvas API for this. Please note that you need to switch the width and height.

  final int width = landscapeBitmap.getWidth(); final int height = landscapeBitmap.getHeight(); Bitmap portraitBitmap = Bitmap.createBitmap(height, width, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(portraitBitmap); c.rotate(90, height/2, width/2); c.drawBitmap(landscapeBitmap, 0,0,null); portraitBitmap.compress(CompressFormat.JPEG, 100, stream); 
+1
source

Use Matrix.rotate (degrees) and draw a Bitmap onto your own canvas using this rotating matrix. I do not know, although if you have to make a copy of the bitmap before drawing.

Use Bitmap.compress (...) to compress your bitmap into output stream.

0
source

Singhak's solution works great. If you need the size of the result bitmap (possibly for ImageView), you can expand this method as follows:

 public static Bitmap rotateBitmapZoom(Bitmap bmOrg, float degree, float zoom){ Matrix matrix = new Matrix(); matrix.postRotate(degree); float newHeight = bmOrg.getHeight() * zoom; float newWidth = bmOrg.getWidth() / 100 * (100.0f / bmOrg.getHeight() * newHeight); return Bitmap.createBitmap(bmOrg, 0, 0, (int)newWidth, (int)newHeight, matrix, true); } 
0
source

Source: https://habr.com/ru/post/914220/


All Articles