在android中分割位图时出现java.lang.IllegalArgumentException
有人可以告诉我为什么在分割位图时会出现此错误。 代码:
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;
}
行= 6,列= 6; 图像大小 = 648 x 484
这是例外,但不知道如何修复:
java.lang.IllegalArgumentException: y + height must be <= bitmap.height()
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您试图抓取原始位图中不存在的部分。
在该行处放置一个断点:
您会看到它在第一次数组迭代之后的某个时间失败,因为每次偏移您通过 xCoord/yCoord 抓取的大地图“切片”的起点。
我的直觉表明您对 FinalSize 的计算是错误的,但我只能推测,因为我们不确切知道您想要完成什么。
Your trying to grab sections of the original bitmap that don't exist.
Put a breakpoint at the line:
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.
我已经尝试过你的代码并进行了一些更改,它对我有用。
I have tried your code and changes a little bit and it works for me.