I have a rectangle-shaped Bitmap, which I need to rotate it by 90 degrees clockwise or counter-clockwise.
I can do the rotation using this code:
Matrix matrix = new Matrix();
matrix.setRotate(90, originalBitmap.getWidth()/2, originalBitmap.getHeight()/2);
return Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true);
However, this code rotates image "in-place" using old values for height/width. And the resulting image looks stretched and ugly.
Is there any good way to rotate the image by 90 degrees into new height/width? Probably, one possible solution is to modify dimensions of the original bitmap first?
Thanks
Don't you use old values while creating new bitmap? Just replace them in the last line:
return Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth()/2, originalBitmap.getHeight()/2, matrix, true);
Related
I am converting each frame retrieved from the camera into a bitmap and i display it on an ImageView, but i found that the imageView contains a rotated bitmap so i wanted to rotate the bitmap 90 degrees to set it right. to achieve this,
i wrote the below code to rotate the bitmap 90 degrees, but at run time i receive
bitmap size exceeds 32bits
i referred to some posts to solve this issue, and one of them suggested to recycle each bitmap used, but it did not solve the problem.
please let me know why i am getting this error and how to solve it?
code:
#Override
protected void onProgressUpdate(Bitmap... values) {
super.onProgressUpdate(values);
Bitmap b = values[0];
Matrix matrix = new Matrix();
matrix.postScale(b.getWidth()-10, b.getHeight()-10);
matrix.postRotate(90);
Bitmap resizedBitmap = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
mIVEdges.setImageBitmap(resizedBitmap);
b.recycle();
}
If you have 100x100 pixel bitmap, then
matrix.postScale(b.getWidth()-10, b.getHeight()-10);
will cause new bitmap to be scaled with 90 scale factor.
matrix.postScale(90, 90);
So output of new Bitmap is 9000x9000 pixel.
If you want to reduce size of new bitmap, you want to use scale factor > 0.0 and < 1.0
matrix.postScale(0.9f, 0.9f);
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
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/
I have a PNG file that I want to use for an overlay - however, this file has to be mirrored (and rotated by 180°), but in order to save space, I don't want to place the mirrored file in the apk, but do this action programmatically.
How can I do this with Froyo and above?
Scaling by -1.0 causes the image to be flipped. Assuming bmp is the bitmap you want to mirror (here on the x axis) you can do :
Matrix matrix = new Matrix();
matrix.preScale(-1.0f, 1.0f);
Bitmap mirroredBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.width(), bmp.height(), matrix, false);
If you don't want to create a second bitmap, you can do the same with canvas.scale :
canvas.save();
canvas.scale(-1.0f, 1.0f);
canvas.drawBitmap(bitmap, ...); // The bitmap is flipped
canvas.restore();
You can simply use View.setScaleX()
For example
public void mirrorView(View v){
v.setScaleX(-1.0f);
}
The issue is simple: I want to rotate an image around a certain pivot point. Here is the code I use:
Matrix matrix = new Matrix();
matrix.setTranslate(bmpWidth/2, 0);
matrix.preRotate(degrees, bmpWidth/2, 0);
Bitmap resizedBitmap = Bitmap.createBitmap(
bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
ImageView imageView = (ImageView) findViewById(R.id.bell);
imageView.setImageBitmap(resizedBitmap);
I get the rotation degrees from the accelerometer sensor. The result is that every time the image is rotated around its center point.
I think you should try doing this:
imageView.setPivotX(desiredXPivotPoint);
imageView.setPivotY(desiredYPivotPoint);
That should do the trick.
This is kinda old, but there is a function that allows you to specify both the degrees of rotation as well as the pivot point:
imageView.setRotation(degrees, px, py)