Get degree from the line that draw on bitmap - android

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

Related

bitmap rotation causes an error

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);

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.

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/

Rotating an image around a pivot point

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)

Rotate rectangle bitmap in Android

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);

Categories

Resources