BufferedImage java:颜色/alpha 分量的数量应为 4,但位数组的长度为 2
我想在 java 中对 BufferedImage 进行简单的颜色替换。
这是我的代码:
public BufferedImage convertEDEDEDtoRGB(BufferedImage bufferedImage,
short nr, short ng, short nb){
short[] r = new short[256];
short[] g = new short[256];
short[] b = new short[256];
short[] a = new short[256]; //leave a the same
for(short i=0; i<r.length; i++) {
r[i]=i;
g[i]=i;
b[i]=i;
a[i]=i;
}
System.out.println("componentSize "+bufferedImage.getColorModel().getComponentSize());
for(int i = 0; i<bufferedImage.getColorModel().getComponentSize().length; i++)
System.out.println("rgb ("+i+") " +bufferedImage.getColorModel().getComponentSize(i) ); //gives 8 bits
System.out.println("num Components "+bufferedImage.getColorModel().getNumColorComponents()); // gives 3
r[237] = nr;
g[237] = ng;
b[237] = nb;
short[][] ededed2rgb = new short[][]{r, g, b, a};
ShortLookupTable t = new ShortLookupTable(0, ededed2rgb);
LookupOp ededed2rgbOp = new LookupOp(t, null);
bufferedImage = ededed2rgbOp.filter(bufferedImage, null);
return bufferedImage;
}
这会产生以下错误,我只是没有看到我错过了什么:
Number of color/alpha components should be 4 but length of bits array is 2
at java.awt.image.ColorModel.<init>(ColorModel.java:318)
at java.awt.image.ComponentColorModel.<init>(ComponentColorModel.java:256)
at java.awt.image.LookupOp.createCompatibleDestImage(LookupOp.java:396)
at java.awt.image.LookupOp.filter(LookupOp.java:136)
at nl.sytematic.projects.myproj.server.custom.MyServlet.convertEDEDEDtoRGB(GenerateFlavourImageServlet.java:225)
我尝试过
new short[][] { r,g,b}
,但弹出了相同的错误。有什么想法吗?顺便说一句,我正在加载 .png 图像(具有透明度)。
I want to do a simple color replace on a BufferedImage, in java.
Here is my code:
public BufferedImage convertEDEDEDtoRGB(BufferedImage bufferedImage,
short nr, short ng, short nb){
short[] r = new short[256];
short[] g = new short[256];
short[] b = new short[256];
short[] a = new short[256]; //leave a the same
for(short i=0; i<r.length; i++) {
r[i]=i;
g[i]=i;
b[i]=i;
a[i]=i;
}
System.out.println("componentSize "+bufferedImage.getColorModel().getComponentSize());
for(int i = 0; i<bufferedImage.getColorModel().getComponentSize().length; i++)
System.out.println("rgb ("+i+") " +bufferedImage.getColorModel().getComponentSize(i) ); //gives 8 bits
System.out.println("num Components "+bufferedImage.getColorModel().getNumColorComponents()); // gives 3
r[237] = nr;
g[237] = ng;
b[237] = nb;
short[][] ededed2rgb = new short[][]{r, g, b, a};
ShortLookupTable t = new ShortLookupTable(0, ededed2rgb);
LookupOp ededed2rgbOp = new LookupOp(t, null);
bufferedImage = ededed2rgbOp.filter(bufferedImage, null);
return bufferedImage;
}
This yields the following error, and I just do not see what I am mising:
Number of color/alpha components should be 4 but length of bits array is 2
at java.awt.image.ColorModel.<init>(ColorModel.java:318)
at java.awt.image.ComponentColorModel.<init>(ComponentColorModel.java:256)
at java.awt.image.LookupOp.createCompatibleDestImage(LookupOp.java:396)
at java.awt.image.LookupOp.filter(LookupOp.java:136)
at nl.sytematic.projects.myproj.server.custom.MyServlet.convertEDEDEDtoRGB(GenerateFlavourImageServlet.java:225)
I tried with
new short[][] { r,g,b}
but the same error pops up. Any thoughts? I am loading a .png image (with transparency) by the way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将过滤器调用更改为 ededed2rgbOb.filter(bufferedImage, bufferedImage) ,错误消失了。通过检查链接中的代码发现了这一点。提供第二个参数可以有效绕过
createCompatibleDestImage
。I changed the filter call into
ededed2rgbOb.filter(bufferedImage, bufferedImage)
and the error is gone. Found this out by inspecting the code at link. Supplying a second parameter effectively bypasses thecreateCompatibleDestImage
.我想您应该将数组的尺寸设置为 65536 个元素,或者使用 ByteLookupTable。
I guess you should either dimension your arrays to 65536 elements, or use a ByteLookupTable.