2010-02-14 206 views

回答

2

我找到了解决方案,在这里它是为那些有兴趣的人。

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; 
} 
+0

是的,它可以工作,但它不会改变任何颜色,甚至有点偏黑,这意味着任何别名的图像最终都会看起来很丑。 – funkybro 2010-02-15 09:12:52

1

您可以解析图像RGB搜索黑色,并用任何你想要的颜色取代它。

+0

谢谢阿里....在下一篇文章中,你会发现实现。 – 2010-02-14 10:14:38

+0

不客气,而且很好的实现。 – 2010-02-14 10:48:42

1

您可以将您的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); 
     } 
    } 
 
+0

你能解释为什么你增加了抵消?偏移参数的目的是什么? – 2013-01-24 16:08:13

+0

'offset'只是文件中的当前位置 - 我们读取流并将其写入当前位置的数组。但是这段代码已经3年了 - 现在看来,将整个文件读入数组然后编辑这个数组会更好:) – 2013-01-24 16:21:13

相关问题