簡體   English   中英

讀取JPG文件,直到出現某些字節

[英]Read a JPG file until certain bytes occur

因此,我正在讀取JPG文件,並且已經完成了讀取“頁眉”數據的操作,現在就可以查看實際的圖像數據了。 問題是,我事先不知道圖像的大小,因此無法創建要讀取的數組。 不過,我可以做的是從“標頭”的末尾讀取到圖像的末尾(兩個字節:FF和D9), ByteArrayOutputStream保留讀取時的每個值,直到遇到字節FF后的字節D9 。 我將如何去做呢?

到目前為止,我的代碼包括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)) {
        ???
    }
    */

編輯我這樣做是為了更好地理解文件格式,這可能是我誤解JFIF。 如果我願意,請不要猶豫告訴我。

圖像的大小在SOF(幀開始)標記中。

在重讀時,我認為原始海報對於JPEG流的結構是錯誤的。

它必須以SOI市場開始,並以EOI標記結束。 除此之外,標記的順序可能會有所不同。

還有其他一些限制:SOF標記必須位於SOS標記之前。 DHT和DQT標記必須出現在使用它們的任何SOS標記之前。

最重要的是,有多種JPEG文件格式,在流的開頭需要APPn標記。

上面的代碼和問題未反映JPEG流的可變性質。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM