简体   繁体   English

BufferedImageOp.filter()抛出通道错误

[英]BufferedImageOp.filter() Throwing Channel Errors

I'm trying to implement the algorithm used to invert the blue portion of each pixel in a BufferedImage using the BufferedImageOp class, as explained here . 我试图实现用于反转在每个像素的蓝色部分算法BufferedImage使用BufferedImageOp类,如解释在这里 My attempt resulted in the creation of this method: 我的尝试导致了这种方法的创建:

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);*/
}

However, I'm getting errors related to the number of channels or bytes when I call the .filter method of my BufferedImageOp class. 但是,当我调用BufferedImageOp类的.filter方法时,我收到与通道数或字节数相关的错误。 The commented sections of code above produce these respective errors: 上面代码的注释部分产生了以下相应的错误:

Error #1: 错误#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)

Error #2: 错误#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)

The code in the link is very old, (it was written in 1998!) so I assume something has changed since then, which is why the code no longer works. 链接中的代码非常古老,(它是在1998年写的!)所以我假设从那以后发生了一些变化,这就是代码不再有效的原因。 However, I haven't been able to find another source that explains the concept nearly as well, which is a primary concern of mine. 但是,我还没有能够找到另外一个解释这个概念的来源,这是我的主要关注点。

Can anyone explain what these errors mean and how to fix them? 任何人都可以解释这些错误意味着什么以及如何解决它们? Or better yet, point me to a more up-to-date, but still thorough, tutorial on how to manipulate images? 或者更好的是,请指出一个关于如何操作图像的更新,但仍然详尽的教程?

I ran into the same thing... and the answer is to create the destination image with the color model that you are seeking. 我遇到了同样的事情......答案是使用您正在寻找的颜色模型创建目标图像。 Also, the short[][] data needs to contain a dimension for the alpha channel as well. 此外,short [] []数据也需要包含alpha通道的维度。 Here's my example of working code that I stumbled upon through trial and error: 这是我通过反复试验偶然发现的工作代码示例:

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

This worked for me. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM