Cutting the image in jigsaw shapes in android - 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.

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

create part of bitmap as a new bitmap

i have a problem and i don't know how to solve it. What i'm trying to do is, to create a Bitmap which is a part of another Bitmap.
I tried already this
Bitmap newBitmap = Bitmap.createBitmap(bitmap, (int)x, (int) y, (int)width, (int)height);
The width/height of the new bitmap is alright, but it's empty, every pixel is just transparent.
So i searched and found that i have to use canvas to draw the part again, because the return of createBitmap is a empty once. But i'm not sure since the call up here returns an immutable Bitmap, which Canvas doesn't like. I also not know which constructor i have to use with canvas.
Could someone help me?
Thanks and greetings
That one will create an INMUTABLE bitmap. Please, refer to http://developer.android.com/reference/android/graphics/Bitmap.html and read about that method.
To create a mutable one, use createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config). Pass in the widht and height of the part u wanna render in and the displayMetrics you get from yourActivity.getResources().getDisplayMetrics().
To render the area you want in the newly created bitmap, yes, use the Canvas. There are lots of methods. You can use this one: http://developer.android.com/reference/android/graphics/Canvas.html#drawBitmap(android.graphics.Bitmap, android.graphics.Rect, android.graphics.RectF, android.graphics.Paint)
You pass in a rect with the source area and the destination area.

Any alternative way to achieve BitmapRegionDecoder.decodeRegion(...) android 2.2 and below?

I am using BitmapRegionDecoder.decodeRegion(...) to crop image. But it does not work for android 2.3.1 (api level 10 ) and below. Please let me know is there any alternative way to achieve this. Even android-support-v4.jar library does not contain this class.
You can use Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height) to crop an image. It's been around since always, so provides an alternative for older APIs.
The documentation is pretty self-explanatory:
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.
Parameters
source The bitmap we are subsetting
x The x coordinate of the first pixel in source
y The y coordinate of the first pixel in source
width The number of pixels in each row
height The number of rows
Returns A copy of a subset of the source bitmap or the source bitmap
itself.

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?

android - Bitmap.createScaledBitmap results in a Bitmap sized -1

i try creating a scaled bitmap using that method but once i access the bitmap, my app crashes with the following message:
"java.lang.IllegalArgumentException: y + height must be <= bitmap.height()"
When I debug it, I can see that immediately after calling "createScaledBitmap", the resulting bitmap has a size of -1, -1. if i use "createBitmap" instead, the resulting bitmap does show the size of the original bitmap.
Should nobody here know what the cause of this is... is there maybe a workaround to resize a bitmap differently? I couldn't find anything in the bitmap class but maybe somebody here knows another way?
First, i want to clarify that you are getting an error from Bitmap.createBitmap method, not from Bitmap.createScaledBitmap. Syntax of createBitmap is as shown below,
Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
Parameters
source: The bitmap we are subsetting
x: The x coordinate of the first pixel in source
y: The y coordinate of the first pixel in source
width: The number of pixels in each row
height: The number of rows
m: Optional matrix to be applied to the pixels
filter: true if the source should be filtered. Only applies if the matrix contains more than just translation.
An error is occured because,
x, y, width, height values are outside of the dimensions of the source bitmap.

Categories

Resources