2016-04-08 1 views
0

J'essaie actuellement d'enregistrer un BufferedImage dans un fichier .png:Java d'écriture du fichier png rend tout transparent

for(int x = left.getBlockX(); x < (left.getBlockX() + height); x++){ 
       for(int z = left.getBlockZ(); z < (left.getBlockZ() + width); z++){ 
        pixels[i] = getBasicColor(new Location(left.getWorld(), x, left.getBlockY(), z)); 
        i++; 
       } 
      } 

Voici la fonction getBasicColor:

@SuppressWarnings("deprecation") 
public static int getBasicColor(Location location){ 
    if(location.getBlock().getType().equals(Material.WOOL)){ 
     Byte data = location.getBlock().getData(); 
     for(BasicColor basicColor : BasicColor.values()){ 
      if(data.equals(basicColor.getDyeColor().getData())){ 
       int rgb = 65536 * basicColor.getRed() + 256 * basicColor.getGreen() + basicColor.getBlue(); 
       System.out.println(rgb); 
       return rgb; 
      } 
     } 
    } 
    return 0; 
} 

Et voici les BasicColors:

public enum BasicColor { 

WHITE (255,255,255, DyeColor.WHITE), 
BLACK (0,0,0, DyeColor.BLACK), 
BLUE (0,0,255, DyeColor.BLUE), 
CYAN (0, 255, 255, DyeColor.CYAN), 
DARK_GRAY (169,169,169, DyeColor.GRAY), 
GRAY (128,128,128, DyeColor.GRAY), 
SILVER (192,192,192, DyeColor.SILVER), 
GREEN (0,128,0, DyeColor.GREEN), 
MAGENTA (255,0,255, DyeColor.MAGENTA), 
ORANGE (255, 165, 0, DyeColor.ORANGE), 
PINK (255,192,203, DyeColor.PINK), 
RED (255, 0, 0, DyeColor.RED), 
YELLOW (255,255,0, DyeColor.YELLOW); 

private final int red, blue, green; 
private final DyeColor color; 

private BasicColor(int red, int green, int blue, DyeColor color){ 
    this.red = red; 
    this.green = green; 
    this.blue = blue; 
    this.color = color; 
} 

public Integer getRed(){ 
    return red; 
} 

public Integer getBlue(){ 
    return blue; 
} 

public Integer getGreen(){ 
    return green; 
} 

public DyeColor getDyeColor(){ 
    return color; 
} 

}

Chaque fois que j'essaie d'enregistrer le fichier avec le code suivant:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
      WritableRaster raster = (WritableRaster) image.getData(); 
      raster.setPixels(0, 0, width, height, pixels); 
      image.setData(raster); 
      try { 
       ImageIO.write(image, "png", new File(name)); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

Je reçois des problèmes parce qu'au lieu d'avoir la bonne couleur je reçois soit une couleur très transparent ou aucune couleur.

Merci pour toute aide,

Lucas

+0

Je ne suis pas un expert, mais ce '' '' '' '' '' '' '' '' '' rgb = 65536 * basicColor.getRed() + 256 * basicColor.getGreen() + basicColor.getBlue(); System.out.println (rgb); 'semble mal. Les valeurs de rgb sont supposées être bit packed int, quelque chose comme [this] (http://stackoverflow.com/questions/2534116/how-to-convert-get-rgbx-y-integer-pixel-to-colorr-gba -in-java), mais c'est pourquoi j'utilise 'java.awt.Color' – MadProgrammer

Répondre

0

Je ne suis pas sûr du contexte du code, mais ne devrait pas être modifié x à la largeur et y être changé à la hauteur?

for(int x = left.getBlockX(); x < (left.getBlockX() + width); x++){ 
    for(int z = left.getBlockZ(); z < (left.getBlockZ() + height); z++){ 
     pixels[i] = getBasicColor(new Location(left.getWorld(), x, left.getBlockY(), z)); 
       i++; 
      } 
     } 
+0

Non, dans ce cas ce n'est pas grave, il lit les coordonnées à plat –

1

pixels sont ensemble correcty En supposant, utilisez ceci:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
image.setRGB(0, 0, width, height, pixels, 0, width); 

Pour vérifier que les pixels regarder ce que vous voulez l'image de la peinture dans certains composants. Si cela ne semble pas bien alors un problème existe dans vos couleurs. Imprimez basicColor.getRed() etc pour voir qu'ils ont raison.

+0

Merci beaucoup, j'ai travaillé! –