Reputation: 3
For cropping images I am using CropImageView of joshholtz (https://github.com/joshdholtz/CropImageView). But I am getting IllegalArgumentException exception (saying: x + width must be <= bitmap.width()) at CreateBitmap function at crop function in the last line right before the return line.
public Bitmap crop(Context context) throws IllegalArgumentException {
// Weird padding cause image size
int weirdSidePadding = this.getWeirdSideMargin();
int weirdVerticalPadding = this.getWeirdVerticalMargin();
FrameLayout.LayoutParams params = (LayoutParams) mDaBox.getLayoutParams();
// Getting crop dimensions
float d = context.getResources().getDisplayMetrics().density;
int x = (int)((params.leftMargin - weirdSidePadding) * d);
int y = (int)((params.topMargin - weirdVerticalPadding) * d);
int width = (int)((this.getWidth() - params.leftMargin - params.rightMargin) * d);
int height = (int)((this.getHeight() - params.topMargin - params.bottomMargin) * d);
Bitmap crooopppppppppppppppeed = Bitmap.createBitmap(mBitmap, x, y, width, height);
return crooopppppppppppppppeed;
}
Actually I had a look at potentially same questions, but unluckily they are not same with my situation to the degree to help me.
Can you please help me to come over this barrier?
Upvotes: -3
Views: 369
Reputation: 93678
So in createBitmap, the function you're using creates a bitmap from a subsection of the original bitmap. For it to work, x+width may not be bigger than the width of the original bitmap (same for y+height) and x and y must both be >=0. This isn't the case here.
I think I get what this function is trying to do, but its just wrong. It seems to confuse the idea of cropping and scaling. If you want to scale and crop a bitmap at once, you should use the createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) with the scale factor in the matrix m. And I'm not sure you want to be scaling here at all- I don't know why the code is looking at the density, but its probably wrong to be doing so.
Upvotes: 0