?? form1.ui.h
字號:
/****************************************************************************** ui.h extension file, included from the uic-generated form implementation.**** If you wish to add, delete or rename functions or slots use** Qt Designer which will update this file, preserving your code. Create an** init() function in place of a constructor, and a destroy() function in** place of a destructor.*****************************************************************************/#include <qpushbutton.h>#include <qdatetime.h>//#include <qlcdnumber.h>#include <qimage.h>#include <qcolor.h>#include <qstring.h>#include <qpainter.h>#include <qrect.h>#include <qpoint.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <linux/videodev.h>#include <sys/types.h>#include <linux/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <qmessagebox.h>#include <math.h>#include <qradiobutton.h>#include <qslider.h>//#include <iostream.h>#include <qlineedit.h>#include <stdlib.h>//#include <qobject.h>#define BPP 24#define USB_VIDEO "/dev/v4l/video0"#define IMAGE_WIDTH 320#define IMAGE_HEIGHT 240int timedatetimer, monitorstarttimer;// findbackgroundmanytimestimer;int mfd;unsigned char imagebuf[320 * 240 * BPP];//uchar imagebuf[320 * 240 * BPP];bool buttonpress = FALSE;bool onlystart = FALSE;bool Isinitvedio = FALSE;int findbackgroudtimer1,findbackgroudtimer2;bool findbgsuccess = FALSE;QImage imagebg;///////////////////////////////////fuction area start////////////////////////////////int min(int r, int g, int b){ //if((r == g) && (r == b)) {return -1;} if((r <= g) && (r <= b)) {return r;} else if((g <= r) && (g <= b)) {return g;} else return b;};QImage calH(QImage img){ int x = 0, y = 0; int r, g, b; int h; QRgb rgb; //QImage imagetemp = img.copy(); // QImage imgend = img; uint *p; for(y = 0; y < img.height(); y++) { for(x = 0; x < img.width(); x++) { rgb = img.pixel(x,y); r = qRed(rgb); g = qGreen(rgb); b = qBlue(rgb); if ((r == g) && (r == b)) { h = 170;// 2/3 max(255) //p = (uint *)imgend.scanLine(y) + x; //*p = qRgb(h,h,h); } else if(min(r,g,b) == r) { h = 85*(g+2*b-3*r)/(g+b-2*r); } else if(min(r,g,b) == g) { h = 85*(3*r+2*b-5*g)/(r+b-2*g); } else {h = 85*(r+2*g-3*b)/(r+g-2*b);} p = (uint *)img.scanLine(y) + x; *p = qRgb(h,h,h); } } return img;};// open the vedio device -- vedio0 int open_vedio_device(){ int open_fd; open_fd = open(USB_VIDEO, O_RDONLY); printf("open_fd = %d\n", open_fd); if (open_fd<0) { perror(USB_VIDEO); exit(1); } return(open_fd);}// vedio device initialize int initdev(int *file_fd){ //unsigned char *src; //FILE * fp; struct video_capability cap; struct video_picture vpic; struct video_window win; //unsigned int src_depth; int fd = *file_fd; if (ioctl(fd, VIDIOCGCAP, &cap) < 0) { perror("VIDIOGCAP"); fprintf(stderr, "("USB_VIDEO" not a video4linux device?)\n"); close(fd); exit(1); } ioctl(fd, VIDIOCGWIN, &win); win.width = 320; win.height = 240; if (ioctl(fd, VIDIOCSWIN, &win) < 0) { perror("VIDIOCGWIN"); close(fd); exit(1); } if (ioctl(fd, VIDIOCGPICT, &vpic) < 0) { perror("VIDIOCGPICT"); close(fd); exit(1); } if (cap.type & VID_TYPE_MONOCHROME) { vpic.depth=8; vpic.palette=VIDEO_PALETTE_GREY; // 8bit grey if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) { vpic.depth=6; if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) { vpic.depth=4; if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) { fprintf(stderr, "Unable to find a supported capture format.\n"); close(fd); exit(1); } } } } else { vpic.depth=24; vpic.palette=VIDEO_PALETTE_RGB24; if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) { vpic.palette=VIDEO_PALETTE_RGB565; vpic.depth=16; if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) { vpic.palette=VIDEO_PALETTE_RGB555; vpic.depth=15; if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) { fprintf(stderr, "Unable to find a supported capture format.\n"); return -1; } } } } vpic.brightness =1698; vpic.hue = 32768; vpic.colour = 32512; vpic.contrast = 32768; vpic.whiteness = 0; vpic.depth =180; vpic.palette = VIDEO_PALETTE_RGB24; if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) { perror("VIDIOCSPICT"); close(fd); exit(1); } return 1;}void VideoInitalize(){ mfd = open_vedio_device(); initdev(&mfd);}QImage getImage(unsigned char *buf){ int x, y; int i=0; QImage img; uint *p; int r, g, b; if(img.create(320, 240, 32, 0, QImage::IgnoreEndian)) { for(y=0; y<240; y++){ for(x=0; x<320; x++){ r=(int)buf[i+2]; g=(int)buf[i+1]; b=(int)buf; p = (uint *)img.scanLine(y)+ x; *p = qRgb(r,g,b); i+=3; } } } return img;}QImage getImagedis(unsigned char *buf){ int x, y; int i=0; QImage img; uint *p; int r, g, b; if(img.create(320, 240, 32, 0, QImage::IgnoreEndian)) { for(y=0; y<240; y++){ for(x=0; x<320; x++){ r=(int)buf[i+2]; g=(int)buf[i+1]; b=(int)buf[i]; p = (uint *)img.scanLine(y)+ x; *p = qRgb(r,g,b); i+=3; } } } return img;}int different(QImage imgA, QImage imgB, int levelpoint){ int x,y; int difcount = 0; QRgb rgbA,rgbB; // int rA,gA,bA,rB,gB,bB; //int level = slider1->value(); // switch(level) // { //case 0:// level = 15;// break; // case 1:// level = 10;// break; // case 2:// level = 5;// break; // } for(y=0;y<imgA.height();y++) { for(x=0;x<imgA.width();x++) { rgbA = imgA.pixel(x,y); rgbB = imgB.pixel(x,y); // rA = qRed(rgbA); // gA = qGreen(rgbA); // bA = qBlue(rgbA); // rB = qRed(rgbB); // gB = qGreen(rgbB); // bB = qBlue(rgbB); if(!((abs(qRed(rgbA) - qRed(rgbB)) <= levelpoint) && (abs(qGreen(rgbA) - qGreen(rgbB)) <= levelpoint) && (abs(qBlue(rgbA) - qBlue(rgbB)) <= levelpoint))) difcount++; } } return (difcount * 10000 / 76800);}int differentdisplayperson(QImage imgA, QImage imgB,QImage *imgC, int levelpoint){ int x,y; int difcount = 0; QRgb rgbA,rgbB; // int rA,gA,bA,rB,gB,bB; uint *q; for(y=0;y<240;y++) { for(x=0;x<320;x++) { rgbA = imgA.pixel(x,y); rgbB = imgB.pixel(x,y); // rA = qRed(rgbA); // gA = qGreen(rgbA); // bA = qBlue(rgbA); // rB = qRed(rgbB); // gB = qGreen(rgbB); // bB = qBlue(rgbB); if(!((abs(qRed(rgbA) - qRed(rgbB)) <= levelpoint) && (abs(qGreen(rgbA) - qGreen(rgbB)) <= levelpoint) && (abs(qBlue(rgbA) - qBlue(rgbB)) <= levelpoint))) difcount++; else { q = (uint *)imgC->scanLine(y) + x; *q = qRgb(255,255,255); } } } //if((difcount * 10000 / 76800) > leveldif) //return -1; return (difcount * 10000 / 76800); // else return FALSE;}QImage Form1::FindBackground(){ QImage image[3]; //+++++++++++++++++++++++++++++++ //QString str; //++++++++++++++++++++++++++++++ int i; //int dif01,dif12; for (i = 0; i < 3; i++) { findbackgroudtimer1 = startTimer(500); //if(!image[i].loadFromData(imagebuf,230400)) {cout<<"image "<<i<<"loadFormData() failed!\n";} image[i] = getImage(imagebuf); //++++++++++++++++++++++++++++++++++++++++++ //str.sprintf("/arca/wsy/image%d.jpg",i); //if(! image[i].save(str,"JPEG")) {cout<<"image "<<i<<"saved failed!\n";} //+++++++++++++++++++++++++++++++++++++++++ } for(i = 0; i < 3; i++) { image[i] = calH(image[i]); } //dif01 = different(image[0],image[1],20); //dif12 = different(image[1],image[2],20); if( (different(image[0],image[1],20) < 450) && (different(image[1],image[2],20) < 450) ) { findbgsuccess = TRUE; //+++++++++++++++++++++++++++++++++++++++ //if(!image[3].save("/arca/wsy/bgsuccess.jpg","JPEG")) {cout<<"image background in FindBackground() saved failed!\n";} //++++++++++++++++++++++++++++++++++++++ startTimer(500); return image[2]; } else { findbgsuccess = FALSE; findbackgroudtimer2 = startTimer(2000); } //findbackgroundmanytimestimer = startTimer(120000); }//int monitorstarttimer;bool isStop = TRUE;void Form1::MonitorStart(){ if(isStop) return; QImage imageinput,imageinputtemp; //,imagesave; QPainter p(this); if(onlystart) { imagebg = FindBackground(); //++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // imagebg.save("/arca/wsy/background.jpg","JPEG"); //++++++++++++++++++++++++++++++++++++++++++++++++++++++++ } int level = slider1->value(); int leveltemp = level * 100 + 100;//leveltemp2 = 0, leveltemp3 = 0; int dif; /****************************** switch(level) { case 0: leveltemp = 500; break; case 1: leveltemp = 450; break; case 2: leveltemp = 400; } ****************************/ read(mfd, imagebuf, 230400); imageinputtemp = getImage(imagebuf); imageinput = getImagedis(imagebuf); QImage imagedis = imageinput.copy(); //imageinputtemp = imageinput.copy(); imagedis = imagedis.smoothScale(160,120); //+++++++++++++++++++++++++++++ p.drawImage(QPoint(30,110),imagedis);//+++++++++++++++++++++++++++++ // p.drawImage(QRect(QPoint(30,110),QPoint(190,230)),imageinput); imageinputtemp = calH(imageinputtemp); if(radioButtondis->isChecked()) { if((dif = differentdisplayperson(imagebg,imageinputtemp,&imageinput,20)) > leveltemp) { QTime time = QTime::currentTime(); QString strtime,strdate,str; str.sprintf("/mnt/picmon/"); strtime.sprintf("%2d:%2d:%2d.jpg",time.hour(),time.minute(),time.second()); QDate date = QDate::currentDate(); strdate.sprintf("%2d.%2d.%2d_",date.year(),date.month(),date.day()); str = str + strdate + strtime; imageinput.save(str,"JPEG"); imageinput = imageinput.smoothScale(160,120); p.drawImage(QPoint(250,110), imageinput);//+++++++++++++++++++++++ // p.drawImage(QPoint(430,140), imageinput); lineEdit1->setText(str); if(dif > 6000) {FindBackground();} } monitorstarttimer = startTimer(500); } else { if((dif = different(imagebg,imageinputtemp,20)) > leveltemp) { QTime time = QTime::currentTime(); QString strtime,strdate,str; str.sprintf("/mnt/picmon/"); strtime.sprintf("%2d:%2d:%2d.jpg",time.hour(),time.minute(),time.second()); QDate date = QDate::currentDate(); strdate.sprintf("%2d.%2d.%2d_",date.year(),date.month(),date.day()); str = str + strdate + strtime; imageinput.save(str,"JPEG"); p.drawImage(QPoint(250,110), imagedis);//+++++++++++++++++++++ // p.drawImage(QRect(QPoint(250,110),QPoint(410,230)), imageinput); lineEdit1->setText(str); if(dif > 6000) {FindBackground();} } monitorstarttimer = startTimer(500); } //320*240*24 // if the monitor is first start ,we must find a background image }////////////////////////////////////fuction area end/////////////////////////////////void Form1::init(){ showTime(); // QPixmap pix("images/back.png"); // this->setPaletteBackgroundPixmap(pix);}void Form1::StartorStop(){ // QImage imagebg; buttonpress = !buttonpress; isStop = !isStop; if(buttonpress) { pushButtonStartorStop->setText("&Stop Monitor"); if(!Isinitvedio) { VideoInitalize(); Isinitvedio = !Isinitvedio; } onlystart = !onlystart; MonitorStart(); } else { pushButtonStartorStop->setText("&Start Monitor"); //close(mfd); } }void Form1::showTime(){ QTime time = QTime::currentTime(); QString strtime,strdate,str; strtime.sprintf("%2d:%2d:%2d ",time.hour(),time.minute(),time.second()); QDate date = QDate::currentDate(); strdate.sprintf("%d-%d-%d",date.year(),date.month(),date.day()); str = strtime + strdate; lineEditTime->setText(str); timedatetimer = startTimer(1000);}//long countbuf;void Form1::timerEvent( QTimerEvent *te ){ if(te->timerId() == timedatetimer) showTime(); if(te->timerId() == findbackgroudtimer1) { read(mfd, imagebuf, 230400); //cout<<"the bytes readed from imagebuf[] is "<<countbuf<<endl; } if(te->timerId() == findbackgroudtimer2) FindBackground(); if(te->timerId() == monitorstarttimer) MonitorStart(); // if(te->timerId() == findbackgroundmanytimestimer) // FindBackground();}void Form1::delimages(){ system("rm /mnt/picmon/*.jpg -f");}void Form1::spinboxchange(){ int s; s = slider1->value() + 1; spinBox3->setValue(s);}void Form1::spliderchange(){ int s; s = spinBox3->value() - 1; slider1->setValue(s);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -