在android中分割位图时出现java.lang.IllegalArgumentException

发布于 2024-10-21 00:51:08 字数 1175 浏览 1 评论 0原文

有人可以告诉我为什么在分割位图时会出现此错误。 代码:

 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()

Image I'm splitting

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天暗了我发光 2024-10-28 00:51:08

您试图抓取原始位图中不存在的部分。

在该行处放置一个断点:

scambledImage.add(Bitmap.createBitmap(bMapScaled, xCoord, yCoord, finalSize,  finalSize));  

您会看到它在第一次数组迭代之后的某个时间失败,因为每次偏移您通过 xCoord/yCoord 抓取的大地图“切片”的起点。

我的直觉表明您对 FinalSize 的计算是错误的,但我只能推测,因为我们不确切知道您想要完成什么。

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.

美人骨 2024-10-28 00:51:08

我已经尝试过你的代码并进行了一些更改,它对我有用。

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文