linux下编写摄像头拍照程序

前几个月在忙项目像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 
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值