Java:将字符串写入JPG文件

时间:2016-02-16 04:06:37

标签: java string stream java-io

好吧,所以我正在编写一个小程序,应该花费10分钟才能完成但是我遇到了无法预料的问题。

该程序应该接收我在旧手机上的保险库程序中的一些旧文件,它们基本上是Jpg文件,但在文件的前面添加了“模糊”文本。

以下是我的代码逻辑

  1. 获取文件的文件夹输入
  2. 创建一个包含每个实际文件的arraylist。
  3. 调用ConvertFiles将文件转换为字符串
  4. 使用子字符串删除前8个字符,并将该临时文件保存到包含字符串的另一个arraylist。
  5. 将该字符串解码为base64并将其输入到bytearrayinputstream并将其保存到bufferedimage。
  6. 这是问题发生的地方。我有内容一直到ImageIO.read(bis),所以当它尝试写入新文件时它会抛出图像== null 来自ImageTypeSpecifier。我已经尝试了多种解码和编码字符串的方法,但是需要任何帮助,如果需要更多信息,我会提供它!

    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:

1 个答案:

答案 0 :(得分:2)

以下示例使用您在问题中包含的文件。您不需要进行任何解码,只需将文件读入内存,存储8字节String,然后将剩余字节从8字节偏移量写入jpg

只需调整以下方法即可使用:“文件夹输入”。您不需要包含每个实际ArrayList文件的jpg

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.

相关问题