1. 程式人生 > >linux系統V4L2架構OV3640攝像頭視訊捕獲儲存圖片jpg格式

linux系統V4L2架構OV3640攝像頭視訊捕獲儲存圖片jpg格式

2、執行流程:

(1)開啟裝置:cameraOpen()

(2)裝置初始化:cameraInit()

(3)建立記憶體對映:mmapInit()

(4)開始視訊採集並捕獲影象資料:captureStart()

(5)迴圈採集:mainLoop()

(6)讀取資料:frameRead()

(7)資料處理:imageProcess()

3、常見錯誤及解決方法:

(1) No capture device info

VIDIOC_REQBUFS error 19, No such device

這個錯誤糾結了很久很久,在網上搜了很久,在群裡問了很多人,都沒解決,最後求助出售此試驗箱裝置公司的工程師,他也沒給出原因,只是給了我一個可以執行的例程,我就自己對照了下,然後除錯,查出來,原因在與,沒有設定

二、儲存jpg圖片程式:

1、原始碼:jpg.c

[cpp] view plaincopyprint?
  1. /*
  2. * jpg.c
  3. *
  4. */
  5. #include "classroom.h"
  6. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7. #define jpgImageName "test.jpg"
  8. extern char * chpt_lcd_mmap_addr;
  9. extern unsigned int int_lcd_height;
  10. extern unsigned int int_lcd_width;
  11. extern unsigned int int_lcd_pixel;
  12. static unsigned int width;
  13. static
    unsigned int height;
  14. static unsigned int channel;
  15. static unsigned char jpegQuality = 100;
  16. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. static void jpegWrite(unsigned char* img)
  18. {
  19. struct jpeg_compress_struct cinfo;
  20. struct jpeg_error_mgr jerr;
  21. JSAMPROW row_pointer[1];
  22. FILE *outfile = fopen( jpgImageName, "w");
  23. // try to open file for saving
  24. if (!outfile) {
  25. errno_exit("jpeg");
  26. }
  27. // create jpeg data
  28. cinfo.err = jpeg_std_error( &jerr );
  29. jpeg_create_compress(&cinfo);
  30. jpeg_stdio_dest(&cinfo, outfile);
  31. // set image parameters
  32. cinfo.image_width = width;
  33. cinfo.image_height = height;
  34. cinfo.input_components = 3;
  35. cinfo.in_color_space = JCS_RGB;
  36. // set jpeg compression parameters to default
  37. jpeg_set_defaults(&cinfo);
  38. // and then adjust quality setting
  39. jpeg_set_quality(&cinfo, jpegQuality, TRUE);
  40. // start compress
  41. jpeg_start_compress(&cinfo, TRUE);
  42. // feed data
  43. while (cinfo.next_scanline < cinfo.image_height)
  44. {
  45. row_pointer[0] = &img[(cinfo.image_height - cinfo.next_scanline - 1) * cinfo.image_width*3];
  46. jpeg_write_scanlines(&cinfo, row_pointer, 1);
  47. }
  48. // finish compression
  49. jpeg_finish_compress(&cinfo);
  50. // destroy jpeg data
  51. jpeg_destroy_compress(&cinfo);
  52. // close output file
  53. fclose(outfile);
  54. }
  55. void jpgImageProduct(const void* p)
  56. {
  57. usleep(500000);
  58. unsigned char* dst;
  59. unsigned char* src = (unsigned char*)p;
  60. unsigned int j;
  61. unsigned int i;
  62. width = int_lcd_width;
  63. height = int_lcd_height;
  64. dst = (unsigned char*)malloc (width*height*3+66);
  65. for (i=0; i< height; i++)
  66. {
  67. for(j=0;j<width;j++)
  68. {
  69. memcpy(dst+(i*width+j)*3, src+(i*width+j)*4+2,1);
  70. memcpy(dst+(i*width+j)*3+1, src+(i*width+j)*4+1,1);
  71. memcpy(dst+(i*width+j)*3+2, src+(i*width+j)*4,1);
  72. }
  73. }
  74. jpegWrite(dst);
  75. free(dst);
  76. }
/*
 *     jpg.c
 *
 */

#include "classroom.h"

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define jpgImageName "test.jpg"

extern char * chpt_lcd_mmap_addr;
extern unsigned int int_lcd_height;
extern unsigned int int_lcd_width;
extern unsigned int int_lcd_pixel;

static unsigned int width;
static unsigned int height;
static unsigned int channel;

static unsigned char jpegQuality = 100;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

static void jpegWrite(unsigned char* img)
{
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
	
  JSAMPROW row_pointer[1];
  FILE *outfile = fopen( jpgImageName, "w");

  // try to open file for saving
  if (!outfile) {
    errno_exit("jpeg");
  }

  // create jpeg data
  cinfo.err = jpeg_std_error( &jerr );
  jpeg_create_compress(&cinfo);
  jpeg_stdio_dest(&cinfo, outfile);

  // set image parameters
  cinfo.image_width = width;	
  cinfo.image_height = height;
  cinfo.input_components = 3;
  cinfo.in_color_space = JCS_RGB;

  // set jpeg compression parameters to default
  jpeg_set_defaults(&cinfo);
  // and then adjust quality setting
  jpeg_set_quality(&cinfo, jpegQuality, TRUE);

  // start compress 
  jpeg_start_compress(&cinfo, TRUE);

  // feed data
  while (cinfo.next_scanline < cinfo.image_height) 
  {
	row_pointer[0] = &img[(cinfo.image_height - cinfo.next_scanline - 1) * cinfo.image_width*3];
    jpeg_write_scanlines(&cinfo, row_pointer, 1);
  }

  // finish compression
  jpeg_finish_compress(&cinfo);

  // destroy jpeg data
  jpeg_destroy_compress(&cinfo);

  // close output file
  fclose(outfile);
}


void jpgImageProduct(const void* p)
{
	usleep(500000);

	unsigned char* dst;
	unsigned char* src = (unsigned char*)p;
	unsigned int j;
	unsigned int i;

	width = int_lcd_width;
    height = int_lcd_height;

	dst = (unsigned char*)malloc (width*height*3+66);

	for (i=0; i< height; i++)
	{
		for(j=0;j<width;j++)
		{
			memcpy(dst+(i*width+j)*3, src+(i*width+j)*4+2,1);
			memcpy(dst+(i*width+j)*3+1, src+(i*width+j)*4+1,1);
			memcpy(dst+(i*width+j)*3+2, src+(i*width+j)*4,1);
		}
	}
	jpegWrite(dst);
	free(dst);
}