Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 484
TylerKinkade Avatar asked Nov 30 '22 15:11

TylerKinkade


2 Answers

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());
like image 138
Brian Avatar answered Dec 04 '22 08:12

Brian


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.

like image 44
Seshu Vinay Avatar answered Dec 04 '22 09:12

Seshu Vinay