0
public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a message");
        String str=sc.nextLine();
        int count=0;
        char ch=' ';
        int p=0,a=0,r=0,g=0,b=0,k=0;
        try
        {
            BufferedImage img=null;
            img=ImageIO.read(new File("in.jpg"));
            for (int i = 0; i < 5184; i++) {
                for (int j = 0; j < 3456; j++) {        
                    p=img.getRGB(i,j);// getting the RGB values at specified pixel
                    // get alpha 
                    a = (p>>24) & 0xff;
                    // get red 
                    r = (p>>16) & 0xff; 
                    // get green 
                    g = (p>>8) & 0xff; 
                    // get blue 
                    b = p & 0xff;
                    if (count<str.length()) { // if a character exists to encode
                        ch=str.charAt(count);// extracting a character
                        count++;

                        //encoding last 2 bits of character in last two bits of a
                        k=ch & (0x3);
                        a=a&(0xfc); 
                        a=a|k;

                        //encoding next 2 bits of char in last two bits of r and so on
                        k= ch & (0xc);
                        r=r&(0xfc);
                        k=k>>2;
                        r=r|k;

                        k= ch &(0x30);
                        g=g&(0xfc);
                        k=k>>4;
                        g=g|k;

                        k=ch &(0xc0);
                        b=b&(0xfc);
                        k=k>>6;
                        b=b|k;
                    }
                    // if no char is there to encode then simply reform the previous p
                    p = (a<<24) | (r<<16) | (g<<8) | b;
                    img.setRGB(i, j, p); 
                }
            } 
            File outputfile = new File("out.jpg");
            ImageIO.write(img, "jpg", outputfile);
        }
        catch (Exception e)
        {
            //System.out.println(e);
        }
        System.out.println("END of programme");
    }

I wrote the above mentioned code to encode a given text in an Image by altering the LSB's of the RGB values of each pixel, But when I read the pixels from the created image I dont get the expected values. Is there some mistake in my encoding technique?

Anurag
  • 33
  • 2
  • You say you "don't get the expected values"; what values do you get, and what values are you expecting? – David Conrad Jan 28 '20 at 20:41
  • I wonder how the decoding part knows how long the encoded string is - also JPEG is, as I know, compressed WITH loss - the encoded bits are probably being discarded (or at least *messed up*) – user85421 Jan 28 '20 at 20:48
  • please check if the read image has the same RGB values as the saved image? see [Maintaining LSB through JPG compilation - is it possible?](https://stackoverflow.com/q/59880267/85421). Or just try PNG – user85421 Jan 28 '20 at 21:04
  • I was just experimenting so just provided length of message while decoding, and yes the reason was that JPEG is lossy, I tried it with PNG and it worked, Thanks a lot for assistance! – Anurag Jan 29 '20 at 08:41
  • Some PNG images are not working for this code as well ! – Anurag Feb 01 '20 at 15:26

0 Answers0