Source code for camera capture in Linux

Source: Internet
Author: User
Tags color representation

/*************************************** *************/

/**/

/* V4lgrab. H */

/**/

/*************************************** *************/

# Ifndef _ v4lgrab_h __

# DEFINE _ v4lgrab_h __

# Define widthbytes (I) (I + 31)/32*4)

# Include <stdio. h>

// Typedef Enum {false = 0, true = 1, o67

// # Define swap_hl_word (x) {(x) = (x) <8) | (x)> 8 );}

// # Define swap_hl_dword (x) {(x) = (x) <24) | (x)> 24) | (X) & 0xff0000)> 8) | (x) & 0xff00) <8 );}

# Define free (x) if (x) {free (x); (x) = NULL ;}

Typedef unsigned char byte;

Typedef unsigned short word;

Typedef unsigned long DWORD;

Typedef struct tagbitmapfileheader {

Word bftype; // the flag of BMP, value is "BM"

DWORD bfsize; // size BMP file, unit is bytes

DWORD bfreserved1; // 0

DWORD bfreserved2; // 0

DWORD bfoffbits; // must be 54

} Bitmapfileheader;

Typedef struct tagbitmapinfoheader {

DWORD bisize; // must be 0x28

DWORD biwidth ;//

DWORD biheight ;//

Word biplanes; // must be 1

Word bibitcount ;//

DWORD bicompression ;//

DWORD bisizeimage ;//

DWORD bixpelspermeter ;//

DWORD biypelspermeter ;//

DWORD biclrused ;//

DWORD biclrimportant ;//

} Bitmapinfoheader;

Typedef struct tagrgbquad {

Byte rgbblue;

Byte rgbgreen;

Byte rgbred;

Byte rgbreserved;

} Rgbquad;

# If defined (_ cplusplus)

Extern "C" {/* Make sure we have C-declarations in C ++ programs */

# Endif

// If successful return 1, or else return 0

Int openvideo ();

Int closevideo ();

// Data space used to store data, size space size

Void getvideodata (unsigned char * data, int size );

# If defined (_ cplusplus)

}

# Endif

# Endif // _ v4lgrab_h ___

//-------------------------------------------------------------------------------
 
/*************************************** *************/

/**/

/* V4lgrab. C */

/**/

/*************************************** *************/

# Include <unistd. h>

# Include <sys/types. h>

# Include <sys/STAT. h>

# Include <fcntl. h>

# Include <stdio. h>

# Include <sys/IOCTL. h>

# Include <stdlib. h>

# Include <Linux/types. h>

# Include <Linux/videodev. h>

# Include "v4lgrab. H"

# Define true 1

# Define false 0

# Define file_video "/dev/video0"

# Define imagewidth 640
# Define imageheight 480

Static int FD;

Static struct video_capability cap;

Static struct video_window win;

Static struct video_picture vpic;

Static struct video_mmap mapbuf;

Int openvideo ()

{

// Open video device

FD = open (file_video, o_rdonly );

If (FD <0 ){

Perror (file_video );

Return (false );

}

// Get capabilities

If (IOCTL (FD, vidiocgcap, & Cap) <0 ){

Perror ("vidiogcap ");

Printf ("(" file_video "not a video4linux device ?) \ N ");

Close (FD );

Return (false );

}

Printf ("devicename --> % s \ n", Cap. Name );
Printf ("devicetape --> % d \ n", Cap. type );
Printf ("channels --> % d \ n", Cap. channels );
Printf ("maxwidth --> % d \ n", Cap. maxwidth );
Printf ("maxheith --> % d \ n", Cap. maxheight );
Printf ("minwidth --> % d \ n", Cap. minwidth );
Printf ("minheith --> % d \ n", Cap. minheight );

// Get the video overlay window

If (IOCTL (FD, vidiocgwin, & Win) <0 ){

Perror ("vidiocgwin ");

Close (FD );

Return (false );

}

// Set the video overlay window

Win. width = imagewidth;

Win. Height = imageheight;

If (IOCTL (FD, vidiocswin, & Win) <0 ){

Perror ("vidiocswin ");

Close (FD );

Return (false );;

}

// Get picture Properties

If (IOCTL (FD, vidiocgpict, & vpic) <0 ){

Perror ("vidiocgpict ");

Close (FD );

Return (false );

}

// Mapbuf. width = 600;
// Mapbuf. Height = 480;

If (cap. Type & vid_type_monochrome ){

} Else {

Vpic. Depth = 32;
// The common color depth is 1-bit, 8-bit, 24-bit, and 32-bit. One digit has two possible values: 0 or 1. A large color depth (more bits per pixel) means that a digital image has more available colors and more precise color representation.
Vpic. palette = video_palette_rgb24;
Vpic. brightness = 5000; // 3000-12000 is suitable
Vpic. Color = 65535;
Vpic. Whiteness = 65535; // 65-65535 is slightly different
If (IOCTL (FD, vidiocspict, & vpic) <0 ){

Printf ("unable to find a supported capture format. \ n ");

Return (false );

}

}

Return (true );

}

Int closevideo ()

{

If (FD! =-1 ){

Close (FD );

Return (true );

}

Return (false );

}

Void getvideodata (unsigned char * data, int size)

{

Size = read (FD, Data, size );

}

//-------------------------------------------------------------------------------
 
/*************************************** *************/

/**/

/* Temp_get.c */

/**/

/*************************************** *************/

# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <stdio. h>
# Include <sys/IOCTL. h>
# Include <stdlib. h>
# Include <Linux/types. h>
# Include <Linux/videodev. h>
# Include "v4lgrab. H"

# Define BMP "image2.bmp"

# Define imagewidth 640
# Define imageheight 480

Int main (INT argc, char ** argv)

{

File * FP;

Bitmapfileheader BF;

Bitmapinfoheader Bi;

Unsigned char * buffer;

Int I;

Fp = fopen (BMP, "WB ");

If (! FP ){

Perror (BMP );
Exit (1 );

}


If (openvideo () =-1 ){

Return (-1 );

}

// Set bitmapinfoheader

Bi. bisize = 40;

Bi. biwidth = imagewidth;

Bi. biheight = imageheight;

Bi. biplanes = 1;

Bi. bibitcount = 24;

Bi. bicompression = 0;

Bi. bisizeimage = widthbytes (Bi. biwidth * bi. bibitcount) * bi. biheight;

Bi. bixpelspermeter = 0;

Bi. biypelspermeter = 0;

Bi. biclrused = 0;

Bi. biclrimportant = 0;

// Set bitmapfileheader

BF. bftype = 0x4d42;

BF. bfsize = 54 + bi. bisizeimage;

BF. bfreserved1 = 0;

BF. bfreserved2 = 0;

BF. bfoffbits = 54;

Buffer = malloc (Bi. bisizeimage );

If (! Buffer ){

Printf ("out of memory. \ n ");

Exit (1 );

}

For (I = 0; I <20; I ++)

Getvideodata (buffer, Bi. bisizeimage );

Fwrite (& BF, 14, 1, FP );

Fwrite (& BI, 40, 1, FP );

Fwrite (buffer, Bi. bisizeimage, 1, FP );

Printf ("Get BMP form video \ t [OK] \ n ");

Fclose (FP );

Closevideo ();

Return 0;

}

//------------------------------------------------------------------------------
Extracted from the net, after debugging modification. In gcc4.1 arm-linux-gcc-3.4.1 compiled through.
It can run normally on the S3C2410 PC.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.