将jpeg文件读取为无符号字符数组

时间:2019-05-17 12:55:08

标签: c++ io jpeg

我正尝试使用下一个项目使用jpeg文件制作视频流: https://www.medialan.de/usecase0001.html

他们根据以下标头和源文件使用jpegsamples。

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)
};

我已经尝试根据以下代码将jpeg文件的二进制数据读入char数组:

    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);
    }

但是这没有用,也没有给出任何错误。流客户端(VLC)处于尝试连接和接收图像的循环中。

首先,我希望能够像下面的示例一样接收jpeg文件的图像: VLC Client connected example

这应该可以通过将jpeg文件的数据存储在char数组中来实现,就像在jpegsamples代码中一样。

编辑: 无需编写新文件,因此我已经编辑了已经尝试过的代码并添加了更多相关代码:

    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 };

生成下一张图片: VLC Client connection with different code 数据肯定有问题。我可能需要jpeg I / O库吗?

1 个答案:

答案 0 :(得分:0)

jpegsamples.cpp中的值被定义为RTP有效负载的JPEG图像。该值与jpeg文件中的原始数据值不同。

因此,不进行任何计算就不可能简单地读取文件的二进制值并将其用于RTP有效负载。我认为这超出了本主题的范围,这将“回答”问题。

有关the RTP payload format for JPEG images

的更多信息