Java 为什么字节数组保存为图像时返回null?

Java 为什么字节数组保存为图像时返回null?,java,Java,我编写代码将消息隐藏到图像中,首先我将图像转换为字节数组并将消息隐藏到图像中,但当我想将新字节数组转换为图像时,我得到null?我知道关于这个话题有很多问题,我尝试了答案,但没有成功。 有人能帮我吗 import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayO

我编写代码将消息隐藏到图像中,首先我将图像转换为字节数组并将消息隐藏到图像中,但当我想将新字节数组转换为图像时,我得到
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
导入java.awt.Graphics2D;
导入java.awt.Image;
导入java.awt.image.buffereImage;
导入java.io.ByteArrayInputStream;
导入java.io.ByteArrayOutputStream;
导入java.io.File;
导入java.io.IOException;
导入java.io.OutputStream;
导入javax.imageio.IIOImage;
导入javax.imageio.imageio;
导入javax.imageio.ImageReadParam;
导入javax.imageio.ImageReader;
导入javax.imageio.ImageWriteParam;
导入javax.imageio.ImageWriter;
导入javax.imageio.stream.ImageInputStream;
导入javax.imageio.stream.ImageOutputStream;
//导入javax.swing.text.html.HTMLDocument.Iterator;
导入com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
导入java.util.Iterator;
公开课考试{
公共静态void main(字符串[]args)引发IOException{
如果(参数长度==0)
{
字符串dirName=“C:\\”;
ByteArrayOutputStream bas=新的ByteArrayOutputStream(1000);
BuffereImage img=ImageIO.read(新文件(“inacup_donut.jpg”);
ImageIO.write(img,“jpg”,baos);
paos.flush();
String base64String=Base64.encode(baos.toByteArray());
baos.close();
字节[]bytearray=Base64.解码(base64String);
int[]int_数组=新的int[bytearray.length];

对于(int i=0;ii),我通过以下编码解决了问题:

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) {
            }






        }
        } 
  }
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
公共类图像编码{
公共静态void main(字符串[]args)引发IOException{
如果(参数长度!=0)
{
BuffereImage sourceImage=null;
试一试{
sourceImage=ImageIO.read(新文件(“inacup_donut.jpg”);
}捕获(IOE异常){
}
int type=sourceImage.getType();
int w=sourceImage.getWidth();
int h=sourceImage.getHeight();
字节[]像素=空;
if(type==buffereImage.type\u 3BYTE\u BGR)
{
System.out.println(“type.3byte.bgr”);
像素=(字节[])sourceImage.getData().getDataElements(0,0,w,h,null);

//为了(int i=0;i哪个部分给出了
null
?什么是
encodeMessage
应该是?@Cirou encodeMessage是函数,使用LSB算法将消息隐藏到int_数组中,并返回int数组,此时会发生某种情况,因为如果您注释掉
new_bytearray
创建部分并使用旧的
字节数组
ImageIO.read
效果很好。是的,这是正确的,我认为使用的格式有问题?你能不能也发布编码消息方法。@DougHauf编码消息方法是用于将数据隐藏到图像中的LSB算法(隐写术),我不能发布此方法,因为我将其用于安全项目,有很多关于此算法的资源,google it。