1

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

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

我正在拆分的图像

谢谢!

4

2 回答 2

1

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

在该行放置一个断点:

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

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

我的直觉是你对 finalSize 的计算是错误的,但我只能推测,因为我们不知道你想要完成什么。

于 2011-03-08T16:44:14.527 回答
0

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

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;
}
于 2012-02-13T12:49:03.167 回答