2016-02-16 72 views
1

好的,所以我寫了一個小程序,應該花10分鐘才能完成,但是我遇到了無法預料的問題。Java:將一個字符串寫入一個JPG文件

該程序應採用我舊手機上的Vault程序中的一些舊文件,它們基本上都是Jpg文件,但在文件的前面添加了「隱藏」文本。所以下面

是我的代碼邏輯

  1. 得到該文件的文件夾輸入,
  2. 創建一個包含每個實際文件的ArrayList。
  3. 調用ConvertFiles將文件轉換爲字符串,
  4. 使用子字符串刪除前8個字符,並將該臨時文件保存到包含字符串的另一個ArrayList中。
  5. 將該字符串解碼爲base64,並將其輸入到bytearrayinputstream並將其保存到bufferedimage。

這是問題發生的地方。我有內容一直到ImageIO.read(bis),所以當它嘗試寫入一個新文件時,它會從ImageTypeSpecifier中拋出image == null 。我已經嘗試了多種解碼和編碼字符串的方式,但需要任何幫助,如果需要更多信息,我會提供!

public class ImageConvert { 

    private File folder; 
    private ArrayList<File> files; 
    private ArrayList<String> stringFiles = new ArrayList<>(); 
    private ArrayList<BufferedImage> bfImages = new ArrayList<>(); 
    boolean isRunning = true; 
    Scanner scanner = new Scanner(System.in); 
    String folderPath; 

    public static void main(String[] args) { 
     ImageConvert mc = new ImageConvert(); 
     mc.mainCode(); 
    } 

    public void mainCode(){ 
     System.out.println("Please enter the folder path: "); 
     folderPath = scanner.nextLine(); 
     folder = new File(folderPath); 
     //System.out.println("folderpath: " + folder); 
     files = new ArrayList<File>(Arrays.asList(folder.listFiles())); 
     convertFiles(); 
    } 

    public void convertFiles(){ 
     for(int i = 0; i < files.size(); i++){ 
      try { 
       String temp = FileUtils.readFileToString(files.get(i)); 
       //System.out.println("String " + i + " : " + temp); 
       stringFiles.add(temp.substring(8)); 
      } catch (IOException ex) { 
       Logger.getLogger(ImageConvert.class.getName()).log(Level.SEVERE, 
                    null, ex); 
      } 
     } 

     //System.out.println("Converted string 1: " + stringFiles.get(0)); 
     for(int j = 0; j < stringFiles.size(); j++){ 

      BufferedImage image = null; 
      byte[] imageByte; 

      try { 
       BASE64Decoder decoder = new BASE64Decoder(); 
       imageByte = decoder.decodeBuffer(stringFiles.get(j)); 
       System.out.println(imageByte.toString()); 
       ByteArrayInputStream bis = new ByteArrayInputStream(imageByte); 
       image = ImageIO.read(bis); 
       bis.close(); 
       bfImages.add(image); 
      } catch (IOException ex) { 
       Logger.getLogger(ImageConvert.class.getName()).log(Level.SEVERE, 
                    null, ex); 
      } 

     } 


     System.out.println("Image 1: " + bfImages.get(0)); 

     for(int k = 0; k < bfImages.size(); k++){ 
      try { 
       ImageIO.write(bfImages.get(k), "jpg", 
           new File(folderPath + "/" + k + ".jpg")); 
      } catch (IOException ex) { 
       Logger.getLogger(ImageConvert.class.getName()).log(Level.SEVERE, 
                    null, ex); 
      } 
     } 

    } 

} 

This is an example of my files:

+2

由於缺少段落,你的問題是不可讀的。 –

+0

大聲笑...反正,我以爲十六進制是基地16(不是64)。看看你是如何真正設法讓你的圖像可以作爲jpg查看的,這將是一件很有趣的事情。 – DevilsHnd

+0

您隱藏的文本中包含多少個字符 –

回答

1

下面的示例使用您附帶的問題的文件。您不需要進行任何解碼,只需將文件讀入內存,存儲8字節String,然後將剩餘字節從8字節偏移量寫入jpg

只需調整下面的方法來處理:「文件夾輸入」。您不需要包含每個實際jpg文件的ArrayList

public void convertFiles() { 
    File imgFile; 
    byte[] bytes; 
    FileOutputStream fos; 
    String temp; 

    for (int i = 0; i < files.size(); i++) { 
     temp = ""; 

     try { 
      // 'read' method can be found below 
      bytes = read(files.get(i)); 

      // read the 8 byte string from the beginning of the file 
      for(int j = 0; j < 8; j++) { 
       temp += (char) bytes[j]; 
      } 

      imgFile = new File("img.jpg"); 

      // points to './img.jpg' 
      fos = new FileOutputStream(imgFile); 

      // write from offset 8 to end of 'bytes' 
      fos.write(bytes, 8, bytes.length - 8); 

      fos.close(); 
     } catch (FileNotFoundException e) { 
      // Logger stuff 
     } catch (IOException ex) { 
      // Logger stuff 
     } 

     System.out.println("[temp]:> " + temp); 
    } 

} 

read(File file)方法改編自一個社區維基答案File to byte[] in Java

public byte[] read(File file) throws IOException { 
    ByteArrayOutputStream ous = null; 
    InputStream ios = null; 
    try { 
     byte[] buffer = new byte[4096]; 
     ous = new ByteArrayOutputStream(); 
     ios = new FileInputStream(file); 
     int read = 0; 
     while ((read = ios.read(buffer)) != -1) { 
      ous.write(buffer, 0, read); 
     } 
    } finally { 
     try { 
      if (ous != null) 
       ous.close(); 
     } catch (IOException e) { 
     } 

     try { 
      if (ios != null) 
       ios.close(); 
     } catch (IOException e) { 
     } 
    } 

    return ous.toByteArray(); 
} 

輸出:

[temp]:> obscured 

映像文件:

Decoded image file.

+0

感謝您的回答,我知道延遲的迴應,但我只是喜歡那個形象,這就是爲什麼它的測試文件xD – ChonBonStudios

相關問題