title: How to read a image in java and turn it to byte array?[use Lena.bmp 512x512 as example]
body: <p>I want to analysis the pixel of lena.bmp (matlab)
  but I don't know how to do it in java and store it to byte[]</p>
  
  <p>I have read this aritle:</p>
  
  <p><a href="https://stackoverflow.com/questions/3211156/how-to-convert-image-to-byte-array-in-java">how to convert image to byte array in java?</a> </p>
  
  <p>But when I implement, I find some<code> pixel value that did't exist</code>.
  For example , the pixel range is 0~255,
  but I can not find '120' on pixel of this photo(lena.bmp).</p>
  
  <p>There is my code</p>
  
  <pre><code>/*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package Image_IO;
  
  import java.awt.image.BufferedImage;
  import java.io.ByteArrayOutputStream;
  import java.io.File;
  import java.io.IOException;
  import javax.imageio.ImageIO;
  
  /**
   *
   * @author user
   */
  public class ReadImage {
      public static void main (String []args){
          byte[] imageInByte;
          int [] kindOfPixel = new int [256];
          try{
              BufferedImage originalImage = ImageIO.read(new File("C:\\Users\\user\\Desktop\\Project\\LSB_implement\\Lena.bmp"));
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              ImageIO.write( originalImage, "bmp", baos );
              baos.flush();
              imageInByte = baos.toByteArray();
              System.out.println(imageInByte.length);
              baos.close();
  
              for(int i=0;i&lt;imageInByte.length;i++){
                  kindOfPixel[imageInByte[i]+128]++;  //java byte defined -128~127  (+128)==&gt; 0~256
              }
  
              for(int i=0;i&lt;kindOfPixel.length;i++){  //show the pixel color value
                  System.out.println(i+" , "+kindOfPixel[i]);
              }
  
          }catch(IOException e){
              System.out.println(e.getMessage());
          }
  
  
     }    
  }
  </code></pre>
  
  <p>I compare this information with the histogram of lena.bmp ,
  but it seems have some different...</p>