2013-10-17 93 views
-2

我寫代碼隱藏消息到圖像中,起初我將圖像轉換爲字節數組,並將消息隱藏到圖像中,但是當我想將新的字節數組轉換爲圖像時,我得到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 
+0

哪一部分給你'null'? – justhalf

+0

imag返回null – Shatha

+0

'encodeMessage'應該是什麼? – Cirou

回答

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






     } 
     } 
    } 
+0

你也可以發佈encodingMessage方法。 –

+1

@DougHauf encodingMessage方法是用於將數據隱藏到圖像(隱寫術)中的LSB算法,我不能發佈這種方法,因爲我將它用於安全項目中,關於這個算法有很多資源,google它。 – Shatha