Android get rotated bitmap. (Image straightening) - android

I am trying to rotate a bitmap and crop out a valid Rectangle out of it, and then save it on the disk. What i am trying to do is explained in the following link:
http://i.stack.imgur.com/IIhBw.png
Black is the original image. Red is rotated image (by 15 degrees here) and green is the valid portion of the red image.
This feature is similar to the Image straightening which Instagram app does.
So far, I have tried this:
public static Bitmap getRotatedBitmap2(Bitmap bmp, int rotation){
Matrix matrix = new Matrix();
matrix.preRotate(rotation);
Bitmap bitmap = Bitmap.createBitmap(bmp, 0, 0,
bmp.getWidth(), bmp.getHeight(),
matrix, false);
return bitmap;
}
This rotates the bitmap but it shrinks in size, also the rotated bitmap is enclosed is an black rectangle. i need to get rid of the out-bounding black rectangle. Also, How can I obtain the valid portion of bitmap?

Aviary and ImageMagick are libraries that can help to achieve the results asked in the question, as well perform other Bitmap operations and Image Manipulations.

I know you already solved this question, but if somebody is looking for another solution, you can check this library image-rotator that solves problem like this or very similar.

Related

Android. How to effective frequently create bitmap?

I'm using library which takes bitmap.
At first I'm showing bitmap in ZoomableImageView. But after scale, zoom I need to send to the library only visible part of image view.
Here is my code to create scaled bitmap with matrix:
Bitmap result = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true);
However, the user may be constantly zooming in on the image, and what happens is that this code is executed frequently and my application freezes. Is there any way to prevent this or is there a better way to create scaled bitmap ?
Please help me.

How to draw an image on top a bitmap and combine as one bitmap in Android Java

I am developing an Android application using Java. I am doing some image manipulating in my application. First of all, I like to save I have no knowledge about image processing. But, I am trying to get into it. What I would like to do now is draw a simple image on a bitmap and save it as one bitmap.
I am loading an image from asset folder as a bitmap like this.
Bitmap rectBitmap = BitmapFactory.decodeStream(istr);
Let's say the photo is a just simple rectangle like this.
Then I would like to draw a bitmap (triangle shape) using coordinates point. The image would be some this.
My imagination of code would be like this.
rectBitmap.drawOnTop(coorPointOneValues, coorPointTwoValues, coorPointThreeValues);
Coordinate point values would be x and y value since I am working on the 2D coordinate system.
Then I would like to save the image something like this after drawing traingle
rectBitmap = rectBitmap.saveBitmap();
How can I do it? The scenario mentioned is a possible way that I can think of. If it is not possible, what would be the other way around?
You can do it like this.
1, read or create your bitmap:
Bitmap rectBitmap = BitmapFactory.decodeStream(istr);
Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2, create a canvas on the bitmap:
Canvas canvas = new Canvas(bitmap);
3, draw something:
canvas.drawColor(Color.RED)
canvas.drawRect / canvas.drawLine / canvas.drawArc ...
//for triangle shape you can use drawPath
4, save the bitmap:
bitmap.compress(CompressFormat format, int quality, OutputStream stream)

How to erase the drawing on image

I am trying to draw on image in android. I found one good example from here.
Here they explained how to draw in the plain canvas. I tried to add image in that canvas.
canvasBitmap = BitmapFactory.decodeResource(res, R.drawable.image)
.copy(Bitmap.Config.ARGB_8888, true);
canvasBitmap = Bitmap.createScaledBitmap(canvasBitmap, this.getWidth(),
this.getHeight(), true);
Now I am facing one issue. When I try to erase the drawings it erase the original image also.
I tried lots of solution. But nothing helps me. Please let me know how to erase the drawings in the image without erase the Image.
A simple solution to this is to just draw the image back onto Canvas. Over everything else.
This would leave you with just the origional image -> Drawings removed.

Crop particular part of image in android

I want to crop Red part from following image, Is there any simple method available in android that can crop following image.
I have found many SO questions but all are suggesting to used following code:
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100);
This code work well if width & height are around 2MP resolution, but if that cropped part is more than 3MP resolution than application got crashed with OOM error.
Is there any way that handle image more than 3MP during cropping?
You can used following code that can solve your problem.
Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100, matrix, true);
Above method do postScalling of image before cropping, so you can get best result with cropped image without getting OOM error.
For more detail you can refer this blog
1- Change your imageview for bitmap
final Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.img);
2-use your bitmap to crop what you want
Bitmap croppedBmp = Bitmap.createBitmap(bitmap, x, y , width , height);
3-Take care x,y from Top and left
4- to preview your bitmap again in your imageview
imageView.setImageBitmap(croppedBmp);
If you want to crop image in any shape OR only selected part then
you can use ready made open source library

Android, rotate 18x18 pixel circle icon, result: icons of different sizes?

I'm drawing a bunch of icons on a map. Actually the icons come from the same image rotated. But on the map the images take on two different sizes, I don't know why. This is what the result looks like: http://orangesoftware.net/iconmap.png
The image file looks like this: http://orangesoftware.net/arrow18.png
The code to rotate the icon:
Matrix mtx = new Matrix();
mtx.postRotate(unit.heading);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.arrow18);
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), mtx, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
Any magically insights appreciated, thanks
The cause of the variation in sizes is when a rotation is not a multiple of 90 degrees. The bmp becomes a diamond who's corners stick out beyond the ImageView holding it, thus it gets resized to fit the ImageView.
The easiest way to take care of this discrepancy is to set the ImageView's scaleType to CENTER. This will simply center the image inside without scaling it to fit.

Categories

Resources