#include <stdlib.h>
#include <QApplication>
#include <QWidget>
#include <QTimer>
#include <QPainter>
#include <QMatrix>
#include <QSplashScreen>
#include <QFileDialog>
#include <QPushButton>
#include <QImage>
#include <QPixmap>
#include <QDateTime>
#include <QAudioOutput>
#include "webcam.h"
#include "SplashScreen.h"
#include "mydialog.h"
#define VAL 0
#define VAL1 1
static unsigned char huffman_table[] = {
0xFF,0xC4,0x01,0xA2,0x00,0x00,0x01,0x05,0x01,0x01,0x01,0x01,
0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,
0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x01,0x00,0x03,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
...........................................................,0xF8,0xF9,0xFA};
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(const QWidget *parent,
const char *name
){
setWindowTitle("Project : Qt/E USB Camera Image capture");
resize
( QSize(320,
240).
expandedTo(minimumSizeHint
()) );
//ClearState( State_Polished );
video_open("/dev/video1")
// QSplashScreen* splash = new SplashScreen(pixmap);
splash.showMessage("Initialize...", 0,"Red");
qApp->processEvents();
sleep(3);
splash.finish();
splash.deleteLater();
video_set_format();
video_reqbufs();
map_the_buffers();
Queue_the_buffers();
video_enable(dev,1);
start_streaming();
changeBKImage();
capt->start(1);
connect( capt, SIGNAL(timeout()), SLOT(slotTimeout()) );
}
WebCam::~WebCam()
{
qWarning("USB-WebCamera exit!! \n");
capt->stop();
video_enable(dev,0);
munmap(mem,buf.length);
close(dev);
}
void WebCam::start_streaming()
{
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)
{
printf("Unable to dequeue buffer (%d).\n", errno);
close(dev);
}
// Save the image.
sprintf(filename, "/root/quickcam-%03u.jpg", VAL1);
file = fopen(filename, "wb");
if (file != NULL)
{
unsigned char *input;
int input_size;
unsigned char *output;
// int output_data;
input = (unsigned char*)mem;
input_size=buf.bytesused;
int has_dht = 0;
output = 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)
{
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(dev);
}
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(dev);
}
}
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(dev);
}
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(dev);
}
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(dev);
return ret;
}
if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
printf("Error opening device %s: video capture not supported.\n",
devname);
close(dev);
return -EINVAL;
}
printf("Device %s opened: %s.\n", devname, cap.card);
return dev;
}
void WebCam::slotTimeout()
{
start_streaming();
changepmBKImage();
repaint(false);
}
{
drawImage();
painter.drawPixmap(0, 0, *pmPaint);
}
void WebCam::changepmBKImage()
{
strFilename.sprintf("/root/quickcam-%03d.jpg", VAL1);
if (!pmBk.load(strFilename)) {
qDebug("QPixmap load error\n");
}
}
void WebCam::drawImage()
{
matrix.scale(-1.0,1.0);
pmBk=pmBk.xForm(matrix);
pmPaint->resize(width(), height());
painter.begin(pmPaint, this);
painter.
drawPixmap(QPoint(0,
0),pmBk
);
painter.end();
}
#include <stdlib.h>
#include <QApplication>
#include <QWidget>
#include <QTimer>
#include <QPainter>
#include <QMatrix>
#include <QSplashScreen>
#include <QFileDialog>
#include <QPushButton>
#include <QImage>
#include <QPixmap>
#include <QDateTime>
#include <QAudioOutput>
#include "webcam.h"
#include "SplashScreen.h"
#include "mydialog.h"
#define VAL 0
#define VAL1 1
static unsigned char huffman_table[] = {
0xFF,0xC4,0x01,0xA2,0x00,0x00,0x01,0x05,0x01,0x01,0x01,0x01,
0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,
0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x01,0x00,0x03,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
...........................................................,0xF8,0xF9,0xFA};
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(const QWidget *parent, const char *name)
: QWidget( parent, name )
{
setWindowTitle("Project : Qt/E USB Camera Image capture");
resize( QSize(320, 240).expandedTo(minimumSizeHint()) );
//ClearState( State_Polished );
video_open("/dev/video1")
QPixmap pixmap("logo.png");
// QSplashScreen* splash = new SplashScreen(pixmap);
QSplashScreen splash(pixmap);
splash.showMessage("Initialize...", 0,"Red");
qApp->processEvents();
sleep(3);
splash.finish();
splash.deleteLater();
video_set_format();
video_reqbufs();
map_the_buffers();
Queue_the_buffers();
video_enable(dev,1);
start_streaming();
changeBKImage();
pmPaint = new QPixmap(320, 240);
capt = new QTimer( this );
capt->start(1);
connect( capt, SIGNAL(timeout()), SLOT(slotTimeout()) );
}
WebCam::~WebCam()
{
qWarning("USB-WebCamera exit!! \n");
capt->stop();
video_enable(dev,0);
munmap(mem,buf.length);
close(dev);
}
void WebCam::start_streaming()
{
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)
{
printf("Unable to dequeue buffer (%d).\n", errno);
close(dev);
}
// Save the image.
sprintf(filename, "/root/quickcam-%03u.jpg", VAL1);
file = fopen(filename, "wb");
if (file != NULL)
{
unsigned char *input;
int input_size;
unsigned char *output;
// int output_data;
input = (unsigned char*)mem;
input_size=buf.bytesused;
int has_dht = 0;
output = 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)
{
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(dev);
}
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(dev);
}
}
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(dev);
}
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(dev);
}
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(dev);
return ret;
}
if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
printf("Error opening device %s: video capture not supported.\n",
devname);
close(dev);
return -EINVAL;
}
printf("Device %s opened: %s.\n", devname, cap.card);
return dev;
}
void WebCam::slotTimeout()
{
start_streaming();
changepmBKImage();
repaint(false);
}
void WebCam::paintEvent( QPaintEvent * )
{
QPainter painter(this);
drawImage();
painter.drawPixmap(0, 0, *pmPaint);
}
void WebCam::changepmBKImage()
{
QString strFilename;
strFilename.sprintf("/root/quickcam-%03d.jpg", VAL1);
if (!pmBk.load(strFilename)) {
qDebug("QPixmap load error\n");
}
}
void WebCam::drawImage()
{
QMatrix matrix;
QPainter painter;
QString strTime;
matrix.scale(-1.0,1.0);
pmBk=pmBk.xForm(matrix);
pmPaint->resize(width(), height());
painter.begin(pmPaint, this);
painter.drawPixmap(QPoint(0, 0),pmBk);
painter.end();
}
To copy to clipboard, switch view to plain text mode
Bookmarks