gpt4 book ai didi

java - 缓冲图像 java : Number of color/alpha components should be 4 but length of bits array is 2

转载 作者:行者123 更新时间:2023-11-29 06:14:25 24 4
gpt4 key购买 nike

我想在 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 图像(透明)。

最佳答案

我将过滤器调用更改为 ededed2rgbOb.filter(bufferedImage, bufferedImage) 并且错误消失了。通过检查链接中的代码发现了这一点。提供第二个参数可以有效地绕过 createCompatibleDestImage

关于java - 缓冲图像 java : Number of color/alpha components should be 4 but length of bits array is 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5620053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com