Crop to fixed portion of ImageView on Android - 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?

Related

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.

How do i split a bitmap image in android?

I'm just a beginner with android. I want to divide a bitmap image into chunks and then display the image in the same way but divided. is this possible?
Instead of trying to break the image up into chunks you should just use:
canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
This will draw parts of the image. So to give the effect of breaking it up you are really just using the src and dest rectangles to draw specified parts of the images. Where src is the rectangle in local image coordinates of the part you want to draw and dest is the rectangle used to position it on the screen.
You can use the following method to get an area of an source bitmap:
Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height)

Android: Cropping an image to specific size

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.

Cutting the image in jigsaw shapes in android

I want to know how to cut an image in jigsaw form in android at run time. What i actually want is user can enter any image and then made puzzle of it.
Any API or method for doing this in android ?
Thanks in Advance.
You can use this method in Bitmap to cut it into a new BitMap of specified width and height pixels
starting from specified pixel location of x and y
public static Bitmap createBitmap (Bitmap source,
int x,
int y, int width, int height)
Assuming your puzzle piece is square, you can use createBitmap() function.
Returns an immutable bitmap from the specified subset of the source
bitmap. The new bitmap may be the same object as source, or a copy may
have been made. It is initialized with the same density as the
original bitmap.

Categories

Resources