I am trying to flip an image view horizontally after putting a Sobel Filter on it. The filter converts the image in black and white and then then I do this to flip the image horizontally.
Matrix matrix = new Matrix();
matrix.preScale( - 1, 1 );
Bitmap resizedBitmap =
Bitmap.createBitmap( bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false );
What happens is really weird. My image flips correctly but the dark spots get whiter. I have attached two images for comparison.
Related
I basically need to rotate 90 degres a small part of an ImageView (e.g):
In the Image above, I'd like to rotate the 4 so it displays correctly. Only the 4, the rest should remain vertical as it is.
Is there a way I can achieve it?
By Implementing the method suggested by MikeM. I'm getting the following result.
As you can see there are two major things I need to fix:
The rotated square is working, although in the wring position. How do I found out the exact coordinates of the 4
The background of the image, has been changed to black. It used to be transparent
If you know, or can figure, the coordinates and dimensions of the region you'd like to rotate, then the process is relatively straightforward.
Load the image as a mutable Bitmap.
Create a second, rotated Bitmap of the desired region from the original.
Create a Canvas on the original Bitmap.
Clear the clipped area, if necessary.
Draw the rotated region back onto the original.
In the following example, it's assumed that the region's coordinates (x, y) and dimensions (width, height) are already known.
// Options necessary to create a mutable Bitmap from the decode
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
// Load the Bitmap, here from a resource drawable
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resId, options);
// Create a Matrix for 90° counterclockwise rotation
Matrix matrix = new Matrix();
matrix.postRotate(-90);
// Create a rotated Bitmap from the desired region of the original
Bitmap region = Bitmap.createBitmap(bmp, x, y, width, height, matrix, false);
// Create our Canvas on the original Bitmap
Canvas canvas = new Canvas(bmp);
// Create a Paint to clear the clipped region to transparent
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
// Clear the region
canvas.drawRect(x, y, x + width, y + height, paint);
// Draw the rotated Bitmap back to the original,
// concentric with the region's original coordinates
canvas.drawBitmap(region, x + width / 2f - height / 2f, y + height / 2f - width / 2f, null);
// Cleanup the secondary Bitmap
region.recycle();
// The resulting image is in bmp
imageView.setImageBitmap(bmp);
To address the concerns in the edit:
The rotated region's figures in the original example were based on the image with the long axis vertical. The image in the edit had been rotated to vertical after that region had been modified.
The black background was due to having inserted the resulting image into MediaStore, which saves images in the JPEG format, which does not support transparency.
I had go through lot of links but still I am confuse how to do it.
I have total 60 images of one object with different angle.When user swipe image right or left then I should be show the particular angle image like image is rotating automatically.
Please suggest me the right way to do so.
Use Matrix rotation.
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
Bitmap rotated = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
I am working on a project where I need to ZoomIn and rotate the image simultaneously on seekbar progress value.
I have tried below code. But this is not working. This is just rotating the image.
Matrix matrix = new Matrix();
matrix.postRotate(curValue);
//matrix.postScale(curValue, curValue);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmaprotate, 0, 0, bmpWidth, bmpHeight, matrix, true);
mainImage.setImageBitmap(resizedBitmap);
For achieving this you better to use custom view and canvas. Using canvas you can easily rotate and scale the images. Please refer this links (resize the bitmap canvas ,Android Scaling Canvas Bitmap , http://www.barebonescoder.com/2010/06/android-development-simple-2d-graphics-part-1/)
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.
While rotating a bitmap using matrix, vertex is not stable..
Matrix matrix = new Matrix();
matrix.postRotate(mDegree,100,100);
mCompasstemp = Bitmap.createBitmap(mCompPic, 0, 0, mCompPic.getWidth(), mCompPic.getHeight(), matrix, true);
mCompassHud.setImageBitmap(mCompasstemp);
Output of my code is like
-bitmap will rotate.
-vertex of my bitmap is not stable.
-Bitmap is resizing
I need disable image resizing and make the rotation stable.Can you please suggest a solution for this?
Rather than creating your new Bitmap directly from the original, another (more straight-forward, imho) option is to create the resultant Bitmap, create a Canvas with that Bitmap, then do your rotation/translation/scaling on the Canvas and draw the original Bitmap onto the new Bitmap via the Canvas.
Basically, you're looking, then, at:
scaledImage = Bitmap.createBitmap (croppedWidth, croppedHeight, Bitmap.Config.ARGB_8888);
Canvas offscreenCanvas = new Canvas (scaledImage);
Matrix matrix = new Matrix();
matrix.setRotate (rotations, centreX, centreY);
matrix.postScale(scaleX, scaleY);
offscreenCanvas.setMatrix (matrix);
offscreenCanvas.drawBitmap (pickedImage, 0, 0, new Paint(Paint.DITHER_FLAG));
Not sure if this is what your looking for but it might help.
Android uses its built in compatibility features to scale and render a bitmap appropriately for screens with different pixel densities. There are two methods of scaling, pre-scaling and auto-scaling.
It will pre-scale bitmaps' from resources and auto-scales when the bitmap is being drawn internally (which is what your doing be using createBitmap).
Go to http://developer.android.com/guide/practices/screens_support.html and check under:
4.Use density and/or size-specific resources:
Pre-scaling and auto-scaling of bitmaps and nine-patches
I have tried this code, and the rotate is stable at the center of the bitmap
matrix.reset();
matrix.setRotate(degree, Xpos+bitmap.getWidth()/2, Ypos+bitmap.getHeight()/2);
and then in canvas doDraw()
canvas.setMatrix(matrix);
canvas.drawBitmap(bitmap, Xpos, Ypos, null);
canvas.setMatrix(null);
The Xpos and Ypos is the X and Y position of the bitmap
The setMatrix(null), set the matrix to null, so that the rotate didn't affect the after bitmap
And it didn't always create new bitmap, so it's great for performance
I hope that help
I know its an old question but, all answers with code imply a canvas, so heres a solution without a canvas that worked for me :
Matrix matrix = new Matrix();
matrix.postRotate(mDegree,100,100);
mCompasstemp = Bitmap.createBitmap(mCompPic, 0, 0, mCompPic.getWidth(),
mCompPic.getHeight(), matrix, true);
mCompasstemp = Bitmap.createScaledBitmap(mCompassTemp, mCompPic.getWidth(),
mCompic.getHeight(), false);
mCompassHud.setImageBitmap(mCompasstemp);
So basically after rotating your bitmap you rescale it to the desired size. Might not be best practice, but at least you dont have to create an extra canvas if you dont want/need to, since the involving Paint() Object is not an inexpensive operation either.