Java 从数据转储中提取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

我从磁盘获得二进制/十六进制数据转储。我的任务是从中提取图像。它是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: 00 3B
在里面完好无损。如何使用Java提取图像?

我建议您:

  • 将输入文件作为
    文件inputstream
    打开
  • 扫描输入,直到找到其中一个标题
  • 使用图像的正确文件扩展名,为输出文件打开一个新的
    FileOutputStream
    ,并将内容复制到该文件,直到找到相应的页脚
  • 关闭输出流
  • 从第2步开始,直到到达流的末尾
  • 关闭输入流。完成了
  • 的源代码和字节码

    这将对jpg有效

    对于png

    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中?你需要什么帮助?