java.lang.NumberFormatException:对于输入字符串:“010010101101111111”

时间:2018-04-23 18:10:22

标签: java steganography

我一直试图通过更改像素来隐藏另一个图像(两者都是相同类型)中的图像。但它会出现如下错误:

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)**

代码如下所示:

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);

    }                               
}

1.jpg的大小大于2.jpg的大小。 可以修改此代码以获得输出吗?或者是嵌入图像的另一种简单方法吗?

1 个答案:

答案 0 :(得分:1)

错误消息不是很清楚。在这种情况下,The NumberFormatExceptiondocumentation也不是。它说:

  

抛出以指示应用程序已尝试转换a   字符串到其中一个数字类型,但字符串没有   适当的格式。

int溢出会发生什么。您可以拥有的最大int是2 147 483 647(10位数),所以10010101101111111(我删除前导0后的17位数)太大了。此问题显示为NumberFormatException

如果您打算将其作为二进制数,请使用Integer.parseInt(spixel, 2)表示基数2(即二进制)。然后你应该能够解析它,因为最多31个二进制数字适合ìnt(不是32因为它已经签名,所以有一个符号位)。

这个问题有一个类似的问题:What is a NumberFormatException and how can I fix it?然而,虽然对那个问题的接受答案确实提到了溢出(在答案中非常深刻),但它并没有涵盖尝试使用错误的基数。你仍然可以阅读问题和答案并学习。