Rotation of Image not working - android

I am using imageView.setRotation(imageView.getRotation+90) on click for rotating image it's working fine. But whenever I sending that image (Rotated Image) to the server it's showing the original view to the server not rotated the view. I'm using Retrofit multipart for sending the image.

imageView.setRotation(imageView.getRotation+90)
it only rotates the imageview not the content of it ,as the result bitmap remians same.
You should rotate the bitmap using
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg,width,height,true);//BitmapOrg- is origanl bitmap
Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
and
imageView.setImageBitmap(rotatedBitmap);
now use the rotatedBitmap to send the server.

Related

Android: What is the correct way to rotate a bitmap?

The problem is when the bitmap gets rotated "R0" degrees, it looks like if it changed the pivot point. What I want is for it to rotate around the middle of the bitmap, but I don't know how.
Bitmap bitmap = (Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.base);
Matrix matrix = new Matrix();
matrix.reset();
matrix.setScale(bitmap.getWidth(), bitmap.getHeight());
matrix.setTranslate(-bitmap.getHeight()/2, -bitmap.getHeight()/2);
matrix.postRotate(R0);
Bitmap b = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
canvas.drawBitmap(b, x-24, y-40, null);
Thanks very much.
Try using matrix.postTranslate, instead of setTranslate, the latter will reset your matrix and setScale call will make no effect.

Android - How to crop a Bitmap image not using getResources

I know how to crop a bitmap image from resources, but i want to cache image from ImageView to Bitmap, and then i want to crop it.
So, here is my code:
mImageViewArt.buildDrawingCache();
Bitmap bmap = mImageViewArt.getDrawingCache(); // works good
Bitmap bmp= BitmapFactory.decodeResource(getResources(),bmap); // Doesn't work(Cannot be aplied Int to Bitmap)
Bitmap resizedbitmap1 = Bitmap.createBitmap(bmp, 0, 0, 100, 100);
Why are you using the bmp intermediate variable?
If you want to crop the image returned by getDrawingCache(), you should just pass it as the input to Bitmap.createBitmap(), i.e.
mImageViewArt.buildDrawingCache();
Bitmap bmap = mImageViewArt.getDrawingCache();
Bitmap resizedbitmap1 = Bitmap.createBitmap(bmap, 0, 0, 100, 100);

Get degree from the line that draw on bitmap

I have a project that will draw a line on a bitmap based on pixel coordinate and preview it with ImageView. The purpose of the line is to get degree from the line and then the degree will be use to rotate that bitmap. Is there any way to get the degree from the line that has been draw? Please give me suggestion how to do this.
Best Regards
This might help you, by this you can rotate your bitmap to 90 degrees actually
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 set the rotated image as
imageView.setImageBitmap(rotatedBitmap);
You can use similar technique to get degree of bitmap

Set ImageView orientation directly in Android?

I understand that I can rotate an ImageView in Android by
matrix.postRotate(finalOrientation - initOrientation, width, height);
But I want to set it as finalOrientation directly instead of rotating it by the difference, finalOrientation - initOrientation.
So I am expecting something like:
matrix.setOrientation(finalOrientation);
How can I do that?
Perfect! thanks #Bette Devine!
I made a function just for convenience.
public Bitmap rotateBmp(Bitmap bmp){
Matrix matrix = new Matrix();
//set image rotation value to 90 degrees in matrix.
matrix.postRotate(90);
//supply the original width and height, if you don't want to change the height and width of bitmap.
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),bmp.getHeight(), matrix, true);
return bmp;
}
I had to do this with a downsized image. I got a "java.lang.OutOfMemoryError" when I tried to do this with a full size image. : (
Matrix matrix = new Matrix();
//set image rotation value to 90 degrees in matrix.
matrix.postRotate(90);
//supply the original width and height, if you don't want to change the height and width of bitmap.
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, newWidth, newHeight, matrix, true);
Please refer the following link:
http://docs.xamarin.com/guides/android/application_fundamentals/handling_rotation/

Android: image rotation not desire to resize

I have a code for rotate an ImageView, and good good.
The problem is that the size of image change in the rotate process and I desire that not happen.
The xml code of image
<ImageView
android:id="#+id/imgBallesta"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/imgballesta"
android:contentDescription="#string/imageString"
/>
The code that creates the bitmap, I put this code in the declaration of class
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.imgObjectImage);
And the code for rotate the image, this code is inluded and onTouchEvent
Matrix mat = new Matrix();
matrix.postRotate((float) (angle*180/Math.PI), image.getWidth(),image.getHeight());
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), matrix, true);
image.setImageBitmap(bMapRotate);
Thanks for the help, and for read my question
Try with my method for rotate picture :
public static Bitmap rotate(Bitmap src, float degree) {
// create new matrix
Matrix matrix = new Matrix();
// setup rotation degree
matrix.postRotate(degree);
// return new bitmap rotated using matrix
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}
Good luck!!
I found a posoble solution to my problem. First I modify the size and height of my image and create the respective image for all drawable folders.
Then I use the next code for to animate the image.
Matrix matrix=new Matrix();
img.setScaleType(ScaleType.MATRIX); //required
matrix.postRotate((float) (a*180/Math.PI) , img.getWidth()/2, img.getHeight()/2);
img.setImageMatrix(matrix);
Thanks for response and read this question.

Categories

Resources