我正在尝试用我制作的方法分割位图:
private List<Bitmap> splitBitmap(File file, int everyPixel)
{
Bitmap comicBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
List<Bitmap> segments = new ArrayList<Bitmap>();
if(comicBitmap == null)
return segments;
int height = comicBitmap.getHeight();
int width = comicBitmap.getWidth();
int h = 0;
for(int i = 0; i < height; i += everyPixel)
{
i = i >= height? height : i;
Bitmap bitmap = Bitmap.createBitmap(comicBitmap, 0, i, width, i + everyPixel);
segments.add(bitmap);
}
return segments;
}
我正在处理的位图的大小是240x4298。这是一个非常非常长的图像。我需要生成位图列表(来自原始源的分段位图)。当 i
到达 2400 时,它会抛出一个错误:
Caused by: java.lang.IllegalArgumentException: y + height must be <= bitmap.height()
我的逻辑是这样的: 1.) 我从 y 轴 0 像素到 400 像素开始 2.) 我从 y 轴 400 像素到 800 像素开始 等等等等...
当前的迭代还没有达到 4298 并且它在提示(在 2400px)。有想过为什么会这样吗?
谢谢!
最佳答案
正如我从您的代码中看到的,问题出在:
Bitmap bitmap = Bitmap.createBitmap(comicBitmap, 0, i, width, i + everyPixel);
例如,您有 width = 1000, height = 3000
,但是everyPixel = 2000
。在周期的第二次迭代中,您将有 i = 2000
并且您将尝试通过获取 left = 0、top = 2000、width = 1000 和 height = i + everyPixel = 4000 的部分来从 Comicbitmap 创建位图,但总体位图的高度为 3000,因此您会收到此错误。
关于java - 分割位图文件会产生 y + height 必须 <= bitmap.height() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39343689/