1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
   |  
 public class LireFichierBMP {
 //Constantes
 private static final int MASK = 0xFF;/**Constante de masque pour la comparaison bit à bit*/
 
 
 public static  BufferedImage readBMP2(String filename) {
DataInputStream inBMP;
 try{
 inBMP = new DataInputStream(new FileInputStream(new File(filename)));
 }
 catch(Exception e){
		System.out.print(e.getMessage()); 
	 return null;
	 }
 //On va lire le début de l'entête
 try{
 inBMP.skipBytes(18);
 
 //Maintenant, on lit la largeur et la hauteur de l'image
 int width = readInt(inBMP);
 int height = readInt(inBMP); 
 
 //On saute les données inutiles del'entête
 inBMP.skipBytes(28);
 
 //Nous allons remplir le BufferedImage
 BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
 int sup = (width * 3) % 4;
 //Lecture des données
 for(int y = 0; y < height; y++){
 for(int x = 0; x < width; x++){
 //Récupération des couleurs
 img.setRGB(x, y, readColor(inBMP));
 }
 //On saute le bourrage
 inBMP.skipBytes(sup);
 }
 inBMP.close();
 return img;
 }
 catch(Exception e){
	System.out.print("erreur fichier"); 
 return null;
 }
 }
   private static int readColor(DataInputStream in){
	  byte[] b = new byte[3];
	  int result = 0;
 
	  try{
	  in.read(b);
	  result = b[0] & MASK;
	  result = result + ((b[1] & MASK) << 8);
	  result = result + ((b[2] & MASK) << 16);
	  }
	  catch(Exception e){
 
	  }
 
	  return result;
	  } 
 
   private static int readInt(DataInputStream in){
	 byte[] b = new byte[4];
	  int result = 0;
 
	  try{
	  in.read(b);
	  result = b[0] & MASK;
	  result = result + ((b[1] & MASK) << 8);
	  result = result + ((b[2] & MASK) << 16);
	  result = result + ((b[3] & MASK) << 24);
 
	  }
	  catch(Exception e){
 
	  }
 
	  return result;
	  } 
 
   public static int[][] contour(BufferedImage src) {
	   ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
	   ColorConvertOp op = new ColorConvertOp(cs,null);
	   op.filter(src, null);
	  // Definition du masque de convolution utilisé pour la détéction des contours de
		// l'image
		float[] mask = { 0f, -1f, 0f, -1f, 4.1f, -1f, 0f, -1f, 0f};
		Kernel kernel = new Kernel(3, 3, mask);
		// On creer notre outils de convolution
		ConvolveOp convo = new ConvolveOp(kernel);
		// On effectue la convolution
		BufferedImage dst = convo.filter(src, null);
		  int[][] reponsef = new int[dst.getWidth()][dst.getHeight()];
		for(int y = 0; y < dst.getHeight(); y++){
			 for(int x = 0; x < dst.getWidth(); x++){			 
			if(dst.getRGB(x, y)>=-15132391)//img.getRGB(x, y);//readColor(inBMP);
				 reponsef[x][y]= 1;
	        else
	        	reponsef[x][y]= 0;
			 }		
		}
			 return reponsef;
	}
 
 
      public static void main(String[] args) {
		// TODO Auto-generated method stub
    	 BufferedImage bi = LireFichierBMP.readBMP2("test4.bmp");
    	int[][] res =  LireFichierBMP.contour(bi);
		for(int y = 0; y < bi.getHeight(); y++){
			 for(int x = 0; x < bi.getWidth(); x++){
				if(res[x][y]==0)
				System.out.print(res[x][y]+" ");
				else
					System.out.print(" ");
			 }
			 System.out.print("\n"); 
		}
 
	}
 
 } | 
Partager