Android: Cropping an image to specific size - android

My intention is to have the user pick an image from the gallery, then have a cropping activity come up. However, I need the rectangle that defines the cropping mask to be locked to a certain dimension and then the user simply repositions it to display a portion of an image.
Any ideas on how this would be done?
Thanks
-T

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null)
.setType("image/*")
.putExtra("crop", "true")
.putExtra("aspectX", width)
.putExtra("aspectY", height)
.putExtra("outputX", width)
.putExtra("outputY", height)
.putExtra("scale", true)
.putExtra("scaleUpIfNeeded", true)
.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f))
.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

You need to create a custom ImageView class to achieve zooming and panning of an image and can have a fixed rectangle image(transparent) overlaying on this image. And can create sub-bitmap of that bitmap. and save it in a file.
createBitmap(Bitmap source, int x, int y, int width, int height);
This method is used to create a sub-bitmap.
http://blog.sephiroth.it/2011/04/04/imageview-zoom-and-scroll/
After achieving zooming and panning, I am not sure if createBitmap can create sub-bitmap from the visible portion of the image(i.e. part of image wont be visible on the screen when it is zoomed), So try getting the drawingCache() from imageView and create sub-bitmap for the same.

Related

Crop the captured image on the basis of Rect Cordinates in android

I am trying to crop out the portion inside the RoundRect. I have an overlay on top of PreviewView. The horizontal margin is 2% of the screen width and top is 15% of the screen height.
When I take the picture, I want to crop it with the same proportion as the RoundRect displayed. As the resolution of the captured image is different than that of the screen, I am not being able to get the exact portion inside the rounded rectangle. Is there anyway I can use the margin value used for the RoundRect to get the margin value for the captured Image?
I did try scaling it down to the screen size as mentioned here and crop it but even that doesn't help as quality and aspect ratio gets compromised.
I am using bitmap to crop the captured image.
Bitmap.createBitmap(originalBp, left, top, right, bottom);
The outputs are aligned with the crop rect. The crop rect of different use cases should be mapped to the same area on the camera sensor. If you are using PreviewView, a simple way to calculate the transform from Preview to ImageCapture is using CoordinateTransform:
// Build the transform from PreviewView to Capture
OutputTransform source = previewView.getOutputTransform();
OutputTransform source = FileTransformFactory().getOutputTransform(capturedFile);
CoordinateTransform coordinateTransform = new CoordinateTransform(source, target);
coordinateTransform.mapRect(roundRect);
// Then use roundRect to crop the capture file.

Android: How to select part of image?

In my application. There's section select block of apartment. In there a A and B block. So i want to choose A or B block from single image. Image is
There is what way to do this?
I guess you could use getLocationOnScreen()and hardcode the values you want. If user clicks on certain part of the image it prompts one response, if they click somewhere else it prompts another response. More info here Android: How do I get the x y coordinates within an image / ImageView?
Use BitMap concept. Convert full image as bitmap the crop the bitmap based on (x,y) positions.
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.myimage);
Bitmap cropbitmap=Bitmap.createBitmap(bitmap, xpoint,ypoint,width, height);
//default android method createBitmap(Bitmap source, int x, int y, int width, int height)
other simple way use library like this from github
android-crop
uCrop
Android-Image-Cropper

Crop bottom portion of a bitmap programmatically

I have a bitmap taken from a camera. I want to crop the image so it only leaves the bottom portion of it. The cropped image should be 80% less the height of the original bitmap, so I want only the 20% of the bottom part starting from the left edge.
I'm doing this explicitly in the code without any Android cropping intent whatsoever.
An image to visualize what I want to achieve:
I've managed to crop the top part of the bitmap by using this code:
final Bitmap toBeCropped = BitmapFactory.decodeFile(mFile.getPath());
final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inTargetDensity = 1;
toBeCropped.setDensity(Bitmap.DENSITY_NONE);
int fromHere = (int) (toBeCropped.getHeight() * 0.2);
Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, 0, toBeCropped.getWidth(), fromHere);
mPreviewHalf.setImageBitmap(croppedBitmap);
But I couldn't find a way to start the cropping 80% from the top. I'm thinking of getting the y-coordinate of the Bitmap, so that I could crop any image sizes and always get the bottom portion only. But can anyone point to me how do I get this coordinate from a bitmap? Or do I have to take it from the layout itself?
I am not familiar with operations on Bitmaps but from inspecting your code and looking at the API my guess would be that you need to specify the y coordinates on the following line to match the starting point:
Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, "here", toBeCropped.getWidth(), fromHere);
So my guess would be something like the following:
Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, (toBeCropped.getHeight() * 0.8), toBeCropped.getWidth(), fromHere);
in this case fromHere will define the number of rows you want to crop not the starting point (which is 20% of the total as you have pointed out)
This is how I do it:
topcutoff is what you want to cut of on top of the image and buttomcutoff on the buttom (if needed)
height = height - topcutoff;
height = height - bottomcutoff;
croppedBitmap = Bitmap.createBitmap(croppedBitmap, 0, topcutoff, width, height);
Basically you just set a startpoint (topcutoff) from where to begin displaying the bitmap. In your case this would be the position after 80% of your bitmap.
This might also explain some things: Google Bitmap Documentation
"int: The y coordinate of the first pixel in source", so where you want to begin displaying your image.

Crop Bitmap using RectF parameters on Android

Using MikeOrtiz's awesome ImageView implementation with touch and zoom events, I wanted to crop a picture taken with the camera to match the zoom. Using his method...
// Return a Rect representing the zoomed image.
RectF getZoomedRect();
...I tried cropping the resulting picture bitmap to the zoom size like so:
RectF zoomCoordinates = mTouchImageView.getZoomedRect();
Bitmap croppedBitmapToOverview = Bitmap.createBitmap(
AppResources.sCurrentImage,
((int) zoomCoordinates.left),
((int) zoomCoordinates.top),
((int) zoomCoordinates.width()),
((int) zoomCoordinates.height()));
However I get a "must be bigger than 0" error with this. While debugging I noticed ALL values were 0 due to casting to an Integer. The real values however go something like this:
//Log.d print for each of those fields without the int cast
Left 0.34047672
Top 0.20797288
Width 0.33333334
Height 0.3429547
So there's my problem, but I can't see how to fix this. I've never worked with bitmaps before or canvas, Rect, etc.
Is there some tweaking I could do to these values, or should I take a different approach altogether?
Got around the problem by simply taking a "screenshot" of the View of sorts. This got me a Bitmap with the picture as it was zoomed
mTouchImageView.setDrawingCacheEnabled(true);
AppResources.sCurrentImage = Bitmap.createBitmap(mTouchImageView.getDrawingCache());
multiply the coordinates with the size of your image like
(int)(zoomCoordinates.left * imageSize)

Crop to fixed portion of ImageView on Android

How do i get a part of a bitmap (which is in a imageview) saved as shown in the picture. All i need to do is to crop a particular image using (x,y) coordinates. all that i have is x,y coordinates and nothing else to select the cropped area. Between, user doesnt select the crop area. So its enough if its static. yellow part is an imageview(not the whole activity)
just create a new Bitmap and use the original one as the source.
Insert as x and y coordinates 0 and your custom width and hight.
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height) .
This post describes how to get a Bitmap from a view, in your case the imageview you want to crop.
Converting a view to Bitmap without displaying it in Android?

Categories

Resources