2013-04-05 2 views
1

J'essaie de lire et d'écrire une image en niveaux de gris en utilisant BufferedImage. Mais les pixels dans les images d'écriture sont un peu différents de sa source. Je ne suis pas bon avec ce truc de traitement d'image. S'il vous plaît, aidez-moi à trouver l'erreur que j'ai faite dans mon code.Lire et écrire des images en pixels en Java

Voici mon code

File InImage=new File("source.jpg"); 
File OutImage=new File("out.jpg"); 

/* code for reading the image*/ 
BufferedImage image=ImageIO.read(InImage); 
WritableRaster raster=image.getRaster(); 

/* writing the same raster to a new image */ 
BufferedImage newImg=new BufferedImage(raster.getWidth(), raster.getHeight(),BufferedImage.TYPE_BYTE_GRAY); 
       newImg.setData(raster); 
ImageIO.write(newImg, "jpg", OutImage); 
+0

Pouvez-vous montrer les images source et de sortie? Remmber Jpg est un format avec perte, donc en décompressant et en recompressant la même image peut entraîner une image différente surtout si les paramètres de compression sont différents – Sibster

Répondre

0

JPEG est un format lossy, lire et écrire en va perdre des informations de l'image il va être différent chaque fois que vous enregistrez.

Pour éviter cela, utilisez un format sans perte tel que PNG.

+0

merci qui a résolu le problème – adithyan

1

Je suis presque sûr que l'image originale n'est pas de type TYPE_BYTE_GRAY. Je vous suggère de sortie le même type d'image que l'entrée un:

BufferedImage newImg = new BufferedImage(raster.getWidth(), raster.getHeight(), image.getType()); 
0

Voilà comment je mess avec images essayer, son conçu pour les bitmaps, mais vous pouvez le modifier pour faire jpg et png. il les charge à un tableau d'octets pour la manipulation. c'est la version la plus de travail de base que je peux trouver comme je l'ai développé davantage depuis

import java.awt.image.BufferedImage; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 


public class ImageEdit 
{ 

    /** 
    * @param args 
    * @throws IOException 
    */ 
    public static void main(String[] args) throws IOException 
    { 
     // TODO Auto-generated method stub 
     //loadBitmapToBytes("icon","resources"); 
     //WriteByteToBitmapFile("foo", "food", null); 
    } 



    public static byte [] loadBitmapToBytes (String filename, String dir) throws IOException 
    { 
     String a = "\\\\"; 
     String b = ".bmp"; 
     String readDir = dir + a + filename + b; 
     BufferedImage image = ImageIO.read(new File(readDir)); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     ImageIO.write(image, "bmp", baos); 
     byte[] img=baos.toByteArray(); 
     return img; 
    } 

    public static void writeByteToBitmapFile (String filename, String dir, byte [] img) throws IOException 
    { 
     String a = "/"; 
     String b = ".bmp"; 
     String writeDir = dir + a + filename + b; 
     BufferedImage imageA = ImageIO.read(new ByteArrayInputStream(img)); 
     ImageIO.write(imageA, "BMP", new File(writeDir)); 
     System.out.println(writeDir); 
    } 

    public static byte [] invertBitmap(byte[] img) 
    { 
     int indexNo = 54; 

     for(int i = 54; i < (img.length-54)/3; i++) 
     { 
      img[indexNo] = (byte) (255-img[indexNo]); 
      indexNo++; 
      img[indexNo] = (byte) (255-img[indexNo]); 
      indexNo++; 
      img[indexNo] = (byte) (255-img[indexNo]); 
      indexNo++; 
     } 
     return img; 
    } 

    public static byte [] invertSingleBitmapChannel(char chanel, byte [] img) 
    { 
     String a = "Color Chanel bad Input"; 
     int indexNo = 54; 
     for (int i = 54; i < (img.length-54)/3; i++, indexNo++) 
     { 
      if (chanel == 'B' || chanel == 'b') 
      { 

       img[indexNo] = (byte) (255-img[indexNo]); 
       indexNo+=2; 

      } 
      else if (chanel == 'G' || chanel == 'g') 
      { 
       indexNo++; 
       img[indexNo] = (byte) (255-img[indexNo]); 
       indexNo++; 
      } 
      else if (chanel == 'R' || chanel == 'r') 
      { 
       indexNo+=2; 
       img[indexNo] = (byte) (255-img[indexNo]); 
      } 
      else 
      { 
       System.out.println(a); 
       return null; 
      } 
     } 
     return img; 

    } 

    public static byte [] greyFromBitmapColor(char fromColor, byte [] img) 
    { 
     int temp; 
     int indexNo = 54; 
     String a = "Color Chanel bad Input"; 
     for(int i = 54; i < (img.length-54)/3; i++, indexNo++) 
     { 
      if (fromColor == 'B' || fromColor == 'b') 
      { 
       temp = img[indexNo+2]; 
       img[indexNo] = (byte) temp; 
       indexNo++; 
       img[indexNo] = (byte) temp; 
       indexNo++; 
      } 
      else if (fromColor == 'G' || fromColor == 'g') 
      { 
       temp = img[indexNo+1]; 
       img[indexNo] = (byte) temp; 
       indexNo+=2; 
       img[indexNo] = (byte) temp; 
      } 
      else if (fromColor == 'R' || fromColor == 'r') 
      { 
       temp = img[indexNo]; 
       indexNo++; 
       img[indexNo] = (byte) temp; 
       indexNo++; 
       img[indexNo] = (byte) temp; 
      } 
      else 
      { 
       System.out.println(a); 
       return null; 
      } 
     } 

     return img; 
    } 

    public static byte [] greyFromBitmapAverage(byte [] img) 
    { 
     int temp; 
     int indexNo = 54; 
     for(int i = 54; i < (img.length-54)/3; i++, indexNo++) 
     { 
      temp = (img[indexNo]+img[indexNo+1]+img[indexNo+2])/3; 
      img[indexNo] = (byte) temp; 
      indexNo++; 
      img[indexNo] = (byte) temp; 
      indexNo++; 
      img[indexNo] = (byte) temp; 
     } 

     return img; 
    } 

    public static byte [] removeChannelFromBitmap(char lostColor, byte [] img) 
    { 
     String a = "Color Chanel bad Input"; 
     int indexNo = 54; 
     for(int i = 54; i < (img.length-54)/3; i++, indexNo++) 
     { 
      if (lostColor == 'B' || lostColor == 'b') 
      { 

       img[indexNo] = 0; 
       indexNo+=2; 

      } 
      else if (lostColor == 'G' || lostColor == 'g') 
      { 
       indexNo++; 
       img[indexNo] = 0; 
       indexNo++; 
      } 
      else if (lostColor == 'R' || lostColor == 'r') 
      { 
       indexNo+=2; 
       img[indexNo] = 0; 
      } 
      else 
      { 
       System.out.println(a); 
       return null; 
      } 
     } 

     return img; 
    } 

    public static byte [] greyFromDominantBitmapColor (byte [] img) 
    { 
     int indexNo = 54; 
     long r = 0; 
     long g = 0; 
     long b = 0; 
     char dom = 'b'; 

     for(int i = 54; i < (img.length-54)/3; i++, indexNo++) 
     { 
       b += img[indexNo]; 
       indexNo++; 
       g += img[indexNo]; 
       indexNo++; 
       r += img[indexNo]; 
     } 

     if (b > 0) 
      dom = 'b'; 
     if (g > b) 
      dom = 'g'; 
     if (r > g) 
      dom = 'r'; 

     greyFromBitmapColor(dom, img); 
     return img; 
    } 

} 

et est ici la classe de test

import java.io.IOException; 

public class TestApp { 

    /** 
    * @param args 
    * @throws IOException 
    */ 
    public static void main(String[] args) throws IOException 
    { 
     byte [] img = ImageEdit.loadBitmapToBytes("fooInv","resources"); 
     //ImageEdit.invertBitmap(img); 
     //ImageEdit.greyFromBitmapColor('r',img); 
     ImageEdit.greyFromDominantBitmapColor(img); 
     //ImageEdit.invertBitmap(img); 
     //ImageEdit.writeByteToBitmapFile("fooInv", "resources", img); 

     System.out.println("Done!"); 
    } 
} 
1
import java.awt.image.BufferedImage; 
import java.awt.image.Raster; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 


public class ToGray { 

    public static void main(String[] args) throws IOException { 
     File file = new File("f:/lion.jpg"); 
     BufferedImage img = ImageIO.read(file); 
     int width = img.getWidth(); 
     int height = img.getHeight(); 
     int[][] pixel = new int[width][height]; 
     Raster raster = img.getData(); 
     for (int i = 0; i < width; i++) { 
      for (int j = 0; j < height; j++) { 
       pixel[i][j] = raster.getSample(i, j, 0); 
      } 
     } 

     BufferedImage theImage = new BufferedImage(width, height, 
       BufferedImage.TYPE_INT_RGB); 
     for (int i = 0; i < width; i++) { 
      for (int j = 0; j < height; j++) { 
       int value = pixel[i][j] << 16 | pixel[i][j] << 8 
         | pixel[i][j]; 
       theImage.setRGB(i, j, value); 
      } 
     } 

     File outputfile = new File("f:/op.png"); 
     try { 
      ImageIO.write(theImage, "png", outputfile); 
     } catch (IOException e1) { 

     } 
} 
} 

Ce code est juste un échantillon pour lire et écrire images qui sont l'échelle de gris.Ici j'ai utilisé raster pour lire l'image et définir rgb pour écrire l'image. Vous pouvez également utiliser get et setRGB pour lire et écrire une image. Le format de l'image écrite doit être soit bmp, gif, png etc .. Depuis jpg, jpeg sont une compression avec perte après l'écriture dans ces formats, nous pouvons obtenir une image noire complète. En voyant la valeur tableau de l'image, nous pouvons obtenir 0 ou 1 ou 2 valeurs comme intensité de pixel.Par conséquent, utilisez png.

+0

Cela fonctionne. Merci beaucoup. Mais pourquoi les images sauvegardées sont toutes en noir et blanc? –