PDA

View Full Version : Video Capture



Harini
6th January 2014, 11:21
Hello Every One :)

I'm using Qt 4.7. I've done the camera project with that I'm able to capture the images but unable to record the video....can any one please help me??


struct v4l2_buffer buf;
unsigned int i,size,buf_size;
char filename[] = "quickcam-0000.jpg";
int dev, ret;
FILE *file;
void *mem;

unsigned char data[65000];




WebCam::WebCam( QWidget *parent ): QWidget( parent )

{

setWindowTitle("Project : Qt/E USB Camera Image capture");

resize( QSize(320, 240).expandedTo(minimumSizeHint()) );

video_open("/dev/video0");

QPixmap pixmap("logo.png");

qDebug("Testing......................!! \n");

QSplashScreen splash(pixmap);
splash.showMessage("Initialize...", 0,"Red");
qApp->processEvents();

sleep(3);

splash.finish(&splash);
splash.deleteLater();

video_set_format();
video_reqbufs();
map_the_buffers();
Queue_the_buffers();
video_enable(dev,1);
start_streaming();
changepmBKImage();
m_ppmPaint = new QPixmap(320, 240);

capt = new QTimer( this );
capt->start(1);
connect( capt, SIGNAL(timeout()), SLOT(slotTimeout()) );
}

WebCam::~WebCam()
{
qDebug("Testing......................1 \n");
qWarning("USB-WebCamera exit!! \n");
capt->stop();
video_enable(dev,0);
munmap(mem,buf.length);
close();
}

void WebCam::start_streaming()
{

DIR* FD;
struct dirent* in_file;
char buffer[40];
char *last_four = &buffer[9];
char num[5];
int number,final=0;

qDebug("Start streaming..................@1 \n");

memset(&buf, 0, sizeof buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
ret = ioctl(dev, VIDIOC_DQBUF, &buf);
if (ret < 0)
{
qDebug("Start streaming..................@2 \n");
printf("Unable to dequeue buffer (%d).\n", errno);
close();
}


if (NULL == (FD = opendir ("/home/stesalit/Desktop/images")))
{
fprintf(stderr, "Error : Failed to open input directory\n");

}
while ((in_file = readdir(FD)))
{

if (!strcmp (in_file->d_name, "."))
continue;
if (!strcmp (in_file->d_name, ".."))
continue;
strcpy(buffer,in_file->d_name);
strcpy(num,last_four);
number = atoi(num);
if (number > final)
final = number;

}

// Save the image.

sprintf(filename, "/home/stesalit/Desktop/images/quickcam-%03u.jpg", final+1);

file = fopen(filename, "wb");
if (file != NULL)
{
unsigned char *input;
int input_size;
unsigned char *output;
//QWidgetData *output;
qDebug("Start streaming..................@4 \n");
input = (unsigned char*)mem;
input_size=buf.bytesused;
int has_dht = 0;
output = (unsigned char*)data;
unsigned char hdr[4];
int o_count=0;
int r_count=0;
int size;
output[o_count++]=0xFF;
output[o_count ++]=0xD8;
r_count += 2;

while(!has_dht)
{
qDebug("Start streaming..................@5 \n");

memcpy(hdr, input+r_count, 4);
r_count += 4;

if(hdr[1]==0xC4) has_dht = 1;
else if(hdr[1]==0xDA) break;

// skip to the next marker
size = (hdr[2]<<8) + hdr[3];
memcpy(output+o_count, hdr, 4);
o_count += 4;

memcpy(output+o_count, input+r_count, size-2);

r_count += (size-2);
o_count += (size-2);
}

if(!has_dht)
{
memcpy(output+o_count, huffman_table, sizeof(huffman_table));
o_count += sizeof(huffman_table);
memcpy(output+o_count, hdr, 4);
o_count += 4;
}
// Process the remaining data in one go
memcpy(output+o_count, input+r_count, input_size-r_count);
o_count += (input_size-r_count);
r_count += (input_size-r_count);

input=NULL;
output=NULL;
fwrite(data,o_count,1,file);
fclose(file);
}

// Requeue the buffer.
ret = ioctl(dev, VIDIOC_QBUF, &buf);
if (ret < 0)
{
printf("Unable to requeue buffer (%d).\n", errno);
close();
}

fflush(stdout);
}


void WebCam::Queue_the_buffers()
{
memset(&buf, 0, sizeof buf);
buf.index = VAL;
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
ret = ioctl(dev, VIDIOC_QBUF, &buf);
if (ret < 0)
{
printf("Unable to queue buffer (%d).\n", errno);
close();
}
}


void WebCam::map_the_buffers()
{
memset(&buf, 0, sizeof buf);
buf.index = VAL;
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
ret = ioctl(dev, VIDIOC_QUERYBUF, &buf);
if (ret < 0)
{
printf("Unable to query buffer %u (%d).\n", VAL, errno);
close();
}

printf("length: %u offset: %u\n", buf.length, buf.m.offset);

mem = mmap(0, buf.length, PROT_READ, MAP_SHARED, dev, buf.m.offset);
if (mem == MAP_FAILED)
{
printf("Unable to map buffer %u (%d)\n", VAL, errno);
close();
}
printf("Buffer %u mapped at address %p\n", VAL, mem);
}

int WebCam::video_set_format()
{
struct v4l2_format fmt;

memset(&fmt, 0, sizeof fmt);
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = 320;
fmt.fmt.pix.height = 240;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
fmt.fmt.pix.field = V4L2_FIELD_ANY;

ret = ioctl(dev, VIDIOC_S_FMT, &fmt);
if (ret < 0) {
printf("Unable to set format: %d.\n", errno);
return ret;
}

printf("Video format set: width: %u height: %u buffer size: %u\n",
fmt.fmt.pix.width, fmt.fmt.pix.height, fmt.fmt.pix.sizeimage);

return 0;
}

int WebCam::video_reqbufs()
{
struct v4l2_requestbuffers rb;

memset(&rb, 0, sizeof rb);
rb.count = 16;
rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
rb.memory = V4L2_MEMORY_MMAP;

ret = ioctl(dev, VIDIOC_REQBUFS, &rb);
if (ret < 0)
{
printf("Unable to allocate buffers: %d.\n", errno);
return ret;
}

printf("%u buffers allocated.\n", rb.count);
return 0;
}

int WebCam::video_enable(int dev, int enable)
{
int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

ret = ioctl(dev, enable ? VIDIOC_STREAMON : VIDIOC_STREAMOFF, &type);
if (ret < 0) {
printf("Unable to %s capture: %d.\n", enable ? "start" : "stop", errno);
return ret;
}

return 0;
}

int WebCam::video_open(const char *devname)
{
struct v4l2_capability cap;

dev = open(devname, O_RDWR);
if (dev < 0) {
printf("Error opening device %s: %d.\n", devname, errno);
exit(1);
}

memset(&cap, 0, sizeof cap);
ret = ioctl(dev, VIDIOC_QUERYCAP, &cap);
if (ret < 0) {
printf("Error opening device %s: unable to query device.\n",devname);
close();
return ret;
}

if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
printf("Error opening device %s: video capture not supported.\n",
devname);
close();
return -EINVAL;
}

printf("Device %s opened: %s.\n", devname, cap.card);
return dev;
}

void WebCam::slotTimeout()
{
start_streaming();
changepmBKImage();
repaint();
}

void WebCam::paintEvent( QPaintEvent * )
{
QPainter painter(this);

drawImage();

painter.drawPixmap(0, 0, *m_ppmPaint);
}

void WebCam::changepmBKImage()
{
QString strFilename;


strFilename.sprintf("/root/quickcam-%03d.jpg", VAL1);

if (!m_pmBk.load(strFilename)) {
qDebug("QPixmap load error\n");
}
}

void WebCam::drawImage()
{
QMatrix matrix;
QPainter painter(this);
QString strTime;

matrix.scale(-1.0,1.0);

resize(width(),height());

painter.begin(m_ppmPaint);

painter.drawPixmap(QPoint(0,0),m_pmBk);

painter.begin(this);
painter.end();


}