Reputation: 220
Can someone tell me why I am getting this error when splitting a bitmap. Code:
public static List<Bitmap> ScambleImage(Bitmap image, int rows, int cols){
List<Bitmap> scambledImage = new ArrayList<Bitmap>();
int chunkWidth = image.getWidth(); //cols
int chunkHeight = image.getHeight(); //rows
int finalSize = chunkWidth/rows;
Bitmap bMapScaled = Bitmap.createScaledBitmap(image, chunkWidth, chunkHeight, true);
int yCoord = 0;//The y coordinate of the first pixel in source
for(int x = 0; x < rows; x++){
int xCoord = 0;//The x coordinate of the first pixel in source
for(int y = 0; y < cols; y++){
scambledImage.add(Bitmap.createBitmap(bMapScaled, xCoord, yCoord, finalSize, finalSize));
xCoord = finalSize + xCoord;
}
yCoord = finalSize + yCoord;//The y coordinate of the first pixel in source
}
return scambledImage;
}
rows = 6, and cols = 6; image size = 648 x 484
this is the exception but don't know how to go about fixing:
java.lang.IllegalArgumentException: y + height must be <= bitmap.height()
Thanks!
Upvotes: 1
Views: 1850
Reputation: 19500
I have tried your code and changes a little bit and it works for me.
private ArrayList<Bitmap> splitImage(Bitmap bitmap, int rows, int cols){
int chunks = rows*cols;
int chunkHeight,chunkWidth;
ArrayList<Bitmap> splittedImages = null;
splittedImages = new ArrayList<Bitmap>(chunks);
chunkHeight = bitmap.getHeight()/rows;
chunkWidth = bitmap.getWidth()/cols;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,bitmap.getWidth(), bitmap.getHeight(), true);
int yCoord = 0;
for(int x=0; x<rows; x++){
int xCoord = 0;
for(int y=0; y<cols; y++){
splittedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
xCoord += chunkWidth;
}
yCoord += chunkHeight;
}
return splittedImages;
}
Upvotes: 0
Reputation: 20177
Your trying to grab sections of the original bitmap that don't exist.
Put a breakpoint at the line:
scambledImage.add(Bitmap.createBitmap(bMapScaled, xCoord, yCoord, finalSize, finalSize));
And you'll see it fails sometime after the first array iteration because each time your offsetting the start point of which "slice" of the bigmap you are grabbing by xCoord/yCoord.
My hunch says your calculation for finalSize is wrong, but I can only speculate since we don't know exactly what your trying to accomplish.
Upvotes: 1