我正在尝试写出我从视频4 linux 2示例程序中获得的内存。然而,它并不起作用。我在尝试访问指针时遇到分段错误。我希望这不是一个愚蠢的错误,因为我已经在上面花了几天的时间。下面是代码:(它没有格式化,因为与html的冲突太多了。)我的计算机正在使用mmap执行分支。mRGB =mScreen->pixelspixel在writeFile()中出现故障;
我使用的是http://linuxtv.org/downloads/v4l-dvb-apis/capture-example.html中的v4l2示例代码
以下是我所做的更改:
在第497行,我更改了
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB332;
fmt.fmt.pix.field = V4L2_FIELD_NONE;我还在main(...)中添加了一行。在704行附近的某处。
close_device();
writeFile();
fprintf(stderr, "\n");并且我插入了下面指定的writeFile()方法:
typedef struct Screen {
unsigned char pixels[640*480];
} Screen;
static void writeFile() {
const int dimx = 640, dimy = 480;
int mNumPixels = dimx * dimy;
Screen *mScreen;
int i, pixel;
FILE *file = fopen("output","w");
if (file == NULL) return;
/* shift the bits around */
(void)fprintf(file, "P6\n%d %d\n255\n", dimx, dimy);
for (i = 1; i < n_buffers; i++) {
mScreen = buffers[i].start;
printf("\npointer to mScreen is: %p\n", mScreen);
for (pixel = 0; pixel < 640*480; pixel++) {
static unsigned char color[3];
unsigned char mRGB = 0;
printf("%d:%x\n", pixel, mRGB);
fflush(stdout);
mRGB = mScreen->pixels[pixel];
color[0] = (mRGB & 0xE0) >> 5;
color[1] = (mRGB & 0x1D) >> 2;
color[2] = mRGB & 0x03;
fwrite(color, sizeof(unsigned char)*3, 1, file);
}
}
fclose(file);
}发布于 2015-05-10 22:24:07
示例代码的uninit_device()修改了可用内存,使得指针不再可用。必须在此方法/函数之前调用writeFile()方法。
https://stackoverflow.com/questions/30142512
复制相似问题