linux系統V4L2架構OV3640攝像頭視訊捕獲儲存圖片jpg格式
阿新 • • 發佈:2019-01-25
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?- /*
- * 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
- 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);
- }
/*
* 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);
}