由于现在部分摄像头采集的格式是YUYV,不能自己转换成jpeg的图片格式,让我们观看相当麻烦。。所以参考了mjpeg-streamer的源码。
从中取得转换函数,通过自己的组合可以实现从YUYV的格式转换成jpeg,以便于我们通过网络实施远程监控。现给出自己组合的源代码如下:
//代码计较繁杂,没去整理,不喜勿喷。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <asm/types.h>
#include <linux/videodev2.h>
#include <jpeglib.h>
#define OUTPUT_BUF_SIZE 4096
#define CLEAR(x) memset (&(x), 0, sizeof (x))
#define WIDTH 320
#define HEIGHT 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;
memc