从原始字符串数据创建图像文件

oiopk7p5  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(331)

例如,我有这样的字符串:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHQAAAB0CAYAAABUmhYnAAAEd0lEQVR4Xu2c0ZLjIAwEk///6GzVvZlspWtWksNRnVcwiGmNwHaS5+v1ej38HKPAU6DHsPy3EIGexVOgh/EUqEBPU+Cw9biHCvQwBQ5bjg4V6GEKHLYcHSrQwxQ4bDk6VKCHKXDYcnSoQA9T4LDllB36fD5vlWR9fUvz0+ve9fp0/O7FU7w0n0CXhBSoDiXTRO06FBKKBLLkLvlGgkTp+UvndPzu/ul46Xq7x2/fQ8kR0wtOBaL+1J6uZ+3fPb5Aw0PRtxOWEkigAr3mCJUMuk9cM45uG3ZvJwel8dN4byW8+r1cgWYPVgRaLIlpwqWCT1cgHbr8skOgYUqkgtHwVYfQKZTiTW8rdCgQFWjtt2Pjty3TGdztOB0aHlosuVcHpglJ+h3nUFow7bE6dDOHCjRN2fBty917qEAF+jEHaI+bTlhK0Nsf/aUBpXtYdXy6noDS9dTePf74oYgWRO3dC6b57k6o7vUJFAh3Cz6dMAIV6FWB9FCQlry1f/ejQXLgt9eX6tXu0DSAtL9APysm0OYHI2mCUgVKxxOoQNOcubc/7XnF5yj3LuYPs5Ud+oc5Ry8R6GEpK1CBjlaMuwcvl1xyBC2I8im9T0xva6pPbtL1V+MjPQW6KEQJRAlAggs0vK2oCibQ4g9+LbnXb96THlQBvl5y0yclqYNQAKgAVGIJQHWPpfjf4uv+bUsagECvClCCkL46VIdecyQtKZRhlKGW3OG3LekeQ0DSBOk+1VLCdbdTAqfzlUuuQFPJe/fM9kORQAV6UYBKJslF11NJS0s8xZO2U3zpeO0lNw2g2+HV8dLbKJov1aMKWKDFfyITKKRsegqmjE7H06FpTRHoRwUoQUnu9pJLh4z0EFMdjwRI46ESWwVC8VK7QMN/TRHookDqCB1Knry261AdmmXMdG86xabzd49H83fP1+5QWkB3e7sg4eu06nra46++4K4uqHp9uyACrSKpXS/Q5kMRnUJruN6vnr7Po/VMn9KrepX3UBKgGmD1UVw6P61HoKmi0F+HfhZIhy766NDhU2F66CEgzQXjQRUjjb8aX7tDaYFpwKkgAi0SSAUXaO0Pjkk/HUoKFQ9p0wm/hjcONC2B6W3B24KKv1ZLx0vzgfQoFsyHQJe3LQINHUEZrUNre6wO1aHLw+AvO5QOHdReLbE0/vSeedyhKBWUDh00XpoAAg2/EkIAqD0FlPYXqEDp3Pix/b8/FKUOIMem7fR6j8Yr0fvlYoEWK4JAw0dplOE6dLnrqH5JrCp4NcMFejPQ6h7RnTAUT/eTKkpYiidtH99D04C6bwvS+QX65W8sUMkVaKgAlcRwuLfuNL5Ah/fQKkC6Pi2JKXB6NEjxUTslKF1P7e17KE1YbRfoZwUFuuijQ4v/l5s6VocOOzQFYv9ZBcoldzY8R08VEGiq2Ob9Bbo5oDQ8gaaKbd5foJsDSsMTaKrY5v0FujmgNDyBpopt3l+gmwNKwxNoqtjm/QW6OaA0PIGmim3eX6CbA0rDE2iq2Ob9Bbo5oDS8H8eCMw7yCzx+AAAAAElFTkSuQmCC

我想把它转换回一个图像文件。以下是我尝试过的:

public void dumpFromRawData(String rawData, String wheretoPut) throws Exception{
        byte[] imageByte = rawData.getBytes();
        BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imageByte));

        File file = new File(wheretoPut);
        ImageIO.write(bufferedImage, "png", file);
    }

哪里 rawData 是上面的示例字符串,并且 wheretoPut 是新映像目录。。例如 D:\\image.png 但当我运行它时,它给了我这个:

Exception in thread "main" java.lang.IllegalArgumentException: image == null!
    at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
    at javax.imageio.ImageIO.getWriter(ImageIO.java:1592)
    at javax.imageio.ImageIO.write(ImageIO.java:1520)

我怎样才能解决这个问题?或者这不是将其转换回图像的正确方法?

bpsygsoo

bpsygsoo1#

我终于找到了解决办法。thans@chris623启发了我:d
首先,我需要分离字符串文件的“header”。。这个 data:image/png;base64, 字符串,然后将其解码为 byte[] ```
String separator = ",";
String encoded = rawData.split(separator)[1];
byte[] decodedByte = Base64.getDecoder().decode(encoded.getBytes(StandardCharsets.UTF_8));

接下来的过程还是一样的。。完整方法如下所示:

public void dumpFromRawData(String rawData, String wheretoPut, String fileName) throws Exception{
String separator = ",";
if(rawData.contains(separator)) {
// use this when the decoded string contains "," separator, like data:image/png;base64,
String encoded = rawData.split(separator)[1];
byte[] decodedByte = Base64.getDecoder().decode(encoded.getBytes(StandardCharsets.UTF_8));

        // you can use this

// Path destinationFile = Paths.get(wheretoPut, fileName);
// Files.write(destinationFile, decodedByte);

        // or this
        BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(decodedByte));
        File file = new File(wheretoPut+fileName);
        ImageIO.write(bufferedImage, "png", file);

        System.out.println("File has been Written as " + wheretoPut + fileName);
    } else {
        System.out.println("i haven't think about it yet.");
    }
}
vs91vp4v

vs91vp4v2#

图像的数据似乎是base64编码的,因此不能将此字符串转换为字节数组。所以你应该用这样的方法:

byte[] imageByte = Base64.decode(rawData, Base64.DEFAULT);

相关问题