从原始图像写入AVI文件

时间:2014-02-14 10:22:18

标签: c++ opencv

我正在读取一个只有1和0的原始文件,我已将其从SurfaceFlinger保存到字符缓冲区并写入IplImage。然后我试着将它写入视频文件,但cvWriteFrame()返回0.可以在我出错的任何一个帮助。

以下是我的代码。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <stdio.h>
#include <cstring>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
    FILE * pFile;
    long lSize;
    char * buffer;
    size_t result;

    pFile = fopen(argv[1], "rb");
    if (pFile == NULL) {
        fputs("File error", stderr);
        exit(1);
    }

    // obtain file size:
    fseek(pFile, 0, SEEK_END);
    lSize = ftell(pFile)
    rewind(pFile);

    // allocate memory to contain the whole file:
    buffer = (char*) malloc(sizeof(char) * lSize);

    if (buffer == NULL) {
        fputs("Memory error", stderr);
        exit(2);
    }

    // copy the file into the buffer:
    result = fread(buffer, 1, lSize, pFile);
    if (result != lSize) {
        fputs("Reading error", stderr);
        exit(3);
    }

    // clean up
    fclose(pFile);


    IplImage *img;
    img = cvCreateImage(cvSize(768, 1280), IPL_DEPTH_8U, 4);
    memcpy(img->imageData, buffer, lSize);
    cvSaveImage("Displaywindow.png", img);

    int width = img->width;
    int height = img->height;

    CvVideoWriter *writer = cvCreateVideoWriter("out.avi",
                                    CV_FOURCC_DEFAULT,
                                      30,
                                       cvSize(height, width), 1);

    if(writer == NULL)
    {
        printf("error\n");
        exit(0);
    }
    for(int counter=0;counter < 3000;counter++)
    {
        if(cvWriteFrame(writer,img) == 0)
        {
            printf("con't write\n");
            exit(0);
        }
    }
    waitKey(0);
    return 0;
}

我将原始文件作为参数传递。

0 个答案:

没有答案