3

ここで説明するように、クラスBufferedImageを使用して各ピクセルの青い部分を反転するために使用されるアルゴリズムを実装しようとしています。私の試みの結果、このメソッドが作成されました。BufferedImageOp

private BufferedImage getInvertedVersion(BufferedImage source) {
     short[] invert = new short[256];
     short[] straight = new short[256];
     for (int i = 0; i < 256; i++) {
        invert[i] = (short)(255 - i);
        straight[i] = (short)i;
     }

     short[][] blueInvert = new short[][] { straight, straight, invert }; //Red stays the same, Green stays the same, Blue is inverted
     BufferedImageOp blueInvertOp = new LookupOp(new ShortLookupTable(0, blueInvert), null);

     //This produces error #1 when uncommented
     /*blueInvertOp.filter(source, source);
     return source;*/

     //This produces error #2 instead when uncommented
     /*return blueInvertOp.filter(source, null);*/
}

ただし、クラスの.filterメソッドを呼び出すと、チャネル数またはバイト数に関連するエラーが発生しますBufferedImageOp。上記のコードのコメント付きセクションでは、それぞれのエラーが発生します。

エラー#1:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Number of channels in the src (4) does not match number of channels in the destination (2)
at java.awt.image.LookupOp.filter(LookupOp.java:273)
at java.awt.image.LookupOp.filter(LookupOp.java:221)

エラー#2:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Number of color/alpha components should be 4 but length of bits array is 2
at java.awt.image.ColorModel.<init>(ColorModel.java:336)
at java.awt.image.ComponentColorModel.<init>(ComponentColorModel.java:273)
at java.awt.image.LookupOp.createCompatibleDestImage(LookupOp.java:413)

リンクのコードは非常に古いので(1998年に書かれました!)、それ以降に何かが変更されたと思います。そのため、コードは機能しなくなりました。しかし、私の主な関心事である概念をほぼ同様に説明する別の情報源を見つけることができませんでした。

これらのエラーの意味と修正方法を誰かが説明できますか?または、さらに良いことに、画像を操作する方法についての、より最新でありながら徹底的なチュートリアルを教えてください。

4

1 に答える 1

6

私は同じことに遭遇しました...そして答えは、あなたが求めているカラーモデルで目的地のイメージを作成することです. また、short[][] データには、アルファ チャネルのディメンションも含める必要があります。試行錯誤の末にたどり着いた作業コードの例を次に示します。

short[] red = new short[256];
short[] green = new short[256];
short[] blue = new short[256];
short[] alpha = new short[256];

for (short i = 0; i < 256; i++) {
  green[i] = blue[i] = 0;
  alpha[i] = red[i] = i;
}
short[][] data = new short[][] {
    red, green, blue, alpha
};

LookupTable lookupTable = new ShortLookupTable(0, data);
LookupOp op = new LookupOp(lookupTable, null);
BufferedImage destinationImage = new BufferedImage(24, 24, BufferedImage.TYPE_INT_ARGB);
destinationImage = op.filter(sourceImage, destinationImage);

これは私にとってはうまくいきました。

于 2012-11-20T05:07:25.857 に答える