title: Hiding message in JPG image
body: <p>I am trying to hide a in an image it's working fine with .bmp &amp; .png but when I write image as JPG and try to retrieve the hidden message it is not working.
  My procedure, first read an image in format (<code>bmp</code>, <code>gif</code>, <code>jpg</code>,<code>png</code>) write message to hide and save it so that we can again read it and extract the message.
  When i save it with  <code>bmp</code> or <code>png</code> it works fine but when saving with <code>jpg</code> and try to extract the message it doesn't work.</p>
  
  <pre><code> ImageIO.write(bimg, "png", outputfile);//working
   ImageIO.write(bimg, "png", outputfile);//not working 
  </code></pre>
  
  <p>What should I do to make it work for JPEG?</p>
  
  <p><strong>note:</strong> I am reading every pixel as 4 bit integer with ARGB value and changing LSB of R,G,B to hide message.</p>
  
  <pre><code>    public void stegnography(BufferedImage bimg,String msg,String filename)
      {
  
        int w=bimg.getWidth();
        int h=bimg.getHeight();
       //*************************************** 
       // String msg="Hide this message:)";
        System.out.println("message="+msg+" length="+msg.length());
      //*************************************** 
  
        if(msg.length()&gt;255 )
        {
           jLabel3.setText("MESSAGE IS LARGE THAN 255 CHARACTERS");            
        }
        else if( msg.length()*11 &gt;w*h)
        {
           jLabel3.setText("Image is too small");    
        }
        else{
  
    //-------------------------------------------
             byte[] msgbytes= msg.getBytes();
  
          int msglendecode= (bimg.getRGB(0,0)&gt;&gt;8)&lt;&lt;8;
  
           msglendecode |= msg.length();
          bimg.setRGB(0, 0,msglendecode );//hidig msg length at first position
  
           //System.out.println("\npixel at position (0,0) ");
           // bitpattern(bimg.getRGB(0,0) );
  
           for(int i=1,msgpos=0,row=0,j=0;   row&lt;h   ;row++  )
        {
            for(int col=0;col&lt;w &amp;&amp; j&lt;msgbytes.length ;col++,i++ )
            {      
  
                 if (i%11==0) {
  
                      int rgb = bimg.getRGB(col,row);
  
  
                      int a=((rgb &gt;&gt; 24) &amp; 0xff);
  
                      int r = (((rgb &gt;&gt; 16) &amp; 0xff)&gt;&gt;3)&lt;&lt;3;
                      r=r|(msgbytes[msgpos]&gt;&gt;5);
  
                      int g = (((rgb &gt;&gt; 8) &amp; 0xff)&gt;&gt;3)&lt;&lt;3;
                      g=g|((msgbytes[msgpos]&gt;&gt;2)&amp; 7);
  
                       int b = ((rgb &amp; 0xff)&gt;&gt;2)&lt;&lt;2;
                      b=b|(msgbytes[msgpos]&amp;0x3);
  
  
                      rgb=0;
                      rgb=(rgb|(a&lt;&lt;24));
                      rgb=(rgb|(r&lt;&lt;16));
                      rgb=(rgb|(g&lt;&lt;8));
  
                      rgb=(rgb|b);
  
                      bimg.setRGB(col,row,rgb);
  
                      msgpos++;
                      j++;
  
                    //bitpattern(bimg.getRGB(col,row));
  
                }
  
  
            }//for 2
        }//for 1
  
  
        ImageIcon image = new ImageIcon(bimg);
             jLabel3.setIcon(image);  
  
       try {
  
    //  File outputfile = new File("c:/Users/yathestha/Documents/"+filename);
       File outputfile = new File("c:/Users/yathestha/Documents/outpng.png");
      ImageIO.write(bimg, "png", outputfile);
  } catch (IOException e) {
           System.out.println("error in saving image ");
  }
  
    //-------------------------------------------------
        }//else
   // decoding part----------------------------------------------------------------------   
  
      }
  ///////////////////////////////////////////////////////////////////////
  private void decodestegnography(BufferedImage bimg) {
  
       System.out.println("in decode");
  
     int w=bimg.getWidth(),h=bimg.getHeight();
      bitpattern(bimg.getRGB(0, 0));
      int msglength=(bimg.getRGB(0, 0)&amp;0xff);
      bitpattern(msglength);
      System.out.println("Message Length="+msglength);
  
      jTextField1.setText("");
        for(int row=0,j=0,i=1;   row&lt;h   ;row++  )
    { 
        for(int col=0;col&lt;w &amp;&amp; j&lt;msglength ;col++ ,i++)
        {
  
            if (i%11==0) {
               int result=bimg.getRGB(col,row);
  
  
                int charatpos = (((result &gt;&gt; 16) &amp; 0x7) &lt;&lt; 5);
  
                charatpos |=  (((result &gt;&gt; 8) &amp; 0x7) &lt;&lt; 2);
  
                charatpos |=  ((result &amp; 0x3));
  
                jTextField1.setText(jTextField1.getText()+ (char)charatpos);
  
               j++;
            }
        }
    } 
  
       System.out.println("decoding done");
  }//function
  </code></pre>