BufferedImage java:颜色/alpha 分量的数量应为 4,但位数组的长度为 2

发布于 2024-10-31 16:33:29 字数 1814 浏览 0 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌梦深 2024-11-07 16:33:29

我将过滤器调用更改为 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 the createCompatibleDestImage.

新雨望断虹 2024-11-07 16:33:29

我想您应该将数组的尺寸设置为 65536 个元素,或者使用 ByteLookupTable。

I guess you should either dimension your arrays to 65536 elements, or use a ByteLookupTable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文