我编写代码将消息隐藏到图像中,首先我将图像转换为字节数组并将消息隐藏到图像中但是当我想将新的字节数组转换为图像时,我得到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
答案 0 :(得分:1)
我通过以下编码解决了这个问题:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class EncodeImage {
public static void main(String[] args) throws IOException{
if(args.length!=0)
{
BufferedImage sourceImage = null;
try {
sourceImage = ImageIO.read(new File("inacup_donut.jpg"));
} catch (IOException e) {
}
int type = sourceImage.getType();
int w = sourceImage.getWidth();
int h = sourceImage.getHeight();
byte[] pixels = null;
if (type == BufferedImage.TYPE_3BYTE_BGR)
{
System.out.println("type.3byte.bgr");
pixels = (byte[]) sourceImage.getData().getDataElements(0, 0, w, h, null);
//for(int i=0;i<pixels.length;i++)
//pixels[i]=(byte) (pixels[i]|2);
System.out.println(pixels[1]);
int [] int_array=new int [pixels.length];
for(int i=0;i< pixels.length;i++)
{
int_array [i]= pixels[i];
}//end for loop
int x []= encodeMessage(int_array,h,w,
"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];
}
try {
BufferedImage edgesImage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
edgesImage.getWritableTile(0, 0).setDataElements(0, 0, w, h, new_bytearray);
ImageIO.write(edgesImage, "png", new File("snap.png"));
} catch (IOException e) {
}
}
}
}