前几个月在忙项目像linux系统移植的事情,遇到了不少的问题,其中一个问题就是linux 下使用摄像头;
由于项目需要调用摄像头进行获取图像操作,java工程师也不知道在linux使用摄像头的接口,急需一个接口打通上层应用和底层驱动之间的问题。linux中摄像头驱动采用的是V4L2标准,所以可以查看内核的V4l2例子进行修改,修改后的程序如下:
#include "video.h"
//摄像头采集的YUYV格式转换为JPEG格式
int compress_yuyv_to_jpeg(unsigned char *buf, FILE *fp, int quality) {
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1];
unsigned char *yuyv;
unsigned char line_buffer[1920];
int z;
static int written;
unsigned char *ptr;
int x;
int r, g, b;
int y, u, v;
//int count = 0;
//printf("%s\n", 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); */
jpeg_stdio_dest(&cinfo, fp);
if(debug1)
printf("compress start1 \n");
//dest_buffer(&cinfo, buffer, size, &written);
if(debug1)
printf("compress start2 \n");
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);
if(debug1)
printf("compress start3 \n");
z = 0;
while (cinfo.next_scanline < HEIGHT) {
//int x;
ptr= line_buffer;
for (x = 0; x < WIDTH; x++) {
r=g=b=0;
y=u=v=0;
if (!z)
y = yuyv[0] << 8;
else
y = yuyv[2] << 8;
u = yuyv[1] - 128;
v = yuyv[3] - 128;
r = (y + (359 * v)) >> 8;
g