-4

I have been trying to hide an image in another image(both of same type) by making changes in the pixels.But it gives an error like this:

Exception in thread "main" java.lang.NumberFormatException: For input 
       string: "010010101101111111"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Image.main(Image.java:160)**

The code is shown as below:

public class Image {




public static void main(String argv[]) throws Exception

{

    String imageFile1 = "C:/Users/Desktop/1.jpg";
    String imageFile2 = "C:/Users/Desktop/2.jpg";


    File file1 = new File(imageFile1);
    FileInputStream fis1 = null;
    try {
        fis1 = new FileInputStream(imageFile1);
    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }





    File file2 = new File(imageFile2);
    FileInputStream fis2 = null;
    try {
        fis2 = new FileInputStream(imageFile2);
    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }



    BufferedImage oimage1 = ImageIO.read(file1);
    BufferedImage oimage2 = ImageIO.read(file2);


    ByteArrayOutputStream baos1=new ByteArrayOutputStream();


    byte[] buf1 = new byte[1024];

    try {
        for (int readNum; (readNum = fis1.read(buf1)) != -1;) {

            baos1.write(buf1, 0, readNum); 

        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


    ByteArrayOutputStream baos2=new ByteArrayOutputStream();


    byte[] buf2 = new byte[1024];

    try {
        for (int readNum; (readNum = fis2.read(buf1)) != -1;) {

            baos2.write(buf2, 0, readNum); 

        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    final byte[] imageInByte1 = baos1.toByteArray();
    final int size1 = imageInByte1.length;


    final byte[] imageInByte2 = baos2.toByteArray();
    final int size2 = imageInByte2.length;



    int width1 = oimage1.getWidth();
    int height1 = oimage1.getHeight();
    int pixel1 = 0; 
    int red1,green1,blue1;

    int width2 = oimage2.getWidth();
    int height2 = oimage2.getHeight();
    int pixel2=0,red2,green2,blue2;



    final BufferedImage newimg1 = new BufferedImage(width1, height1, BufferedImage.TYPE_INT_ARGB);
    final BufferedImage newimg2 = new BufferedImage(width2, height2, BufferedImage.TYPE_INT_ARGB);

    for (int i = 0; i < width1; i++)
        for (int j = 0; j < height1; j++) {
            //scan through each pixel
            pixel1 = oimage1.getRGB(i, j);
            pixel2 = oimage2.getRGB(i, j);

        //for red

        String redpix1=Integer.toBinaryString(pixel1);  

        String binaryred1 = redpix1.substring(20,23);

        String redpix2=Integer.toBinaryString(pixel2);

         String binaryred2=redpix2.substring(20,23);

        String newred= binaryred1 + binaryred2;


        //for green
        String greenpix1=Integer.toBinaryString(pixel1);


        String binarygreen1=greenpix1.substring(12,15);

        String greenpix2=Integer.toBinaryString(pixel2);    
        String binarygreen2=greenpix2.substring(12,15);

        String newgreen= binarygreen1 + binarygreen2;


      //for blue
        String bluepix1=Integer.toBinaryString(pixel1); 


        String binaryblue1=bluepix1.substring(4,7);

        String bluepix2=Integer.toBinaryString(pixel2); 
        String binaryblue2=bluepix2.substring(4,7);

        String newblue= binaryblue1 + binaryblue2;

        //combining the new values
        String spixel=newred +newgreen + newblue;
        int newpixel = Integer.parseInt(spixel);

        newimg2.setRGB(i,j,newpixel);

        }   


    JFrame f =new JFrame();
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     f.getContentPane().add(new JLabel(new ImageIcon(newimg2)));
     f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);

    }                               
}

The size of 1.jpg is greater than size of 2.jpg. Can this code be modified to get output? or is any another easy way to embed the image?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
user123
  • 105
  • 7
  • Did you look what `java.lang.NumberFormatException` means? – Jim Garrison Apr 23 '18 at 18:12
  • 1
    Basically that number is too large for an int so perhaps look into another data type. – achAmháin Apr 23 '18 at 18:37
  • Thanks for the correction.I have made changes to my code.Kindly look into it – user123 Apr 24 '18 at 03:44
  • 1
    @user123 That’s a different question. Please ask it as a new question. Also experience shows that it is very hard to get closed questions reopened, so you’re not likely to get anywhere further here. Looking forward to your new question. Thank you (and glad that my tip seemed to work). – Ole V.V. Apr 24 '18 at 05:01
  • well because of my reputation i cant ask a new question today.That's why i updated the question, hoping that it will be re-opened. Anyway thanks for the effort. – user123 Apr 24 '18 at 08:35

1 Answers1

1

The error message isn’t very explanatory. The NumberFormatExceptiondocumentation isn’t either in this case. It says:

Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

What happens is an int overflow. The largest int you can have is 2 147 483 647 (10 digits), so 10010101101111111 (17 digits after I removed the leading 0) is way too large. This problem shows as a NumberFormatException.

If you intended that to be a binary number, use Integer.parseInt(spixel, 2) to indicate radix 2 (that is, binary). Then you should be able to parse it since up to 31 binary digits fit in an ìnt (not 32 because it’s signed, so there’s a sign bit).

There is a similar question to this one: What is a NumberFormatException and how can I fix it? However, while the accepted answer to that one does mention overflow (pretty deep down in the answer), it doesn’t cover trying to parse a string with the wrong radix. Still you may want to read through the question and answers and learn.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161