Android: Cropping an image to specific size

2019-03-10 11:40发布

问题:

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

回答1:

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());


回答2:

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.