java.lang.NumberFormatException:对于输入字符串:";010010101101111111“;
我一直试图通过改变像素来隐藏另一个图像(两个图像类型相同)中的图像。但它会产生如下错误:java.lang.NumberFormatException:对于输入字符串:";010010101101111111“;,java,steganography,Java,Steganography,我一直试图通过改变像素来隐藏另一个图像(两个图像类型相同)中的图像。但它会产生如下错误: Exception in thread "main" java.lang.NumberFormatException: For input string: "010010101101111111" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.par
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);
}
}
公共类映像{
公共静态void main(字符串argv[])引发异常
{
字符串imageFile1=“C:/Users/Desktop/1.jpg”;
字符串imageFile2=“C:/Users/Desktop/2.jpg”;
文件file1=新文件(imageFile1);
FileInputStream fis1=null;
试一试{
fis1=新文件输入流(imageFile1);
}捕获(FileNotFoundException e2){
//TODO自动生成的捕捉块
e2.printStackTrace();
}
文件file2=新文件(imageFile2);
FileInputStream fis2=null;
试一试{
fis2=新文件输入流(imageFile2);
}捕获(FileNotFoundException e2){
//TODO自动生成的捕捉块
e2.printStackTrace();
}
BuffereImage oimage1=ImageIO.read(文件1);
BuffereImage oimage2=ImageIO.read(文件2);
ByteArrayOutputStream=newbytearrayoutputstream();
字节[]buf1=新字节[1024];
试一试{
for(int readNum;(readNum=fis1.read(buf1))!=-1;){
1.写入(buf1,0,readNum);
}
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
ByteArrayOutputStream=newbytearrayoutputstream();
字节[]buf2=新字节[1024];
试一试{
for(int readNum;(readNum=fis2.read(buf1))!=-1;){
写入(buf2,0,readNum);
}
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
最后一个字节[]imageInByte1=1.toByteArray();
最终整数大小1=imageInByte1.length;
最后一个字节[]imageInByte2=2.toByteArray();
最终整数大小2=imageInByte2.length;
int width1=oimage1.getWidth();
int height1=oimage1.getHeight();
int像素1=0;
内部红色1、绿色1、蓝色1;
int width2=oimage2.getWidth();
int height2=oimage2.getHeight();
int pixel2=0,红色2,绿色2,蓝色2;
final BuffereImage newimg1=新的BuffereImage(宽度1,高度1,BuffereImage.TYPE_INT_ARGB);
final buffereImage newimg2=新的buffereImage(宽度2,高度2,buffereImage.TYPE_INT_ARGB);
对于(int i=0;i
1.jpg的大小大于2.jpg的大小。
是否可以修改此代码以获得输出?或者有没有其他简单的方法来嵌入图像?错误消息不是很清楚。在这种情况下也不是。它说: 抛出以指示应用程序已尝试转换 字符串,但该字符串没有 适当的格式 发生的是
int
溢出。你能拥有的最大的int
是2147483647(10位),所以100101011111(在我去掉前导0后是17位)太大了。此问题显示为NumberFormatException
如果您希望它是一个二进制数,请使用Integer.parseInt(spixel,2)
指示基数2(即二进制)。然后您应该能够解析它,因为一个ìnt
最多可以容纳31个二进制数字(不是32个,因为它是有符号的,所以有符号位)
这里有一个与此类似的问题:然而,尽管这个问题的公认答案确实提到了溢出(答案中非常深入),但它不包括试图解析具有错误基数的字符串。您可能仍然希望通读问题和答案并进行学习。您是否查看了
java.lang.NumberFormatException
的含义?基本上,该数字对于int来说太大了,因此可能需要查看其他数据类型。感谢您的更正。我已对代码进行了更改。请查看it@user123那是另一个问题。请把它当作一个新问题来问。另外,经验表明,很难重新打开封闭式问题,所以你不太可能在这里取得更大进展。期待你的新问题。谢谢(很高兴我的建议似乎奏效了)。由于我的声誉,我今天不能问新问题。这就是为什么我更新了这个问题,希望它能重新打开。无论如何,谢谢你的努力。