Reputation: 1
The function calls the name of the image then where it is going to be put then from where to where it is going to be cropped and to where but it doesn't seem to work correctly it doesn't execute it
public void _cropImage(final ImageView _img, final String _nameImg, final double _x, final double _y, final double _x1, final double _y1) {
// Get the resource ID of the drawable image specified
int resId = getResources().getIdentifier(_nameImg, "drawable", getPackageName());
// Decode the image resource into a Bitmap object
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), resId);
// Calculate the scale factor to convert pixels to image coordinates
float scaleFactor = (float) originalBitmap.getWidth() / (float) _img.getWidth();
// Convert the clip dimensions from pixels to image coordinates
int x_1 = (int) (_x * scaleFactor);
int y_1 = (int) (_y * scaleFactor);
int x_2 = (int) ((_x + _x1) * scaleFactor);
int y_2 = (int) ((_y + _y1) * scaleFactor);
// Ensure that the crop dimensions are within the bounds of the original bitmap
x_1 = Math.max(x_1, 0);
y_1 = Math.max(y_1, 0);
x_2 = Math.min(x_2, originalBitmap.getWidth());
y_2 = Math.min(y_2, originalBitmap.getHeight());
// Create a new bitmap to hold the clipped region
Bitmap croppedBitmap = Bitmap.createBitmap(originalBitmap, x_1, y_1, x_2 - x_1, y_2 - y_1);
// Display the cropped bitmap in the ImageView
_img.setImageBitmap(croppedBitmap);
}
The function doesn't crop the image from x,y at x,y
Upvotes: 0
Views: 651
Reputation: 386
For Image crop, you can use the below dependency. It's very simple and easy to use more useful I personally use it
implementation 'com.isseiaoki:simplecropview:1.1.8'
Upvotes: 0