1

バッファリングされた画像用の1Dバイト配列があります.2Dバイト配列に変換したいので、以下のようにコードを書きました

File file = new File("/home/tushar/temp.jpg");
try {
        input_bf = ImageIO.read(file);
        width = input_bf.getWidth();
        height = input_bf.getHeight();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
byte [][] image = new byte[width][height];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
        ImageIO.write(input_bf, "jpg", bos );
        bos.flush();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

byte[] imageInByte = bos.toByteArray();
        try {
            bos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


//here is the main logic to convert 1D to 2D
int x=0;
for(int i=0;i<width;i++)
{
    for(int j=0;j<height;j++)
    {
        image[i][j] = imageInByte[x];
        x++;
    }
}

しかし、私は次のような例外を受け取ります

java.lang.ArrayIndexOutOfBoundsException: 26029
    at smoothing.main(smoothing.java:70)

1D 配列のサイズは 26029 で、例外を示しています。

今、私は何をすべきですか?

1D を 2D 画像配列に変換する方法は?

または、画像を2D配列に変換する方法を知っている人はいますか?

4

1 に答える 1

2

ByteArrayOutputStreamuseを使用する代わりにDataBufferByte、それは機能します。

DataBufferByte db = (DataBufferByte)image.getRaster().getDataBuffer();

            byte[] pixelarray = db.getData();

次に、1D配列を2D配列に変換するロジックを適用します

これにより、正しい画像サイズが得られ、例外が回避されます。

于 2013-09-03T08:05:38.490 に答える