我寫代碼隱藏消息到圖像中,起初我將圖像轉換爲字節數組,並將消息隱藏到圖像中,但是當我想將新的字節數組轉換爲圖像時,我得到null
?我知道關於這個話題有很多問題,我嘗試了答案,但沒有成功。 請誰能幫助我?爲什麼字節數組保存爲圖像時返回null?
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
//import javax.swing.text.html.HTMLDocument.Iterator;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.util.Iterator;
public class test {
public static void main(String[] args) throws IOException{
if(args.length==0)
{
String dirName="C:\\";
ByteArrayOutputStream baos=new ByteArrayOutputStream(1000);
BufferedImage img=ImageIO.read(new File("inacup_donut.jpg"));
ImageIO.write(img, "jpg", baos);
baos.flush();
String base64String=Base64.encode(baos.toByteArray());
baos.close();
byte[] bytearray = Base64.decode(base64String);
int [] int_array=new int [bytearray.length];
for(int i=0;i<bytearray.length;i++)
{
int_array [i]=(int)bytearray[i];
}//end for loop
int x []= encodeMessage(int_array,img.getHeight(),img.getWidth(),
"message") ;
System.out.println("read " + x[0]+ " bytes,");
byte [] new_bytearray = new byte [x.length] ;
for(int i=0;i<x.length;i++)
{
new_bytearray[i]=(byte)x[i];
}//end for loop
BufferedImage imag=ImageIO.read(new ByteArrayInputStream(new_bytearray));
ImageIO.write(imag, "jpg", new File(dirName,"snap.jpg"));
}
}//end main function
}//end class
哪一部分給你'null'? – justhalf
imag返回null – Shatha
'encodeMessage'應該是什麼? – Cirou