2010-02-14 5 views

Répondre

2

J'ai trouvé la solution, la voici pour ceux qui sont intéressés.

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

Oui, cela fonctionnera, mais il ne changera pas les couleurs qui sont même légèrement noir, ce qui signifie que toute image qui est un alias finira par être plutôt moche. – funkybro

1

Vous pouvez analyser l'image RGBs recherche de la couleur noire et la remplacer par la couleur que vous désirez.

+0

Merci Ali .... dans le prochain post, vous trouverez la mise en œuvre. –

+0

Vous êtes les bienvenus, et une belle mise en œuvre. –

1

Vous pouvez lire votre image PNG pour créer un tableau d'octets et éditer un fragment de palette. Cette méthode convient uniquement aux images PNG-8. Voici mon 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); 
     } 
    } 
 
+0

pourriez-vous expliquer pourquoi vous incrémentez le décalage? Et quel est le but du paramètre offset? –

+0

'offset' est juste une position actuelle dans un fichier - nous lisons le flux et l'écrivons dans le tableau à la position actuelle. Mais ce code a 3 ans - maintenant il me semble qu'il vaudrait mieux lire tout le fichier dans le tableau puis éditer ce tableau :) –

Questions connexes