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);
Related
I need to test a simple task. I want to create scaled bitmap by setting postScale to the Matrix and using it in the creation, here's a code:
Matrix matrix = new Matrix();
matrix.postScale(5.0f, 5.0f);
Bitmap bitmap = Bitmap.createBitmap(bitmapSrc, 500, 500, 50, 50, matrix, true);
I thought this code supposed to crop 50x50 bitmap from the source scaled in 5 times, but when i'm using this bitmap to show the result in ImageView
imageView.setImageBitmap(bitmap);
The scaling doesn't seem to work and i'm getting 50x50 bitmap from original source bitmap(without scaling).
I think i'm missing something, but i can't quite figure out what. Any help highly appreciated
Edit: I've also tried to set last parameter to false and it didn't help, but if i'm using postRotate in matrix i'm getting rotated bitmap
Android contains the function Bitmap.createScaledBitmap()...
You can use this as follows:
public Bitmap getScaledBitmap(Bitmap bitmap, float scale) {
Integer originalHeight = bitmap.getHeight();
Integer originalWidth = bitmap.getWidth();
Integer requiredHeight = Math.round(originalHeight * scale);
Integer requiredWidth = Math.round(originalWidth * scale);
return Bitmap.createScaledBitmap(bitmap, requiredWidth, requiredHeight, true);
}
You can checkout this for other relevant functions here.
It might be that the scaling happens too late and the crop area is out of bounds because of it. Did you try it with preScale instead of postScale?
If that does not work, you can try using coordinates within the small bitmap first, like this:
Bitmap bitmap = Bitmap.createBitmap(bitmapSrc, 100, 100, 10, 10, matrix, true);
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 created a bitmap image which is a circle and than as I wanted to animate it so i converted it into bitmapdrawable and than added it to animation drawable..But due to this the circle shaped has changed to oval shape...
So what should I do ?
Is there any other method to animate only the bitmap file. ?
Thanks in advance..
If you're using Canvas, I'd suggest holding a pointer to the current Bitmap and loading all other Bitmaps into an array.
Say,
Bitmap[] frames = new Bitmap[10] //10 frames
Bitmap frame[0] = BitmapFactory.decodeResource(getResources(), R.drawable.circlefram1);
Bitmap frame[1] = BitmapFactory.decodeResource(getResources(), R.drawable.circlefram2);
...
Select the currentFrame by pointing at the frame you're interested in.
Bitmap currentBitmap = frame[3]; // 4th frame
So when you call drawBitmap(currentBitmap) it will only draw the frame you are interested in. You can change the bitmap every so many frames, by assigning an fps to the frame animation.
If you just want to scale or rotate the bitmap (rotating a circle?), the best way to resize a bitmap is using createScaledBitmap, and rotating using a matrix.
For Scaling, you load any bitmap into memory like this
Bitmap circleBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.circle);
If you would like the circle (or any bitmap) rescaled you would do something like this:
Bitmap scaledCircle = Bitmap.createScaledBitmap(circleBitmap, dstWidth, dstHeight, filter);
Where dstWidth and dstHeight are the target destination width and height, which you can previously calculate by scaling the original width and height.
int scaledHeight = circleBitmap.getHeight()/2;
int scaledWidth = circleBitmap.getWidth()/2;
And finally you would normally draw this Bitmap using a canvas like this
canvas.drawBitmap(bitmap)
For rotating, create a Matrix
Matrix mat;
mat.postRotate(degrees); // Rotate the matrix
Bitmap rotatedBitmap = Bitmap.createBitmap(originalBitmap, x, y, width, height, mat, filter);
and finally
canvas.drawBitmap(rotatedBitmap);
Keep in mind Canvases are slow for games or anything real-time!
Hope it helps.
no, you can not animate the bitmap itself with the android animation framwork. You can directly animate Views or ViewGroups and all the classes that derives from View and ViewGroup.
Call view the ImageView that contains the bitmap.
TranslateAnimation slide = new TranslateAnimation(view.getX(), view.getX() + 100, view.getY(), view.getY() + 100 );
slide.setDuration(1000);
view.startAnimation(slide)
that should translate the ImageView by 100px from the current position
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);