V4L2视频采集jpeg图片–Apple的学习笔记
由于网上下载的V4L2的代码保存后的jpg图片打开后都描述不是jpg格式。所以下载了个YUYV转JPEG格式的代码,但是需要安装jpeglib。
问题1:
安装jpeglib参考《libjpeg 库的安装.txt》
问题2
安装完后gcc cam3 -o cam3.c进行编译报错
对jpeg_std_error未定义的引用
对jpeg_xxx未定义的引用
通过./configure –help找到安装路径默认为/usr/local/bin和/usr/local/lib
进入/usr/local/lib找到了libjpeg。通过grep ‘jpeg_std_error’ libjpeg.a没有找到。
通过nm -e libjpeg.a看到里面是有这个函数的。
说明的我编译的时候没有链接库导致报错。同时网上也搜索了下解决方法就是添加-ljpge即可
问题3
gcc cam3 -o cam3.c -ljpeg编译通过。
运行./cam3保存的test-mmap.jpg是一个完整的图像,可是看不到内容。
于是猜测分辨率问题,于是把高度和宽度改成640和480,能正常显示图片。
问题4
可是看起来模糊,于是我把质量参数80改为100稍微感觉效果好些。
学习的东东
- select函数使用
- calloc和malloc区别
- 图像基本格式YUV和RGB
- V4L2应用变成的步骤
4.1 打开视频设备文件,进行视频采集的参数初始化,通过V4L2接口设置视频图像的采集窗口、采集的点阵大小和格式;
4.2 申请若干视频采集的帧缓冲区,并将这些帧缓冲区从内核空间映射到用户空间,便于应用程序读取/处理视频数据;
4.3 将申请到的帧缓冲区在视频采集输入队列排队,并启动视频采集;
4.4 驱动开始视频数据的采集,应用程序从视频采集输出队列取出帧缓冲区,处理完后,将帧缓冲区重新放入视频采集输入队列,循环往复采集连续的视频数据;
4.5 停止视频采集。
最后的代码cam3.c如下
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define OUTPUT_BUF_SIZE 4096
#define CLEAR(x) memset (&(x), 0, sizeof (x))
#define WIDTH 640//320
#define HEIGHT 480//240
struct buffer {
void *start;
size_t length;
};
typedef struct {
struct jpeg_destination_mgr pub;
JOCTET * buffer;
unsigned char *outbuffer;
int outbuffer_size;
unsigned char *outbuffer_cursor;
int *written;
}mjpg_destination_mgr;
typedef mjpg_destination_mgr *mjpg_dest_ptr;
static char * dev_name = "/dev/video0";
static int fd = -1;
struct buffer * buffers = NULL;
static unsigned int n_buffers = 0;
FILE *file_fd;
static unsigned long file_length;
static unsigned char *file_name;
METHODDEF(void) init_destination(j_compress_ptr cinfo) {
mjpg_dest_ptr dest = (mjpg_dest_ptr) cinfo->dest;
dest->buffer = (JOCTET *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, OUTPUT_BUF_SIZE * sizeof(JOCTET));
*(dest->written) = 0;
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
}
METHODDEF(boolean) empty_output_buffer(j_compress_ptr cinfo) {
mjpg_dest_ptr dest = (mjpg_dest_ptr) cinfo->dest;
memcpy(dest->outbuffer_cursor, dest->buffer, OUTPUT_BUF_SIZE);
dest->outbuffer_cursor += OUTPUT_BUF_SIZE;
*(dest->written) += OUTPUT_BUF_SIZE;
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
return TRUE;
}
METHODDEF(void) term_destination(j_compress_ptr cinfo) {
mjpg_dest_ptr dest = (mjpg_dest_ptr) cinfo->dest;
size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
/* Write any data remaining in the buffer */
memcpy(dest->outbuffer_cursor, dest->buffer, datacount);
dest->outbuffer_cursor += datacount;
*(dest->written) += datacount;
}
void dest_buffer(j_compress_ptr cinfo, unsigned char *buffer, int size, int *written) {
mjpg_dest_ptr dest;
if (cinfo->dest == NULL) {
cinfo->dest = (struct jpeg_destination_mgr *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(mjpg_destination_mgr));
}
dest = (mjpg_dest_ptr)cinfo->dest;
dest->pub.init_destination = init_destination;
dest->pub.empty_output_buffer = empty_output_buffer;
dest->pub.term_destination = term_destination;
dest->outbuffer = buffer;
dest->outbuffer_size = size;
dest->outbuffer_cursor = buffer;
dest->written = written;
}
//摄像头采集的YUYV格式转换为JPEG格式
int compress_yuyv_to_jpeg(unsigned char *buf, unsigned char *buffer, int size, int quality) {
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1];
unsigned char *line_buffer, *yuyv;
int z;
static int written;
//int count = 0;
//printf("%sn", buf);
line_buffer = calloc (WIDTH * 3, 1);
yuyv = buf;//将YUYV格式的图片数据赋给YUYV指针
printf("compress start...n");
cinfo.err = jpeg_std_error (&jerr);
jpeg_create_compress (&cinfo);
/* jpeg_stdio_dest (&cinfo, file); */
dest_buffer(&cinfo, buffer, size, &written);
cinfo.image_width = WIDTH;
cinfo.image_height = HEIGHT;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults (&cinfo);
jpeg_set_quality (&cinfo, quality, TRUE);
jpeg_start_compress (&cinfo, TRUE);
z = 0;
while (cinfo.next_scanline > 8;
g = (y - (88 * u) - (183 * v)) >> 8;
b = (y + (454 * u)) >> 8;
*(ptr++) = (r > 255) ? 255 : ((r 255) ? 255 : ((g 255) ? 255 : ((b > 8) & 0xFF,
(fmt1.pixelformat >> 16) & 0xFF, (fmt1.pixelformat >> 24) & 0xFF,
fmt1.description);
}
CLEAR (fmt);
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = 640;
fmt.fmt.pix.height = 480;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
ff = ioctl (fd, VIDIOC_S_FMT, &fmt); //设置图像格式
if(ff
文章来源于互联网,如有雷同请联系站长删除:V4L2视频采集jpeg图片–Apple的学习笔记