【问题标题】:java.lang.IllegalArgumentException when splitting bitmap in android在android中拆分位图时出现java.lang.IllegalArgumentException
【发布时间】:2011-03-08 16:13:05
【问题描述】:

谁能告诉我为什么在拆分位图时会出现此错误。 代码:

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

Image I'm splitting

谢谢!

【问题讨论】:

    标签: android bitmap android-image


    【解决方案1】:

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

    在该行设置断点:

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

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

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

    【讨论】:

    • 感谢您的回复,我正在尝试将图像拆分为 6 x 6 网格。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 2011-10-17
      • 2023-04-01
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      相关资源
      最近更新 更多