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 に答える