Java 从数据转储中提取JPEG/PNG/GIF图像
我从磁盘获得二进制/十六进制数据转储。我的任务是从中提取图像。它是JPEG、PNG和GIF图像的混合转储 您可以在此处找到数据文件 图像的页眉和页脚Java 从数据转储中提取JPEG/PNG/GIF图像,java,png,jpeg,gif,Java,Png,Jpeg,Gif,我从磁盘获得二进制/十六进制数据转储。我的任务是从中提取图像。它是JPEG、PNG和GIF图像的混合转储 您可以在此处找到数据文件 图像的页眉和页脚 jpg: header: FF D8 FF E0 xx xx 4A 46 49 46 00, footer: FF D9 png: header: 89 50 4E 47 0D 0A 1A 0A, footer: 49 45 4E 44 AE 42 60 82 gif: header: 47 49 46 38 39 61, footer: 0
jpg:
header: FF D8 FF E0 xx xx 4A 46 49 46 00, footer: FF D9
png:
header: 89 50 4E 47 0D 0A 1A 0A, footer: 49 45 4E 44 AE 42 60 82
gif:
header: 47 49 46 38 39 61, footer: 00 3B
在里面完好无损。如何使用Java提取图像?我建议您:
文件inputstream
打开李>
FileOutputStream
,并将内容复制到该文件,直到找到相应的页脚李>
copyJpeg
功能替换为
private void copypng(InputStream in, OutputStream out) throws
IOException
{
boolean notFinished = true;
long copiedBytes = 3;
do
{
int v1 = in.read();
if (v1 == 0x89) // marker
{
int v2 = in.read();
if (v2 < 0) // end of file
{
out.write(0x82);
copiedBytes++;
notFinished = false;
}
else
if (v2 == 0x49) // embedded png stream ended
{
// copy the end of stream marker
out.write(0x49);
out.write(0x45);
out.write(0x4E);
out.write(0x44);
out.write(0xAE);
out.write(0x42);
out.write(0x60);
out.write(0x82);
copiedBytes += 2;
notFinished = false;
}
else
{
// copy the two bytes, just a marker of the embedded png
out.write(0x89);
out.write(v2);
copiedBytes += 2;
}
}
else
if (v1 == -1) // unexpected end of input file
{
notFinished = false;
}
else // just copy that value
{
out.write(v1);
copiedBytes++;
}
}
while (notFinished);
totalBytes += copiedBytes;
if (!quiet)
{
System.out.println(" (" + copiedBytes + " bytes)");
}
totalOutputFiles++;
// close output stream
try
{
out.close();
}
catch (IOException ioe)
{
// ignore error when closing output stream
}
}
private void copygif(InputStream in, OutputStream out) throws
IOException
{
boolean notFinished = true;
long copiedBytes = 3;
do
{
int v1 = in.read();
if (v1 == 0x47) // marker
{
int v2 = in.read();
if (v2 < 0) // end of file
{
out.write(0x00);
copiedBytes++;
notFinished = false;
}
else
if (v2 == 0x21) // embedded png stream ended
{
// copy the end of stream marker
out.write(0x21);
out.write(0x00);
out.write(0x00);
out.write(0x3B);
out.write(0x00);
copiedBytes += 2;
notFinished = false;
}
else
{
// copy the two bytes, just a marker of the embedded png
out.write(0x47);
out.write(v2);
copiedBytes += 2;
}
}
else
if (v1 == -1) // unexpected end of input file
{
notFinished = false;
}
else // just copy that value
{
out.write(v1);
copiedBytes++;
}
}
while (notFinished);
totalBytes += copiedBytes;
if (!quiet)
{
System.out.println(" (" + copiedBytes + " bytes)");
}
totalOutputFiles++;
// close output stream
try
{
out.close();
}
catch (IOException ioe)
{
// ignore error when closing output stream
}
}
对于gif,
将copyJpeg
功能替换为
private void copypng(InputStream in, OutputStream out) throws
IOException
{
boolean notFinished = true;
long copiedBytes = 3;
do
{
int v1 = in.read();
if (v1 == 0x89) // marker
{
int v2 = in.read();
if (v2 < 0) // end of file
{
out.write(0x82);
copiedBytes++;
notFinished = false;
}
else
if (v2 == 0x49) // embedded png stream ended
{
// copy the end of stream marker
out.write(0x49);
out.write(0x45);
out.write(0x4E);
out.write(0x44);
out.write(0xAE);
out.write(0x42);
out.write(0x60);
out.write(0x82);
copiedBytes += 2;
notFinished = false;
}
else
{
// copy the two bytes, just a marker of the embedded png
out.write(0x89);
out.write(v2);
copiedBytes += 2;
}
}
else
if (v1 == -1) // unexpected end of input file
{
notFinished = false;
}
else // just copy that value
{
out.write(v1);
copiedBytes++;
}
}
while (notFinished);
totalBytes += copiedBytes;
if (!quiet)
{
System.out.println(" (" + copiedBytes + " bytes)");
}
totalOutputFiles++;
// close output stream
try
{
out.close();
}
catch (IOException ioe)
{
// ignore error when closing output stream
}
}
private void copygif(InputStream in, OutputStream out) throws
IOException
{
boolean notFinished = true;
long copiedBytes = 3;
do
{
int v1 = in.read();
if (v1 == 0x47) // marker
{
int v2 = in.read();
if (v2 < 0) // end of file
{
out.write(0x00);
copiedBytes++;
notFinished = false;
}
else
if (v2 == 0x21) // embedded png stream ended
{
// copy the end of stream marker
out.write(0x21);
out.write(0x00);
out.write(0x00);
out.write(0x3B);
out.write(0x00);
copiedBytes += 2;
notFinished = false;
}
else
{
// copy the two bytes, just a marker of the embedded png
out.write(0x47);
out.write(v2);
copiedBytes += 2;
}
}
else
if (v1 == -1) // unexpected end of input file
{
notFinished = false;
}
else // just copy that value
{
out.write(v1);
copiedBytes++;
}
}
while (notFinished);
totalBytes += copiedBytes;
if (!quiet)
{
System.out.println(" (" + copiedBytes + " bytes)");
}
totalOutputFiles++;
// close output stream
try
{
out.close();
}
catch (IOException ioe)
{
// ignore error when closing output stream
}
}
并更新
private void setDefaultArguments()
中outputSuffix
的值。是的,看起来查找标题将是一个非常好的开始。。。现在你可以搜索方法来实现这一点。我实际上不知道如何处理这些数据…搜索:java中的二进制搜索“不知道如何处理这些数据…”到底是什么问题?你的意思是你不知道什么是byteArray
?或者你不能在字节内移动?或者,您不能执行While
循环来搜索字节内的值?或者你甚至不能把你的十六进制转储文件带到Java中?你需要什么帮助?