我正在开发一个 .opus 音乐库软件,它将音频/视频文件转换为 .opus 文件并自动用元数据标记它们。
该程序的早期版本已将专辑封面保存为二进制数据,这显然是由 exiftool
揭示的。
问题是,当我使用 -b
选项运行命令以二进制形式输出数据时,整个内容看起来都是二进制的。我不确定如何让程序解析它。我有点期待像 Picture : 11010010101101101011...
这样的条目。
如何解析图片数据,以便为新版本的程序重建图像? (我在 Kubuntu 18.04 上使用 Java8_171)
最佳答案
看起来您正在尝试在文本编辑器中打开原始字节,这当然会让您大吃一惊,因为这些原始字节不代表任何文本编辑器都可以显示的字符。我可以从 exiftool 的输出中看到,您可以知道图像的长度(以字节为单位)。如果您知道文件中的起始字节位置,那么只需一点 Java 代码就可以使您的任务相对简单。如果您可以获得文件中图像的起始位置,您应该能够执行以下操作:
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class SaveImage {
public static void main(String[] args) throws IOException {
byte[] imageBytes;
try (RandomAccessFile binaryReader =
new RandomAccessFile("your-file.xxx", "r")) {
int dataLength = 0; // Assign this the byte length shown in your
// post instead of zero
int startPos = 0; // I assume you can find this somehow.
// If it's not at the beginning
// change it accordingly.
imageBytes = new byte[dataLength];
binaryReader.read(imageBytes, startPos, dataLength);
}
try (InputStream in = new ByteArrayInputStream(imageBytes)) {
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert,
"jpg", // or whatever file format is appropriate
new File("/path/to/your/file.jpg"));
}
}
}
关于java - 从 exiftool 读取二进制图片数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51350425/