天天看點

Linux下采集攝像頭的圖像再儲存為JPG圖檔存放到本地(YUYV轉JPG)

一、環境介紹

作業系統:ubuntu18.04 X64位   和   嵌入式Linux操作(ARM)

二、功能介紹

通過Linux下标準V4L2架構采集攝像頭的資料,通過jpeglib庫連續編碼為JPG格式圖檔存放到本地。

三、安裝jpeglib庫

jpeglib下載下傳位址:

http://www.ijg.org/
Linux下采集攝像頭的圖像再儲存為JPG圖檔存放到本地(YUYV轉JPG)
Linux下采集攝像頭的圖像再儲存為JPG圖檔存放到本地(YUYV轉JPG)
下載下傳之後解壓到linux指定目錄下,進行接解壓、編譯。

wbyq@wbyq:~/work_pc$ tar xvf /mnt/hgfs/linux-share-dir/jpegsrc.v9d.tar.gz
wbyq@wbyq:~/work_pc$ cd jpeg-9d/
wbyq@wbyq:~/work_pc/jpeg-9d$ ./configure 
wbyq@wbyq:~/work_pc/jpeg-9d$ make
wbyq@wbyq:~/work_pc/jpeg-9d$ sudo make install
[sudo] wbyq 的密碼: 
make[1]: 進入目錄“/home/wbyq/work_pc/jpeg-9d”
 /bin/mkdir -p '/usr/local/lib'
 /bin/bash ./libtool   --mode=install /usr/bin/install -c   libjpeg.la '/usr/local/lib'
libtool: install: /usr/bin/install -c .libs/libjpeg.so.9.4.0 /usr/local/lib/libjpeg.so.9.4.0
libtool: install: (cd /usr/local/lib && { ln -s -f libjpeg.so.9.4.0 libjpeg.so.9 || { rm -f libjpeg.so.9 && ln -s libjpeg.so.9.4.0 libjpeg.so.9; }; })
libtool: install: (cd /usr/local/lib && { ln -s -f libjpeg.so.9.4.0 libjpeg.so || { rm -f libjpeg.so && ln -s libjpeg.so.9.4.0 libjpeg.so; }; })
libtool: install: /usr/bin/install -c .libs/libjpeg.lai /usr/local/lib/libjpeg.la
libtool: install: /usr/bin/install -c .libs/libjpeg.a /usr/local/lib/libjpeg.a
libtool: install: chmod 644 /usr/local/lib/libjpeg.a
libtool: install: ranlib /usr/local/lib/libjpeg.a
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/sbin" ldconfig -n /usr/local/lib
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/local/lib
 
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the 'LD_RUN_PATH' environment variable
     during linking
   - use the '-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to '/etc/ld.so.conf'
 
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
 /bin/mkdir -p '/usr/local/bin'
  /bin/bash ./libtool   --mode=install /usr/bin/install -c cjpeg djpeg jpegtran rdjpgcom wrjpgcom '/usr/local/bin'
libtool: install: /usr/bin/install -c .libs/cjpeg /usr/local/bin/cjpeg
libtool: install: /usr/bin/install -c .libs/djpeg /usr/local/bin/djpeg
libtool: install: /usr/bin/install -c .libs/jpegtran /usr/local/bin/jpegtran
libtool: install: /usr/bin/install -c rdjpgcom /usr/local/bin/rdjpgcom
libtool: install: /usr/bin/install -c wrjpgcom /usr/local/bin/wrjpgcom
/bin/bash /home/wbyq/work_pc/jpeg-9d/install-sh -d /usr/local/include
/usr/bin/install -c -m 644 jconfig.h /usr/local/include/jconfig.h
 /bin/mkdir -p '/usr/local/include'
 /usr/bin/install -c -m 644 jerror.h jmorecfg.h jpeglib.h '/usr/local/include'
 /bin/mkdir -p '/usr/local/share/man/man1'
 /usr/bin/install -c -m 644 cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 wrjpgcom.1 '/usr/local/share/man/man1'
 /bin/mkdir -p '/usr/local/lib/pkgconfig'
 /usr/bin/install -c -m 644 libjpeg.pc '/usr/local/lib/pkgconfig'
make[1]: 離開目錄“/home/wbyq/work_pc/jpeg-9d”      

預設的安裝路徑在/usr/local/lib/ 目錄下。

如果是編譯給嵌入式Linux平台使用可以按照下面方法配置:

./configure --prefix=/usr/local/lib CC=arm-linux-gcc --host=arm-linux --enable-shared --enable-static
      

四、核心代碼

video_app.c :  攝像頭采集的主要代碼

#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <poll.h>
#include <stdlib.h>
#include "yuv_to_jpeg.h"
 
int image_height;
int image_width;
unsigned char *image_buffer[4];
int uvc_video_fd;
 
/*
函數功能: 攝像頭裝置初始化
*/
int VideoDeviceInit(char *DEVICE_NAME)
{
    /*1. 打開攝像頭裝置*/
    uvc_video_fd=open(DEVICE_NAME,O_RDWR);
    if(uvc_video_fd<0)
    {
        printf("%s 攝像頭裝置打開失敗!\n",DEVICE_NAME);
        return -1;
    }
    else
    {
        printf("攝像頭打開成功.\n");
    }
    
    /*2 設定攝像頭的屬性*/
    struct v4l2_format format;
    /*2.1 查詢目前攝像頭支援的格式*/
    //目前視訊裝置支援的視訊圖像格式
    struct v4l2_fmtdesc fmt;
    memset(&fmt,0,sizeof(fmt));
    fmt.index = 0;
    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    printf("目前攝像頭支援輸出的圖像格式如下:\n");
    while(ioctl(uvc_video_fd,VIDIOC_ENUM_FMT,&fmt) == 0)
    {
        fmt.index ++ ;
        printf("<'%c%c%c%c'--'%s'>\n",
                fmt.pixelformat & 0xff,(fmt.pixelformat >> 8)&0xff,
                (fmt.pixelformat >> 16) & 0xff,(fmt.pixelformat >> 24)&0xff,
                fmt.description);
    }
 
    /*2.2 設定攝像頭輸出的寬度高度與顔色格式(V4L2_PIX_FMT_YUYV)*/
    memset(&format,0,sizeof(struct v4l2_format));
    format.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; /*表示視訊捕獲裝置*/
    format.fmt.pix.width=2000;  /*預設的寬度*/
    format.fmt.pix.height=2000; /*預設的高度*/
    format.fmt.pix.pixelformat=V4L2_PIX_FMT_YUYV; /*預設的格式*/
    format.fmt.pix.field=V4L2_FIELD_ANY; /*系統自動設定: 幀屬性*/
    if(ioctl(uvc_video_fd,VIDIOC_S_FMT,&format)) /*設定攝像頭的屬性*/
    {
        printf("攝像頭格式設定失敗!\n");
        return -2;
    }
    image_width=format.fmt.pix.width;
    image_height=format.fmt.pix.height;
    printf("攝像頭實際輸出的圖像尺寸:x=%d,y=%d\n",format.fmt.pix.width,format.fmt.pix.height);
    if(format.fmt.pix.pixelformat==V4L2_PIX_FMT_YUYV)
    {
        printf("目前攝像頭支援YUV格式圖像輸出!\n");
    }
    else
    {
        printf("目前攝像頭不支援YUV格式圖像輸出!\n");
        return -3;
    }
    
    /*2.3 設定攝像頭采集的幀率*/
    struct v4l2_streamparm streamparm;
    streamparm.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; /*表示視訊捕獲裝置*/
    streamparm.parm.capture.timeperframe.numerator=1;
    streamparm.parm.capture.timeperframe.denominator=30;
    printf("設定目前攝像頭采集幀率: %d秒%d幀\n",streamparm.parm.capture.timeperframe.numerator,streamparm.parm.capture.timeperframe.denominator);
    if(ioctl(uvc_video_fd,VIDIOC_S_PARM,&streamparm)) /*設定攝像頭的幀率*/
    {
        printf("設定攝像頭采集的幀率失敗!\n");
        return -3;
    }
    if(ioctl(uvc_video_fd,VIDIOC_S_PARM,&streamparm)) /*擷取攝像頭的幀率*/
    {
        printf("擷取攝像頭采集的幀率失敗!\n");
        return -3;
    }
    printf("目前攝像頭實際采集幀率: %d秒%d幀\n",streamparm.parm.capture.timeperframe.numerator,streamparm.parm.capture.timeperframe.denominator);
    
    /*3. 請求緩沖區: 申請攝像頭資料采集的緩沖區*/
    struct v4l2_requestbuffers req_buff;
    memset(&req_buff,0,sizeof(struct v4l2_requestbuffers));
    req_buff.count=4; /*預設要申請4個緩沖區*/
    req_buff.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; /*視訊捕獲裝置*/
    req_buff.memory=V4L2_MEMORY_MMAP; /*支援mmap記憶體映射*/
    if(ioctl(uvc_video_fd,VIDIOC_REQBUFS,&req_buff)) /*申請緩沖區*/
    {
        printf("申請攝像頭資料采集的緩沖區失敗!\n");
        return -4;
    }
    printf("攝像頭緩沖區申請的數量: %d\n",req_buff.count);
 
    /*4. 擷取緩沖區的詳細資訊: 位址,編号*/
    struct v4l2_buffer buff_info;
    memset(&buff_info,0,sizeof(struct v4l2_buffer));
    int i;
    for(i=0;i<req_buff.count;i++)
    {
        buff_info.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; /*視訊捕獲裝置*/
        buff_info.memory=V4L2_MEMORY_MMAP; /*支援mmap記憶體映射*/
        if(ioctl(uvc_video_fd,VIDIOC_QUERYBUF,&buff_info)) /*擷取緩沖區的詳細資訊*/
        {
            printf("擷取緩沖區的詳細資訊失敗!\n");
            return -5;
        }
        /*根據攝像頭申請緩沖區資訊: 使用mmap函數将核心的位址映射到程序空間*/
        image_buffer[i]=mmap(NULL,buff_info.length,PROT_READ|PROT_WRITE,MAP_SHARED,uvc_video_fd,buff_info.m.offset); 
        if(image_buffer[i]==NULL)
        {
            printf("緩沖區映射失敗!\n");
            return -6;
        }
    }
 
    /*5. 将緩沖區放入采集隊列*/
    memset(&buff_info,0,sizeof(struct v4l2_buffer));
    for(i=0;i<req_buff.count;i++)
    {
        buff_info.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; /*視訊捕獲裝置*/
        buff_info.index=i; /*緩沖區的節點編号*/
        buff_info.memory=V4L2_MEMORY_MMAP; /*支援mmap記憶體映射*/
        if(ioctl(uvc_video_fd,VIDIOC_QBUF,&buff_info)) /*根據節點編号将緩沖區放入隊列*/
        {
            printf("根據節點編号将緩沖區放入隊列失敗!\n");
            return -7;
        }
    }
 
    /*6. 啟動攝像頭資料采集*/
    int Type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
    if(ioctl(uvc_video_fd,VIDIOC_STREAMON,&Type))
    {
        printf("啟動攝像頭資料采集失敗!\n");
        return -8;
    }
    return 0;
}
 
int main(int argc,char **argv)
{
    if(argc!=2)
    {
        printf("./app </dev/videoX>\n");
        return 0;
    }
    int err;
    char jpg_file_name[100]; /*存放JPG圖檔名稱*/
    int jpg_cnt=0;
    FILE *jpg_file;
    int jpg_size;
    
    /*1. 初始化攝像頭裝置*/
    err=VideoDeviceInit(argv[1]);
    printf("VideoDeviceInit=%d\n",err);
 
    /*2. 循環讀取攝像頭采集的資料*/
    struct pollfd fds;
    fds.fd=uvc_video_fd;
    fds.events=POLLIN;
 
    /*3. 申請存放JPG的資料空間*/
    unsigned char *jpg_p=malloc(image_height*image_width*3);
    
    struct v4l2_buffer video_buffer;
    while(1)
    {
         /*(1)等待攝像頭采集資料*/
         poll(&fds,1,-1);
         /*(2)取出隊列裡采集完畢的緩沖區*/
         video_buffer.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; /*視訊捕獲裝置*/
         video_buffer.memory=V4L2_MEMORY_MMAP;
         ioctl(uvc_video_fd,VIDIOC_DQBUF,&video_buffer);
         /*(3)處理圖像資料*/
         printf("image_buffer[%d]=%X\n",video_buffer.index,image_buffer[video_buffer.index]);
    
         /*YUV資料轉JPEG格式*/
         jpg_size=yuv_to_jpeg(image_width,image_height,image_height*image_width*3,image_buffer[video_buffer.index],jpg_p,80);
         sprintf(jpg_file_name,"%d.jpg",jpg_cnt++);
         printf("圖檔名稱:%s,位元組大小:%d\n",jpg_file_name,jpg_size);
         
         jpg_file=fopen(jpg_file_name,"wb");
         fwrite(jpg_p,1,jpg_size,jpg_file);
         fclose(jpg_file);
         
         /*(4)将緩沖區再放入隊列*/
         ioctl(uvc_video_fd,VIDIOC_QBUF,&video_buffer);
    }
    return 0;
}      

yuv_to_jpg.c :  YUV格式圖檔轉JPG格式圖檔核心代碼

#include "yuv_to_jpeg.h"
 
#define OUTPUT_BUF_SIZE  4096
typedef struct 
{
  struct jpeg_destination_mgr pub; /* public fields */
  JOCTET * buffer;    /* start of buffer */
  unsigned char *outbuffer;
  int outbuffer_size;
  unsigned char *outbuffer_cursor;
  int *written;
 
} mjpg_destination_mgr;
typedef mjpg_destination_mgr *mjpg_dest_ptr;
 
/******************************************************************************
函數功能: 初始化輸出的目的地
******************************************************************************/
METHODDEF(void) init_destination(j_compress_ptr cinfo) 
{
  mjpg_dest_ptr dest = (mjpg_dest_ptr) cinfo->dest;
 
  /* Allocate the output buffer --- it will be released when done with image */
  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;
}
 
/******************************************************************************
函數功能: 當jpeg緩沖區填滿時調用
******************************************************************************/
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;
}
 
/******************************************************************************
函數功能:在寫入所有資料之後,由jpeg_finish_compress調用。通常需要重新整理緩沖區。
******************************************************************************/
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;
}
 
/******************************************************************************
功能描述:初始化輸出流
函數參數:
        j_compress_ptr cinfo  :儲存JPG圖像壓縮資訊的結構體位址
        unsigned char *buffer :存放壓縮之後的JPG圖檔的緩沖區首位址
        int size              :源圖像位元組總大小
        int *written          :存放壓縮之後的JPG圖像位元組大小
******************************************************************************/
GLOBAL(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;
}
 
/************************************************
功能描述:将YUV格式的資料轉為JPG格式。
函數參數:
    int Width    源圖像寬度
    int Height   源圖像高度
    int size     源圖像位元組總大小
    unsigned char *yuv_buffer :存放YUV源圖像資料緩沖區的首位址
    unsigned char *jpg_buffer :存放轉換之後的JPG格式資料緩沖區首位址
    int quality               :jpg圖像的壓縮品質(值越大品質越好,圖檔就越清晰,占用的記憶體也就越大)
                            一般取值範圍是: 10 ~ 100 。 填10圖檔就有些模糊了,一般的JPG圖檔都是品質都是80。
傳回值:壓縮之後的JPG圖像大小
**************************************************************/
int yuv_to_jpeg(int Width,int Height,int size,unsigned char *yuv_buffer, unsigned char *jpg_buffer, 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;
    
  /*1. 解壓之前的初始化*/
  line_buffer =(unsigned char *)calloc(Width*3,1);
  yuyv=yuv_buffer; //得到圖像源資料
 
  cinfo.err = jpeg_std_error (&jerr);
  jpeg_create_compress (&cinfo);
  
  /* 原版jpeglib庫的标準輸出初始化函數,隻能填檔案指針: jpeg_stdio_dest (&cinfo, file); */
  /* 修改之後的标準輸出初始化函數,将輸出指向記憶體空間*/
  dest_buffer(&cinfo, jpg_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);
 
  /*2. YUV轉RGB格式*/
  z = 0;
  while (cinfo.next_scanline < Height) 
  {
    int x;
    unsigned char *ptr = line_buffer;
 
    for (x = 0; x < Width; x++) {
      int r, g, b;
      int y, u, v;
 
      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 = (y - (88 * u) - (183 * v)) >> 8;
      b = (y + (454 * u)) >> 8;
 
      *(ptr++) = (r > 255) ? 255 : ((r < 0) ? 0 : r);
      *(ptr++) = (g > 255) ? 255 : ((g < 0) ? 0 : g);
      *(ptr++) = (b > 255) ? 255 : ((b < 0) ? 0 : b);
 
      if (z++) {
        z = 0;
        yuyv += 4;
      }
    }
    /*3.進行JPG圖像壓縮(一行一行壓縮)*/
    row_pointer[0] = line_buffer;
    jpeg_write_scanlines(&cinfo, row_pointer, 1);
  }
   
   /*4. 釋放壓縮時占用的記憶體空間*/
  jpeg_finish_compress(&cinfo);
  jpeg_destroy_compress(&cinfo);
  free (line_buffer);
  
   /*5. 傳回壓縮之後JPG圖檔大小*/
  return (written);
}      

yuv_to_jpg.h :  YUV轉JPG代碼的頭檔案

#ifndef YUC_TO_JPEG_H
#define YUC_TO_JPEG_H
#include <stdio.h>
#include <jpeglib.h>
#include <stdlib.h>
#include <string.h>
 
int yuv_to_jpeg(int Width,int Height,int size,unsigned char *yuv_buffer, unsigned char *jpg_buffer, int quality);
#endif      

Makefile 檔案代碼:

all:
    gcc video_app.c yuv_to_jpeg.c -o app -ljpeg      

五、編譯運作-測試效果

如果運作時提示找不到庫,可以先搜尋庫的路徑,将庫的路徑加入到環境變量裡即可:

error while loading shared libraries: libjpeg.so.9: cannot open shared object file: No such file or directory

wbyq@wbyq:/mnt/hgfs/linux-share-dir/linux_c/yuv_save_jpg$ ./app 
./app: error while loading shared libraries: libjpeg.so.9: cannot open shared object file: No such file or directory
wbyq@wbyq:/mnt/hgfs/linux-share-dir/linux_c/yuv_save_jpg$ sudo find / -name libjpeg.so.9
find: ‘/run/user/1000/gvfs’: 權限不夠
/home/wbyq/work_pc/jpeg-9d/.libs/libjpeg.so.9
/usr/local/lib/libjpeg.so.9
wbyq@wbyq:/mnt/hgfs/linux-share-dir/linux_c/yuv_save_jpg$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/      

編譯程式運作步驟如下:

wbyq@wbyq:/mnt/hgfs/linux-share-dir/linux_c/yuv_save_jpg$ make
gcc video_app.c yuv_to_jpeg.c -o app -ljpeg
video_app.c: In function ‘main’:
video_app.c:190:33: warning: format ‘%X’ expects argument of type ‘unsigned int’, but argument 3 has type ‘unsigned char *’ [-Wformat=]
       printf("image_buffer[%d]=%X\n",video_buffer.index,image_buffer[video_buffer.index]);
                                ~^                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                %hhn
wbyq@wbyq:/mnt/hgfs/linux-share-dir/linux_c/yuv_save_jpg$ ./app 
./app: error while loading shared libraries: libjpeg.so.9: cannot open shared object file: No such file or directory
wbyq@wbyq:/mnt/hgfs/linux-share-dir/linux_c/yuv_save_jpg$ sudo find / -name libjpeg.so.9
find: ‘/run/user/1000/gvfs’: 權限不夠
/home/wbyq/work_pc/jpeg-9d/.libs/libjpeg.so.9
/usr/local/lib/libjpeg.so.9
wbyq@wbyq:/mnt/hgfs/linux-share-dir/linux_c/yuv_save_jpg$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/
wbyq@wbyq:/mnt/hgfs/linux-share-dir/linux_c/yuv_save_jpg$ ./app 
./app </dev/videoX>
wbyq@wbyq:/mnt/hgfs/linux-share-dir/linux_c/yuv_save_jpg$ ./app /dev/video0 
攝像頭打開成功.
目前攝像頭支援輸出的圖像格式如下:
<'YUYV'--'YUYV 4:2:2'>
<'MJPG'--'Motion-JPEG'>
攝像頭實際輸出的圖像尺寸:x=1280,y=960
目前攝像頭支援YUV格式圖像輸出!
設定目前攝像頭采集幀率: 1秒30幀
目前攝像頭實際采集幀率: 2秒15幀
攝像頭緩沖區申請的數量: 4
VideoDeviceInit=0
image_buffer[0]=4C129000
圖檔名稱:0.jpg,位元組大小:135722
image_buffer[1]=4BED1000
圖檔名稱:1.jpg,位元組大小:135722
image_buffer[2]=4BC79000
圖檔名稱:2.jpg,位元組大小:135722
image_buffer[3]=4BA21000
圖檔名稱:3.jpg,位元組大小:135722
image_buffer[0]=4C129000
圖檔名稱:4.jpg,位元組大小:132494
image_buffer[1]=4BED1000
圖檔名稱:5.jpg,位元組大小:132494
image_buffer[2]=4BC79000
圖檔名稱:6.jpg,位元組大小:132494
image_buffer[3]=4BA21000
圖檔名稱:7.jpg,位元組大小:132494
image_buffer[0]=4C129000
圖檔名稱:8.jpg,位元組大小:126119
image_buffer[1]=4BED1000
圖檔名稱:9.jpg,位元組大小:126119
image_buffer[2]=4BC79000
圖檔名稱:10.jpg,位元組大小:126119
image_buffer[3]=4BA21000
圖檔名稱:11.jpg,位元組大小:126119      

程式停止,可以按下Ctrl+C。

下面圖檔是采集的效果。

Linux下采集攝像頭的圖像再儲存為JPG圖檔存放到本地(YUYV轉JPG)

繼續閱讀