Question

I am trying to decode the data encoded in the image. The encoding works fine and the data size of image also changes but for some reason the decoded data is an empty string. Either the encoded data get lost or this code has some mistake.

int temp,tempText=0,x=0,p=0;

    try
    {
        image= ImageIO.read(new File("C:\\Users\\Desktop\\Encoded.png"));

    }
    catch (IOException e)
    {
        e.printStackTrace();
    }


    for(int i=0;i<image.getWidth();i++)
    {
        for(int j=0;j<image.getHeight();j++)
        {
            pixels[i][j]=image.getRGB(i, j);
        }
    }

    for(int i=0;i<Width;i++)
    {
        for(int j=0;j<Height;j++)
        {
            temp=pixels[i][j];

            int change=0;   

            for(int k=0;k<4;k++) // 4 iterations for 4bytes of every pixel
            {
                if(k==0)
                {
                    change=1;
                }
                else
                    if(k==1)
                    {
                        change=256;
                    }
                    else
                        if(k==2)
                        {
                            change=65536;
                        }
                        else
                            if(k==3)
                            {
                                change = 16777216;
                            }


                tempText=tempText | (pixels[i][j] & change);

                p++;

                if(p==8) // because character is of 8bits
                {
                    myString.concat(String.valueOf(tempText));// Writing decoded data in string
                    p=0;
                    tempText=0;

                }
            }


        }

 // Writing in file

    try
    {
        file = new File("C:\\Users\\Desktop\\Retreive.txt");
        fw = new FileWriter(file);

        bw= new BufferedWriter(fw);

        bw.write(myString);
        bw.close();

    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }

Kindly notify me if any mistake I am making or any thing this code is lacking.

Was it helpful?

Solution

String.concat doesn't change the string you call it on, but instead returns a new string. So if you use myString = myString.concat(...) instead, you might get better results. If tempText contains a character code, you could cast it to a char (since String.valueOf returns the string representation of the int):

// Writing decoded data in string
// myString = myString.concat(String.valueOf(tempText));
myString += (char) tempText;

instead of:

myString.concat(String.valueOf(tempText));// Writing decoded data in string
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top