简体   繁体   English

读取JPG文件,直到出现某些字节

[英]Read a JPG file until certain bytes occur

So I'm reading JPG files, and I've finished reading the 'header' data and now I'm onto the actual image data. 因此,我正在读取JPG文件,并且已经完成了读取“页眉”数据的操作,现在就可以查看实际的图像数据了。 Thing is, I don't know the size of the image beforehand, so I cannot create an array to read from. 问题是,我事先不知道图像的大小,因此无法创建要读取的数组。 What I could do, though, is read from the end of the 'header' until the end of the image (two bytes: FF and D9), ByteArrayOutputStream to hold each value as it is read, until I encounter byte D9 after byte FF. 不过,我可以做的是从“标头”的末尾读取到图像的末尾(两个字节:FF和D9), ByteArrayOutputStream保留读取时的每个值,直到遇到字节FF后的字节D9 。 How would I go about doing this? 我将如何去做呢?

My code so far, including JPG recognition just so you know the context: 到目前为止,我的代码包括JPG识别,以便您了解上下文:

// check header data, assign header data to important fields

    // Start Of Image (SOI) must be FFD8 and the next marker must be FF
    if(!(bData[0] == (byte) 0xFF && bData[1] == (byte) 0xD8
            && this.bData[2] == (byte) 0xFF))
        this.isValid = false;

    // check if file is not valid
    if(!isValid) {
        System.err.printf("ERROR: File %s is not"
                        + " registered as a bitmap!\n", filename);
        Logger.getLogger(Bitmap.class.getName()).log(Level.SEVERE, null, new IllegalArgumentException());
    }

    // If the next values are correct, then the data stream starts at SOI
    // If not, the data stream is raw
    this.isRawDataStream = !(bData[3] == (byte) 0xE0
            && bData[6]  == (byte) 0x4A
            && bData[7]  == (byte) 0x46
            && bData[8]  == (byte) 0x49
            && bData[9]  == (byte) 0x46
            && bData[10] == (byte) 0x00);

    // get size of image
    ByteArrayOutputStream iData = new ByteArrayOutputStream();

    // start at index 20 of the file (end of 'header')
    // read until End of Image
    /* while(!(iData at i is FF and iData at i+1 is D9)) {
        ???
    }
    */

edit I'm doing this as an exercise to better understand file formats among other things, and I may be horribly misinterpreting JFIF. 编辑我这样做是为了更好地理解文件格式,这可能是我误解JFIF。 If I am, don't hesitate to tell me. 如果我愿意,请不要犹豫告诉我。

The size of the image is in the SOF (Start of Frame) marker. 图像的大小在SOF(帧开始)标记中。

In rereading, I think the original poster is mistaken about the structure of a JPEG stream. 在重读时,我认为原始海报对于JPEG流的结构是错误的。

It must begin with a SOI market and end with an EOI marker. 它必须以SOI市场开始,并以EOI标记结束。 Beyond that, markers can vary in ordering. 除此之外,标记的顺序可能会有所不同。

There are some other restrictions: The SOF marker must come before the SOS markers. 还有其他一些限制:SOF标记必须位于SOS标记之前。 The DHT and DQT markers must appear before any SOS markers that use them. DHT和DQT标记必须出现在使用它们的任何SOS标记之前。

On top of that, there are various JPEG file formats that require an APPn marker at the start of the stream. 最重要的是,有多种JPEG文件格式,在流的开头需要APPn标记。

The code above and question do not reflect the variable nature of a JPEG stream. 上面的代码和问题未反映JPEG流的可变性质。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM