如何以编程方式更改图像中的颜色?

发布于 2024-08-22 01:11:23 字数 97 浏览 3 评论 0原文

我有一个具有透明背景的 .PNG 图像,其中有一个黑色绘图,我如何以编程方式将此图像中的“黑色绘图颜色”更改为我想要的任何颜色;使用 rim 4.5 API ? 提前致谢 ....

I have a .PNG image with a transparent background and a drawing in it with a black color, how could I change the "black drawing color" in this image to any color i want programmatically; using rim 4.5 API ?
THANKS IN ADVANCE ....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

萌化 2024-08-29 01:11:23

我找到了解决方案,这里是为有兴趣的人提供的。

Bitmap colorImage(Bitmap image, int color) {
    int[] rgbData= new int[image.getWidth() * image.getHeight()];
    image.getARGB(rgbData, 
                  0, 
                  image.getWidth(), 
                  0, 
                  0, 
                  image.getWidth(), 
                  image.getHeight());
    for (int i = 0; i < rgbData.length; i++) {
        int alpha = 0xFF000000 & rgbData[i];
        if((rgbData[i] & 0x00FFFFFF) == 0x00000000)
            rgbData[i]= alpha | color;
    }
    image.setARGB(rgbData,
                  0, 
                  image.getWidth(),
                  0,
                  0,
                  image.getWidth(),
                  image.getHeight());
    return image;
}

I found the solution, here it is for those who are interested.

Bitmap colorImage(Bitmap image, int color) {
    int[] rgbData= new int[image.getWidth() * image.getHeight()];
    image.getARGB(rgbData, 
                  0, 
                  image.getWidth(), 
                  0, 
                  0, 
                  image.getWidth(), 
                  image.getHeight());
    for (int i = 0; i < rgbData.length; i++) {
        int alpha = 0xFF000000 & rgbData[i];
        if((rgbData[i] & 0x00FFFFFF) == 0x00000000)
            rgbData[i]= alpha | color;
    }
    image.setARGB(rgbData,
                  0, 
                  image.getWidth(),
                  0,
                  0,
                  image.getWidth(),
                  image.getHeight());
    return image;
}
長街聽風 2024-08-29 01:11:23

您可以解析图像 RGB,搜索黑色并将其替换为您想要的任何颜色。

You can parse the image RGBs searching for the black color and replace it with whatever color you desire.

趁年轻赶紧闹 2024-08-29 01:11:23

您可以将 PNG 图像读取为字节数组并编辑调色板块。
此方法仅适用于 PNG-8 图像。
这是我的代码:


public static Image createImage(String filename) throws Throwable
    {
        DataInputStream dis = null;
        InputStream is = null;

        try {
            is = new Object().getClass().getResourceAsStream(filename);
            dis = new DataInputStream(is);

            int pngLength = dis.available();
            byte[] png = new byte[pngLength];
            int offset = 0;
            dis.read(png, offset, 4);   offset += 4;    //‰PNG  
            dis.read(png, offset, 4);   offset += 4;    //....
            while (true) {
                //length
                dis.read(png, offset, 4);   offset += 4;
                int length = (png[offset-1]&0xFF) | ((png[offset-2]&0xFF)<<8) | ((png[offset-3]&0xFF)<<16) | ((png[offset-4]&0xFF)<<24);
                //chunk type
                dis.read(png, offset, 4);   offset += 4;
                int type = (png[offset-1]&0xFF) | ((png[offset-2]&0xFF)<<8) | ((png[offset-3]&0xFF)<<16) | ((png[offset-4]&0xFF)<<24);

                //chunk data
                for (int i=0; i<length; i++) {
                    dis.read(png, offset, 1);   offset += 1;
                }
                //CRC
                dis.read(png, offset, 4);   offset += 4;
                int crc = (png[offset-1]&0xFF) | ((png[offset-2]&0xFF)<<8) | ((png[offset-3]&0xFF)<<16) | ((png[offset-4]&0xFF)<<24);

                if (type == 0x504C5445) {   //'PLTE'
                    int CRCStart = offset-4;
                    int PLTEStart = offset-4-length;

                    //modify PLTE chunk
                    for (int i=PLTEStart; i<PLTEStart+length; i+=3) {
                        png[i+0] = ...
                        png[i+1] = ...
                        png[i+2] = ...
                    }

                    int newCRC = crc(png, PLTEStart-4, length+4);
                    png[CRCStart+0] = (byte)(newCRC>>24);
                    png[CRCStart+1] = (byte)(newCRC>>16);
                    png[CRCStart+2] = (byte)(newCRC>>8);
                    png[CRCStart+3] = (byte)(newCRC);

                }
                if (offset >= pngLength)
                    break;
            }

            return Image.createImage(png, 0, pngLength);
        } catch (Throwable e) {
            throw e;
        } finally {
            MainCanvas.closeInputStream(dis);
            MainCanvas.closeInputStream(is);
        }
    }

You can read your PNG image to byte array and edit palette chunk.
This method is suitable only for PNG-8 images.
Here is my code:


public static Image createImage(String filename) throws Throwable
    {
        DataInputStream dis = null;
        InputStream is = null;

        try {
            is = new Object().getClass().getResourceAsStream(filename);
            dis = new DataInputStream(is);

            int pngLength = dis.available();
            byte[] png = new byte[pngLength];
            int offset = 0;
            dis.read(png, offset, 4);   offset += 4;    //‰PNG  
            dis.read(png, offset, 4);   offset += 4;    //....
            while (true) {
                //length
                dis.read(png, offset, 4);   offset += 4;
                int length = (png[offset-1]&0xFF) | ((png[offset-2]&0xFF)<<8) | ((png[offset-3]&0xFF)<<16) | ((png[offset-4]&0xFF)<<24);
                //chunk type
                dis.read(png, offset, 4);   offset += 4;
                int type = (png[offset-1]&0xFF) | ((png[offset-2]&0xFF)<<8) | ((png[offset-3]&0xFF)<<16) | ((png[offset-4]&0xFF)<<24);

                //chunk data
                for (int i=0; i<length; i++) {
                    dis.read(png, offset, 1);   offset += 1;
                }
                //CRC
                dis.read(png, offset, 4);   offset += 4;
                int crc = (png[offset-1]&0xFF) | ((png[offset-2]&0xFF)<<8) | ((png[offset-3]&0xFF)<<16) | ((png[offset-4]&0xFF)<<24);

                if (type == 0x504C5445) {   //'PLTE'
                    int CRCStart = offset-4;
                    int PLTEStart = offset-4-length;

                    //modify PLTE chunk
                    for (int i=PLTEStart; i<PLTEStart+length; i+=3) {
                        png[i+0] = ...
                        png[i+1] = ...
                        png[i+2] = ...
                    }

                    int newCRC = crc(png, PLTEStart-4, length+4);
                    png[CRCStart+0] = (byte)(newCRC>>24);
                    png[CRCStart+1] = (byte)(newCRC>>16);
                    png[CRCStart+2] = (byte)(newCRC>>8);
                    png[CRCStart+3] = (byte)(newCRC);

                }
                if (offset >= pngLength)
                    break;
            }

            return Image.createImage(png, 0, pngLength);
        } catch (Throwable e) {
            throw e;
        } finally {
            MainCanvas.closeInputStream(dis);
            MainCanvas.closeInputStream(is);
        }
    }

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