3
我正在试图使用BufferedImageOp
类来实现用于反转BufferedImage
中每个像素的蓝色部分的算法,如解释here所述。我尝试导致产生这种方法的:BufferedImageOp.filter()投掷通道错误
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);*/
}
但是,我得到的时候我打电话给我的BufferedImageOp
类的.filter方法与渠道或字节数错误。上面的代码中的注释的部分产生这些相应的错误:
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)
在链接代码非常老,(这是写:
错误#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在1998年!),所以我认为自那时以来有所改变,这就是为什么代码不再起作用。但是,我一直无法找到解释这个概念的另一个来源,这是我的主要关注点。
任何人都可以解释这些错误是什么意思,以及如何解决它们?或者更好的是,将我指向一个更新,但仍然彻底的如何操作图像的教程?