capture_init.cpp~ ( File view )

  • By rarshion 2014-11-04
  • View(s):131
  • Download(s):3
  • Point(s): 2
			#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h> 
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <asm/types.h>
#include <linux/videodev2.h>
#include <time.h>
#include "struct.hpp"


extern "C"{

#include <jpeglib.h>

}


#include "capture.h"


#define CLEAR(x) memset (&(x), 0, sizeof (x))

struct buffer {

        void *                  start;
        size_t                  length;

};

 int          fd              = -1;
 struct buffer *      buffers         = NULL;
 unsigned int     n_buffers       = 0;
 unsigned int width = 2048;
 unsigned int height = 1944;
 unsigned char jpegQuality = 80;
 char  jpegFilename[80];
 char* deviceName = "/dev/video0";
 int              br_v            = 0; //value brightness[-3-0-3] 
 int              cont_v          = 0; //value contrast [-2-0-2]
 int              sat_v           = 0; //value saturation [-3-0-3]
 int              wb_v            = 0; //value  wb 1.Auto 2.sunny 3.cloudy 4.office 5.Home
 int              e_v            = 0; //value exposure  [-5-0-5]
 int             rc_v            =1; //value resolution [0-5]
 int             jpegc             =0;
 int             CapMode            =0; //0:picture 1:video




 void errno_exit(const char* s)
{

  //printf(stderr, "%s error %d, %s\n", s, errno, strerror (errno));
  exit(EXIT_FAILURE);

}


 int xioctl(int fd, int request, void* argp)
{

  int r;

  do r = ioctl(fd, request, argp);
  while (-1 == r && EINTR == errno);

  return r;

}


/**
  start capturing
*/
 void captureStart(void)
{

  printf("-----captureStart ------\n");
  unsigned int i;
  enum v4l2_buf_type type;
  struct v4l2_control ctl;
  struct v4l2_streamparm strp;
  struct v4l2_captureparm capt;

      for (i = 0; i < n_buffers; ++i) 
      {

        struct v4l2_buffer buf;
        CLEAR (buf);
        buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        buf.memory      = V4L2_MEMORY_MMAP;
        buf.index       = i;
        if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
          errno_exit("VIDIOC_QBUF");
        else
         {

             // //printf("success to VIDIOC_QBUF\n");
         
}
      
}

     //1.camera init resolution 640*480
      type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
      if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
        errno_exit("VIDIOC_STREAMON");
      else
         {

             // //printf("success to VIDIOC_STREAMON\n");
         
}


    //2.camera change to other resolution
 
    capt.capturemode = V4L2_MODE_HIGHQUALITY; //change to the QXGA
    capt.timeperframe.numerator = rc_v;  
    strp.parm.capture = capt;
      if(-1==xioctl(fd,VIDIOC_S_PARM,&strp))
      {

        // printf("erro to set_set_capturesspram\n");
      
}
       else 
       {

             //printf("success to set_capturesspram\n");
       
}
 


    //2.image special effect
     ctl.id = V4L2_CID_DO_WHITE_BALANCE; //set while_balance
     ctl.value = wb_v;
    if(-1==xioctl(fd,VIDIOC_S_CTRL,&ctl))
     {

         // printf("erro to set_white_balance\n");
     
}
       else 
     {

            //printf("success to set_white_balance\n");
     
}

   
      ctl.id = V4L2_CID_BRIGHTNESS;  //set  brightness
      ctl.value=br_v;
      if(-1==xioctl(fd,VIDIOC_S_CTRL,&ctl))
      {

        // printf("erro to set_brightness\n");
      
}
       else 
       {

          //   printf("success to set_brightness\n");
       
}


       ctl.id = V4L2_CID_CONTRAST ;  //set contrast 
       ctl.value=cont_v;
      if(-1==xioctl(fd,VIDIOC_S_CTRL,&ctl))
      {

         //printf("erro to set_contrast\n");
      
}
       else 
       {

             //printf("success to set_contrast\n");
       
}

       ctl.id = V4L2_CID_SATURATION ;   //set saturation
       ctl.value=sat_v;
      if(-1==xioctl(fd,VIDIOC_S_CTRL,&ctl))
      {

         //printf("erro to set_saturation\n");
      
}
       else 
       {

             //printf("success to set_saturation\n");
       
}


     sleep(5);
  

}



 void mmapInit(void)
{

  //printf("-----start mmapInit------\n");
  struct v4l2_requestbuffers req;
  CLEAR (req);
  req.count               = 4;
  req.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  req.memory              = V4L2_MEMORY_MMAP;

  if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) 
  {

    if (EINVAL == errno) 
    {

     // //printf(stderr, "%s does not support memory mapping\n", deviceName);
      exit(EXIT_FAILURE);
    
} 
    else 
    {

      errno_exit("VIDIOC_REQBUFS");
    
}
  
}

  if (req.count < 2) {

  //  //printf(stderr, "Insufficient buffer memory on %s\n", deviceName);
    exit(EXIT_FAILURE);
  
}

  buffers = (buffer*)calloc(req.count, sizeof(*buffers));

  if (!buffers)
  {

    //printf(stderr, "can't calloc the buffers Out of memory\n");
    exit(EXIT_FAILURE);
  
}
  else
  {

   // //printf("success to malloc buffers\n");
  
}
  for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {

    struct v4l2_buffer buf;

    CLEAR (buf);

    buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buf.memory      = V4L2_MEMORY_MMAP;
    buf.index       = n_buffers;

    if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
      errno_exit("VIDIOC_QUERYBUF");
    else
    {

      // //printf("success to init VIDIOC_QUERYBUF\n");
    
}

    buffers[n_buffers].length = buf.length;
    buffers[n_buffers].start =
    mmap (NULL /* start anywhere */, buf.length, PROT_READ | PROT_WRITE /* required */, MAP_SHARED /* recommended */, fd, buf.m.offset);

    if (MAP_FAILED == buffers[n_buffers].start)
     {

       errno_exit("failed to init mmap"); 
     
}
     else
     {

         //printf("success to init mmap\n");
     
}
  
}

}


/**
  initialize device
*/
 void deviceInit(void)
{

  //printf("-----deviceInit-----\n");
  struct v4l2_capability cap;
  struct v4l2_cropcap cropcap;
  struct v4l2_crop crop;
  struct v4l2_format fmt;
  unsigned int min;

  if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) 
  {

    if (EINVAL == errno) 
    {

      printf("no device \n");
      exit(EXIT_FAILURE);
    
} 
    else 
    {

       printf("failed to vidioc_quercap \n");
    	errno_exit("VIDIOC_QUERYCAP");
    
}
  
}
  else
  {

      printf("success to VIDIOC_QUERYCAP\n");
  
}

  if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) 
  {

    //printf(stderr, "%s is no video capture device\n",deviceName);
    printf("no capture device\n");
    exit(EXIT_FAILURE);
  
}

 
  /* Select video input, video standard and tune here. */
  if(CLEAR(cropcap))
  {

     // //printf("sucess to mement the cropscap \n");
  
}
   else
   {

    //  //printf("failed to mement the cropscap \n");
   
}
  cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) 
  {

    crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    crop.c = cropcap.defrect; /* reset to default */

    if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop))
    {

      switch (errno) 
     {

        case EINVAL:
          /* Cropping not supported. */
          break;
        default:
          /* Errors ignored. */
          break;
      
}
    
}
  
} 
  else 
  {
        
    /* Errors ignored. */
  
}

  if(CLEAR(fmt))
  {

     // //printf("sucess to mement the cropscap \n");
  
}
   else
   {

     // //printf("failed to mement the cropscap \n");
   
}

  // v4l2_format
  fmt.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  fmt.fmt.pix.width       = width; 
  fmt.fmt.pix.height      = height;
  fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  fmt.fmt.pix.field       = V4L2_FIELD_INTERLACED;

  if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
  {

    errno_exit("failed to VIDIOC_S_FMT");
  
}
  else
  {

   // //printf("success to VIDIOC_S_FMT\n");
  
}
  /* Note VIDIOC_S_FMT may change width and height. */
  if (width != fmt.fmt.pix.width) {

    width = fmt.fmt.pix.width;
    //printf(stderr,"Image width set to %i by device %s.\n",width,deviceName);
  
}
  if (height != fmt.fmt.pix.height) {

    height = fmt.fmt.pix.height;
    //printf(stderr,"Image height set to %i by device %s.\n",height,deviceName);
  
}

  /* Buggy driver paranoia. */
  min = fmt.fmt.pix.width * 2;
  if (fmt.fmt.pix.bytesperline < min)
    fmt.fmt.pix.bytesperline = min;
  min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
  if (fmt.fmt.pix.sizeimage < min)
    fmt.fmt.pix.sizeimage = min;

      mmapInit();


}

/**
  open device
*/
 void deviceOpen(void)
{

  printf("-----start opendevice-----\n");
  struct stat st;
 
  if (-1 == stat(deviceName, &st))
  {

    //printf(stderr, "Cannot identify '%s': %d, %s\n", deviceName, errno, strerror (errno));
    printf("erro to identify\n");
    exit(EXIT_FAILURE);
  
}
  else
  {

      printf("success to identify \n");
  
}

  if (!S_ISCHR (st.st_mode))
 {

    //printf(stderr, "%s is no device\n", deviceName);
    printf("is no device\n");
    exit(EXIT_FAILURE);
  
}


  fd = open(deviceName, O_RDWR /* required */ | O_NONBLOCK, 0);

  if (-1 == fd) 
 {

    //printf(stderr, "Cannot open '%s': %d, %s\n", deviceName, errno, strerror (errno));
     printf("erro to open\n");
    exit(EXIT_FAILURE);
  
}
   else
  {

     printf("success to open device  \n");
  
}

}


void capture_init(struct PhotoAttr& attr,int capturemode) 
{

  //printf("----start capture_init------\n");

  rc_v = attr.resolution;

 if(rc_v == 1)
 {

   width = 2048;
   height = 1944;
 
}
 else if(rc_v ==2)
 {

   width = 2048;
   height= 1536;
 
}
 else if(rc_v ==3)
 {

  width = 1600;
  height = 1200;
 
}
 else if(rc_v ==4)
 {

  width = 1280;
  height = 960;
 
}
 else if(rc_v ==5)
 {

  width = 800;
  height=600;
 
}
 else
 {

   width = 640;
   height = 480;
 
}

  //width = attr.width;
 // height = attr.height;

  printf("width=%d,height=%d\n",width,height);
  jpegQuality = attr.quality; 
  br_v = attr.bright; 
  cont_v = attr.contrast; 
  sat_v = attr.saturation;
  wb_v  = attr.whitebalace; 


 

  CapMode =  capturemode;

  deviceOpen();
  deviceInit();

  printf("finish capture_inint sleep 5's \n");
  

 


}





			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 2

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
arm_capture.o7.44 kB23-09-14 11:04
arm_capture_init.o6.25 kB23-09-14 11:04
arm_capture_uinit.o2.11 kB23-09-14 11:04
arm_capvideo.o18.83 kB23-09-14 11:04
arm_capvideo_init.o6.44 kB23-09-14 11:04
arm_capvideo_uinit.o2.16 kB23-09-14 11:04
arm_Client.o102.76 kB03-11-14 23:23
arm_ClientPort99.95 kB03-11-14 23:23
arm_Common.o2.89 kB23-09-14 11:04
arm_Connector.o37.29 kB23-09-14 11:04
arm_control146.15 kB04-11-14 12:37
arm_Father9.37 kB23-09-14 08:26
arm_Functions.o2.02 kB23-09-14 11:04
arm_Lock.o1.60 kB23-09-14 11:04
arm_Logger.o7.59 kB23-09-14 11:04
arm_Mutex.o6.47 kB23-09-14 11:04
arm_Sem.o4.98 kB23-09-14 11:04
arm_ShareBuff.o21.13 kB23-09-14 11:04
arm_ShareMemory.o7.59 kB23-09-14 11:04
arm_ShmIPS.o12.88 kB23-09-14 11:04
arm_Socket.o1.16 kB23-09-14 11:04
arm_SocketOps.o5.43 kB23-09-14 11:04
arm_SocketStream.o6.77 kB23-09-14 11:04
arm_SsbSipH264Encode.o4.00 kB23-09-14 11:04
arm_TcpClient.o50.54 kB23-09-14 11:04
arm_Thread.o5.96 kB23-09-14 11:04
arm_Transform.o11.05 kB23-09-14 11:04
arm_Video.o22.58 kB23-09-14 11:04
capture.cpp4.76 kB12-12-13 15:12
capture.cpp~4.76 kB23-11-13 09:17
capture.h1,009.00 B23-11-13 09:17
capture.o5.05 kB23-09-14 11:03
capture_init.cpp9.78 kB12-12-13 14:49
capture_init.cpp~9.78 kB12-12-13 14:49
capture_init.o6.09 kB23-09-14 11:03
capture_uinit.cpp1.32 kB23-11-13 09:17
capture_uinit.o1.78 kB23-09-14 11:03
capvideo.cpp8.17 kB07-01-14 19:34
capvideo.cpp~8.15 kB23-11-13 09:17
capvideo.h527.00 B23-11-13 09:17
capvideo.o12.45 kB23-09-14 11:03
capvideo_init.cpp8.04 kB23-11-13 09:17
capvideo_init.o6.01 kB23-09-14 11:03
capvideo_uinit.cpp1.69 kB23-11-13 09:17
capvideo_uinit.o1.82 kB23-09-14 11:03
Client.cpp6.93 kB03-11-14 23:19
Client.cpp~6.91 kB30-03-14 18:48
Client.hpp1.03 kB23-11-13 09:17
Client.o66.13 kB03-11-14 23:23
Client11_19.pro1.27 kB23-11-13 09:17
ClientPort87.28 kB03-11-14 23:23
Common.cpp358.00 B23-11-13 09:17
Common.hpp308.00 B23-11-13 09:17
Common.o2.57 kB23-09-14 11:03
Connector.cpp2.37 kB23-09-14 10:28
Connector.cpp~2.37 kB16-03-14 20:18
Connector.hpp715.00 B16-03-14 21:59
Connector.hpp~715.00 B23-11-13 09:17
Connector.o24.09 kB23-09-14 11:03
control128.54 kB04-11-14 12:37
Control.cpp11.11 kB04-11-14 12:29
Control.cpp~10.58 kB07-04-14 21:25
<DeviceControl.cpp>0.00 B23-09-14 11:09
DeviceControl.hpp171.00 B23-09-14 11:09
Father12.37 kB23-09-14 08:25
Father.cpp1.89 kB21-02-14 15:48
Father.cpp~1.89 kB23-11-13 09:17
Functions.cpp852.00 B23-11-13 09:17
Functions.hpp317.00 B23-11-13 09:17
Functions.o1.48 kB23-09-14 11:03
IIPS.hpp328.00 B23-11-13 09:17
IMutex.hpp194.00 B23-11-13 09:17
IPC.hpp824.00 B23-11-13 09:17
Lock.cpp168.00 B23-11-13 09:17
Lock.hpp453.00 B23-11-13 09:17
Lock.o1.21 kB23-09-14 11:03
Logger.cpp1.95 kB23-11-13 09:17
Logger.hpp1.40 kB23-11-13 09:17
Logger.o6.40 kB23-09-14 11:03
LogMsg.c1.22 kB23-11-13 09:17
LogMsg.h370.00 B23-11-13 09:17
main.cpp1.24 kB23-11-13 09:17
makefile4.00 kB09-04-14 22:33
makefile~4.00 kB23-11-13 09:17
MfcDriver.h1.71 kB23-11-13 09:17
MfcDrvParams.h4.41 kB23-11-13 09:17
Mutex.cpp543.00 B23-11-13 09:17
Mutex.hpp284.00 B23-11-13 09:17
Mutex.o5.23 kB23-09-14 11:03
Sem.cpp1.01 kB23-11-13 09:17
Sem.hpp504.00 B23-11-13 09:17
Sem.o4.15 kB23-09-14 11:03
ShareBuff.cpp621.00 B23-11-13 09:17
ShareBuff.hpp301.00 B23-11-13 09:17
ShareBuff.o12.41 kB23-09-14 11:03
ShareMemory.cpp1.31 kB23-11-13 09:17
ShareMemory.hpp706.00 B28-03-14 21:41
ShareMemory.hpp~705.00 B28-03-14 21:25
ShareMemory.o6.66 kB23-09-14 11:03
ShmIPS.cpp797.00 B23-11-13 09:17
ShmIPS.hpp771.00 B23-11-13 09:17
ShmIPS.o9.46 kB23-09-14 11:03
Socket.cpp252.00 B23-11-13 09:17
Socket.hpp304.00 B23-11-13 09:17
Socket.o1.02 kB23-09-14 11:03
SocketOps.cpp2.25 kB23-11-13 09:17
SocketOps.hpp566.00 B23-11-13 09:17
SocketOps.o4.96 kB23-09-14 11:03
SocketStream.cpp2.03 kB23-11-13 09:17
SocketStream.hpp685.00 B23-11-13 09:17
SocketStream.o5.71 kB23-09-14 11:03
SsbSipH264Encode.c9.73 kB23-11-13 09:17
SsbSipH264Encode.h1.85 kB23-11-13 09:17
SsbSipH264Encode.o2.63 kB23-09-14 11:03
struct.hpp1.44 kB23-09-14 10:46
TcpClient.cpp1.22 kB23-11-13 09:17
TcpClient.hpp588.00 B23-11-13 09:17
TcpClient.o31.48 kB23-09-14 11:03
Thread.cpp1.56 kB23-11-13 09:17
Thread.hpp1.07 kB23-11-13 09:17
Thread.o5.08 kB23-09-14 11:03
ThreadAttr.cpp1.71 kB23-11-13 09:17
ThreadAttr.hpp651.00 B23-11-13 09:17
Transform.cpp1.26 kB23-11-13 09:17
Transform.hpp547.00 B23-11-13 09:17
Transform.o7.17 kB23-09-14 11:03
Uncopyable.hpp238.00 B28-03-14 21:30
Uncopyable.hpp~239.00 B28-03-14 21:30
Video.cpp1.05 kB23-11-13 09:17
Video.hpp536.00 B23-11-13 09:17
Video.o13.39 kB23-09-14 11:03
<Client2013-12-15>0.00 B04-11-14 22:07
...
Sponsored links

capture_init.cpp~ (337.95 kB)

Need 2 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D