简体   繁体   中英

reading jpeg file as a unsigned char array

I'm trying to making a video stream using jpeg files using next project: https://www.medialan.de/usecase0001.html

they are using jpegsamples according the following header and source file.

jpegsmaples.h:

#ifndef JPEGSAMPLES_H
#define JPEGSAMPLES_H

#define KJpegCh1ScanDataLen 32
#define KJpegCh2ScanDataLen 56

extern char JpegScanDataCh1A[];
extern char JpegScanDataCh1B[];

extern char JpegScanDataCh2A[];
extern char JpegScanDataCh2B[];

jpegsamples.cpp:

#include "jpegsamples.h"

// RGB JPEG images as RTP payload - 48x32 pixel
char JpegScanDataCh1A[KJpegCh1ScanDataLen] =
{
    static_cast<char>(0xf8), static_cast<char>(0xbe),
    static_cast<char>(0x8a), static_cast<char>(0x28),
    static_cast<char>(0xaf), static_cast<char>(0xe5),
    static_cast<char>(0x33), static_cast<char>(0xfd),
    static_cast<char>(0xfc), static_cast<char>(0x0a),
    static_cast<char>(0x28), static_cast<char>(0xa2),
    static_cast<char>(0x80), static_cast<char>(0x0a),
    static_cast<char>(0x28), static_cast<char>(0xa2),
    static_cast<char>(0x80), static_cast<char>(0x0a),
    static_cast<char>(0x28), static_cast<char>(0xa2),
    static_cast<char>(0x80), static_cast<char>(0x0a),
    static_cast<char>(0x28), static_cast<char>(0xa2),
    static_cast<char>(0x80), static_cast<char>(0x0a),
    static_cast<char>(0x28), static_cast<char>(0xa2),
    static_cast<char>(0x80), static_cast<char>(0x3f),
    static_cast<char>(0xff), static_cast<char>(0xd9)
};
char JpegScanDataCh1B[KJpegCh1ScanDataLen] =
{
    static_cast<char>(0xf5), static_cast<char>(0x8a),
    static_cast<char>(0x28), static_cast<char>(0xa2),
    static_cast<char>(0xbf), static_cast<char>(0xca),
    static_cast<char>(0xf3), static_cast<char>(0xfc),
    static_cast<char>(0x53), static_cast<char>(0x0a),
    static_cast<char>(0x28), static_cast<char>(0xa2),
    static_cast<char>(0x80), static_cast<char>(0x0a),
    static_cast<char>(0x28), static_cast<char>(0xa2),
    static_cast<char>(0x80), static_cast<char>(0x0a),
    static_cast<char>(0x28), static_cast<char>(0xa2),
    static_cast<char>(0x80), static_cast<char>(0x0a),
    static_cast<char>(0x28), static_cast<char>(0xa2),
    static_cast<char>(0x80), static_cast<char>(0x0a),
    static_cast<char>(0x28), static_cast<char>(0xa2),
    static_cast<char>(0x80), static_cast<char>(0x3f),
    static_cast<char>(0xff), static_cast<char>(0xd9)
};

I've already tried to read the binary data of the jpeg file into a char array according following code:

    std::ifstream Datafile("C:\test01.jpg", std::ios::binary);
    if(!Datafile.good())
        return;
    Datafile.seekg(0, std::ios::end);
    size_t filesize = (int)Datafile.tellg();
    Datafile.seekg(0);

    char * output = new char[filesize];
    if(Datafile.read((char *)output, filesize))
    {
        std::ofstream fout("def.jpeg", std::ios::binary);
        if(!fout.good())
            return;
        fout.write((char *)output, filesize);
    }

but this didn't work and didn't gave any error. the streaming client (VLC) was in a loop of trying to connect and receiving images.

First I want to be able to receive a image of the jpeg file like in next example: VLC客户端连接示例

This should be doable by storing the data of the jpeg file in an char array like they did in the jpegsamples code.

EDIT: There is no need to write a new file so i've edited the code I've already tried and added some more relevant code:

    std::ifstream Datafile("C:\\test01.jpg", std::ios::binary);
    if(!Datafile.good())
    {
        std::cout <<"error file"<<std::endl;
        return;
    }
    Datafile.seekg(0, std::ios::end);
    size_t filesize = (int)Datafile.tellg();
    Datafile.seekg(0);

    char * output = new char[filesize];

    char  * Samples1[2] = {output , output };
    char  * Samples2[2] = {output , output };

which resulted into next image: VLC客户端连接使用不同的代码 There is definitely something wrong with the data. I might need a jpeg I/O library?

The values in jpegsamples.cpp are defined as JPEG images for RTP payload. This are not the same values as the raw data values in the jpeg file.

Therefore It is not possible to simple read the binary values of the file and use them for the RTP payload, without any calculation. I think goes behind the scope of this topic and this will 'answer' the question.

For more info about the RTP payload format for JPEG images

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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