开发者社区> 问答> 正文

linux下的C程序内存泄露问题:报错

/////////////////////////////////////////////////////////////////////////////////////////
//MainDlg.cpp
//Author: EmbedVision
/////////////////////////////////////////////////////////////////////////////////////////
#include "MainDlg.h"


#include <QDebug>
#include <stdlib.h>    //system函数头文件
#include <math.h>
#define max_feature_num 100




CMainDlg::CMainDlg(QWidget* parent) : QDialog(parent)
{
//设置对话框的标题、位置与大小
this->setWindowTitle(QString::fromUtf8("高效率的绳轮升降器滑轮检测软件"));
this->setGeometry(0, 40, 800, 440);
// startInit();
    myCom = NULL;


//创建按钮对象
m_startBtn = new QPushButton(QString::fromUtf8("开始采集"), this);
m_stopBtn = new QPushButton(QString::fromUtf8("停止采集"), this);
m_snapBtn = new QPushButton(QString::fromUtf8("抓拍模板"), this);
m_deleteBtn = new QPushButton(QString::fromUtf8("删除模板"), this);
m_matchBtn = new QPushButton(QString::fromUtf8("匹配模式"), this);
m_cejuBtn = new QPushButton(QString::fromUtf8("测距模式"), this);
m_cancelBtn = new QPushButton(QString::fromUtf8("退出程序"), this);

//设置按钮对象的位置
m_startBtn->setGeometry(35, 320, 100, 40);
m_stopBtn->setGeometry(140, 320, 100, 40);
m_snapBtn->setGeometry(245, 320, 100, 40);
m_deleteBtn->setGeometry(350, 320, 100, 40);
m_matchBtn->setGeometry(455, 320, 100, 40);
m_cejuBtn->setGeometry(560, 320, 100, 40);
m_cancelBtn->setGeometry(665, 320, 100, 40);

//设置按钮对象的样式及状态
m_startBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
m_stopBtn->setEnabled(false);
m_stopBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_snapBtn->setEnabled(false);
m_snapBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_deleteBtn->setEnabled(false);
m_deleteBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_matchBtn->setEnabled(false);
m_matchBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_cejuBtn->setEnabled(false);
m_cejuBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
    m_cancelBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");


//关联信号与槽
connect(m_startBtn, SIGNAL(clicked()), this, SLOT(startBtn()));
connect(m_stopBtn, SIGNAL(clicked()), this, SLOT(stopBtn()));
connect(m_snapBtn, SIGNAL(clicked()), this, SLOT(snapBtn()));
connect(m_deleteBtn, SIGNAL(clicked()), this, SLOT(deleteBtn()));
connect(m_matchBtn, SIGNAL(clicked()), this, SLOT(matchBtn()));
connect(m_cejuBtn, SIGNAL(clicked()), this, SLOT(cejuBtn()));
connect(m_cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));


//设置图像的大小
m_image = QImage(IMAGE_WIDTH, IMAGE_HEIGHT, QImage::Format_RGB888);


//创建定时器
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimer()));

m_img_8u1_gray = new unsigned char[IMAGE_WIDTHIMAGE_HEIGHT];
m_img_8u1_merge = new unsigned char[IMAGE_WIDTH
IMAGE_HEIGHT];

m_srcImg = cvCreateImage(cvSize(IMAGE_WIDTH, IMAGE_HEIGHT), IPL_DEPTH_8U,1);
m_gaussionImg = cvCreateImage(cvSize(IMAGE_WIDTH, IMAGE_HEIGHT), IPL_DEPTH_8U,1);
m_cannyEdgeImg = cvCreateImage(cvSize(IMAGE_WIDTH, IMAGE_HEIGHT), IPL_DEPTH_8U,1);
m_cannyEdgeImg1 = cvCreateImage(cvSize(IMAGE_WIDTH, IMAGE_HEIGHT), IPL_DEPTH_8U,1);
edge = cvCreateImage(cvSize(IMAGE_WIDTH, IMAGE_HEIGHT), IPL_DEPTH_8U,1);
edge1 = cvCreateImage(cvSize(IMAGE_WIDTH, IMAGE_HEIGHT), IPL_DEPTH_8U,1);
img = cvCreateImage(cvSize(IMAGE_WIDTH, IMAGE_HEIGHT), IPL_DEPTH_8U,1);
temp = cvCreateImage(cvSize(IMAGE_WIDTH, IMAGE_HEIGHT), IPL_DEPTH_8U,1);
img1 = cvCreateImage(cvSize(IMAGE_WIDTH, IMAGE_HEIGHT), IPL_DEPTH_8U,1);
temp1 = cvCreateImage(cvSize(IMAGE_WIDTH, IMAGE_HEIGHT), IPL_DEPTH_8U,1);


m_eig_image =cvCreateImage(cvSize(IMAGE_WIDTH, IMAGE_HEIGHT),IPL_DEPTH_32F ,1);
m_temp_image=cvCreateImage(cvSize(IMAGE_WIDTH, IMAGE_HEIGHT),IPL_DEPTH_32F ,1);
m_camera.snap_mode=0;

//初始化摄像头参数
memset(&m_camera, 0, sizeof(V4L2_data));
m_camera.dev_name = (char*)("/dev/video2");
b = 0;

}


CMainDlg::~CMainDlg()
{
delete []m_img_8u1_gray;
m_img_8u1_gray = NULL;
delete []m_img_8u1_merge;
m_img_8u1_merge = NULL;

cvReleaseImage(&m_srcImg);
cvReleaseImage(&m_gaussionImg);
cvReleaseImage(&img);
cvReleaseImage(&m_cannyEdgeImg);
cvReleaseImage(&m_cannyEdgeImg1);
cvReleaseImage(&edge);
cvReleaseImage(&edge1);
cvReleaseImage(&img1);
cvReleaseImage(&temp);
cvReleaseImage(&temp1);

cvReleaseImage(&m_eig_image);
cvReleaseImage(&m_temp_image);

if(myCom != NULL){
        if(myCom->isOpen()){
            myCom->close();
        }
        delete myCom;
    }

}


void CMainDlg::paintEvent(QPaintEvent event)
{
    float u=0.15187333;          //放大倍数
//显示图像及叠加的信息
QPainter painter(this);
m_pixmap = QPixmap::fromImage(m_image.scaled(400, 300,  Qt::KeepAspectRatio));
painter.drawPixmap(0, 0, m_pixmap);



if(m_camera.snap_mode == 0)
{
QPainter painter4(this);
painter4.setPen(QPen(QColor(255,0,0), 12, Qt::SolidLine, Qt::RoundCap));
painter4.setFont(QFont("wenquanyi", 16));
painter4.drawText(590, 155, QString::fromUtf8("请抓拍模板"));
}
//显示抓拍的模板图像
if(m_camera.snap_mode == 1)
{
QPainter painter1(this);
img_8u3_rgb888.load("../need2_gray.bmp");
n_pixmap = QPixmap::fromImage(img_8u3_rgb888.scaled(400, 300,  Qt::KeepAspectRatio));
painter1.drawPixmap(400, 0, n_pixmap);
QPainter painter2(this);
painter2.setPen(QPen(QColor(255,0,0), 12, Qt::SolidLine, Qt::RoundCap));
painter2.setFont(QFont("wenquanyi", 10));

painter2.drawText(410, 287, QString::fromUtf8("模板图像"));

edge=QImageToIplImage(&img_8u3_rgb888);

cvThresholdOtsu(edge, edge1);


CvSeq
contours2=NULL;
CvMemStorage*    mem2 = cvCreateMemStorage(0);
count=cvFindContours(m_cannyEdgeImg1,mem2,&contours2,sizeof(CvContour),CV_RETR_EXTERNAL);

int aa[1024];
int j=0;  
    for (CvSeq* c = contours2; c != NULL; c = c->h_next) 
{  
// if(j<=20) 
// {
// QString tlength = QString::number(j, 10);
// QMessageBox* box1 = new QMessageBox;
    // QTimer::singleShot(200, box1, SLOT(close()));
// box1->setWindowTitle(tr("返回结果"));
    // box1->setIcon(QMessageBox::Warning);
    // box1->setText(tlength);
// box1->show();
// } 
         CvRect rc =cvBoundingRect(c,0);  
if(rc.width<108||rc.height<108)
{
continue;
}
 rc.x=rc.x-2;
  rc.y=rc.y-2;
  rc.width=rc.width+4;
  rc.height=rc.height+4;//重新稍微扩大矩形框
       
QPainter painter7(this);
painter7.setPen(QPen(QColor(255,0,0), 1, Qt::SolidLine, Qt::RoundCap));
     painter7.drawRect(cvRound(rc.x300/480),cvRound(rc.y300/480),cvRound(rc.width300/480),cvRound(rc.height300/480));
aa[j]=rc.x;
    IplImage* imgNo = cvCreateImage(cvSize(rc.width, rc.height), IPL_DEPTH_8U, 1);   
    cvSetImageROI(m_cannyEdgeImg1, rc);  
    cvCopyImage(m_cannyEdgeImg1, imgNo); 
CvSeq*    contours1=NULL;
CvSeq*    contours3=NULL;
CvMemStorage*  mem1 = cvCreateMemStorage();
CvMemStorage*   mem3= cvCreateMemStorage();
cvFindContours(edge1,mem1,&contours1,sizeof(CvContour),CV_RETR_EXTERNAL);
cvFindContours(imgNo,mem3,&contours3,sizeof(CvContour),CV_RETR_EXTERNAL);
bb[j]= cvMatchShapes(contours1,contours3,3);
j++;
    cvResetImageROI(m_cannyEdgeImg1);  
     cvReleaseImage(&imgNo);
cvReleaseMemStorage(&mem1);
cvReleaseMemStorage(&mem3);
   }
   cvReleaseMemStorage(&mem2);
              
    
     
 for(int k=1;k<j;k++)
          for(int m=0;m<j-k;m++)
          {
              if (aa[m]>aa[m+1])
              {
 int t;
 double x;
              t=aa[m];
     x=bb[m];
              aa[m]=aa[m+1];
 bb[m]=bb[m+1];
              aa[m+1]=t;
 bb[m+1]=x;
              }
          }
if(j!=0)
{
 char text[1024];
 sprintf(text, "轮廓数%d", j);
 QPainter painter3(this);
 painter3.setPen(QPen(QColor(255,0,0), 12, Qt::SolidLine, Qt::RoundCap));
 painter3.setFont(QFont("wenquanyi", 12));
 painter3.drawText(10, 400, QString::fromUtf8(text));
}
if(bb[0]!=0)
 {
 double zhi=bb[0];
 char text1[1024];
 sprintf(text1, "匹配值%.9f", zhi);
 QPainter painter8(this);
 painter8.setPen(QPen(QColor(255,0,0), 12, Qt::SolidLine, Qt::RoundCap));
 painter8.setFont(QFont("wenquanyi", 12));
 painter8.drawText(100, 400, QString::fromUtf8(text1));
 }
 if(bb[1]!=0)
 {
 double zhi1=bb[1];
 char text2[1024];
 sprintf(text2, "匹配值%.9f", zhi1);
 QPainter painter9(this);
 painter9.setPen(QPen(QColor(255,0,0), 12, Qt::SolidLine, Qt::RoundCap));
 painter9.setFont(QFont("wenquanyi", 12));
 painter9.drawText(310, 400, QString::fromUtf8(text2));
 }
//QString text  = QString("%1  %2  %3").arg(count).arg(zhi, 0,'g',8).arg(zhi1, 0,'g',8);
//QMessageBox* box1 = new QMessageBox;
   // QTimer::singleShot(1000, box1, SLOT(close()));
   // box1->setWindowTitle(tr("警告"));
   // box1->setIcon(QMessageBox::Warning);
   // box1->setText(text);
   // box1->show();

/char text[1024];
sprintf(text, "匹配值%.9f", match);
QPainter painter3(this);
painter3.setPen(QPen(QColor(255,0,0), 12, Qt::SolidLine, Qt::RoundCap));
painter3.setFont(QFont("wenquanyi", 16));
painter3.drawText(10, 400, QString::fromUtf8(text));
storage=cvCreateMemStorage(0);
results=cvHoughCircles(m_cannyEdgeImg1,storage,CV_HOUGH_GRADIENT,2,m_cannyEdgeImg1->width/7,the,60,102,120);//测外径
for( i=0;i<results->total ;i++)
     {
           float
p=(float*) cvGetSeqElem(results,i);
           CvPoint pt=cvPoint(cvRound(p[0]300/480),cvRound(p[1]300/480));//圆心坐标
           int a=cvRound(p[0]300/480)-cvRound(p[2]300/480);
  int b=cvRound(p[1]300/480)-cvRound(p[2]300/480);
  int c=2
cvRound(p[2]300/480);
  QPainter painter5(this);
  painter5.setPen(QPen(QColor(255,0,0), 1, Qt::SolidLine, Qt::RoundCap));
  painter5.drawEllipse(a,b,c,c);
           //cvCircle(image,pt,cvRound(p[2]),CV_RGB(0xff,0xff,0xff));//半径长度
     }
if(results!=NULL)
{
float
m=(float
) cvGetSeqElem(results,0);
char text_buf0[1024];
if(m!=NULL)
{
   sprintf(text_buf0, "R0:%.9f mm", cvRound(m[2])u);
r0=cvRound(m[2])u;
    }
QPainter painter6(this);
painter6.setPen(QPen(QColor(255,0,0), 6, Qt::SolidLine, Qt::RoundCap));
painter6.setFont(QFont("wenquanyi", 8));
painter6.drawText(400, 50, QString::fromUtf8(text_buf0));
}
/
}
QWidget::paintEvent(event);
}

void CMainDlg::moveEvent(QMoveEvent
event)
{
//保持窗口在触摸屏中的位置不变
this->setGeometry(0, 40, 800, 440);


QDialog::moveEvent(event);
}


void CMainDlg::startBtn()
{
//初始化摄像头
int error_code = init_dev(&m_camera);
  if(error_code < 0 )
{
//显示错误信息
char error_buf[1024];
sprintf(error_buf, "open camera failed. error code:%d", error_code);
QMessageBox::information(this, "cann't open camera!", QString(error_buf));
return;
}


//开启定时器,启动采集
m_timer->start(80);

on_actionOpen_triggered();//打开串口



//重新设置按钮对象的样式及状态
m_startBtn->setEnabled(false);
m_startBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_stopBtn->setEnabled(true);
m_stopBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
m_snapBtn->setEnabled(true);
m_snapBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
m_deleteBtn->setEnabled(false);
m_deleteBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_matchBtn->setEnabled(false);
m_matchBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_cejuBtn->setEnabled(false);
m_cejuBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_cancelBtn->setEnabled(false);
m_cancelBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
}


void CMainDlg::stopBtn()
{
//关闭定时器,停止采集
m_timer->stop();


//停止摄像头
stop_dev(&m_camera);

    on_actionClose_triggered();//关闭串口  
//重新设置按钮对象的样式及状态
m_startBtn->setEnabled(true);
m_startBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
m_stopBtn->setEnabled(false);
m_stopBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_snapBtn->setEnabled(false);
m_snapBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_deleteBtn->setEnabled(false);
m_deleteBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_matchBtn->setEnabled(false);
m_matchBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_cejuBtn->setEnabled(false);
m_cejuBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_cancelBtn->setEnabled(true);
m_cancelBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
    
}


void CMainDlg::snapBtn()
{
char file_name[1024];
sprintf(file_name, "need2_gray.bmp");
qsBmpGraySave((unsigned char
)(m_cannyEdgeImg1->imageData), IMAGE_WIDTH, IMAGE_HEIGHT, file_name, 1);
m_snapBtn->setEnabled(false);
m_snapBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");

m_stopBtn->setEnabled(false);
m_stopBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_deleteBtn->setEnabled(true);
m_deleteBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
m_matchBtn->setEnabled(true);
m_matchBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
m_cejuBtn->setEnabled(true);
m_cejuBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
startInit();//打开定时器开始发数据

}


void CMainDlg::deleteBtn()
{

system("rm -rf need2_gray.bmp");   //删除抓拍到的模板图像
on_timeClose_triggered();//关闭定时器停止发数据
m_camera.snap_mode = 0;

m_snapBtn->setEnabled(true);
m_snapBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
m_deleteBtn->setEnabled(false);
m_deleteBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_stopBtn->setEnabled(true);
m_stopBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(55,100,255); background-color:rgb(155,200,33); border-radius:8px}");
m_matchBtn->setEnabled(false);
m_matchBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");
m_cejuBtn->setEnabled(false);
m_cejuBtn->setStyleSheet("QPushButton{font-size:24px; color:rgb(50,50,50); background-color:rgb(155,155,155); border-radius:8px}");

}
void CMainDlg::matchBtn()
{
m_camera.snap_mode = 1;


}
void CMainDlg::cejuBtn()
{
m_camera.snap_mode = 2;


}
void CMainDlg::onTimer()
{
//统计算法耗时
struct  timeval tpstart, tpend;
float timeuse;
gettimeofday(&tpstart, NULL);

//获取当前帧图像
if(1 != read_frame(&m_camera, NULL, m_img_8u1_gray) )
return;

memcpy(m_srcImg->imageData, m_img_8u1_gray, IMAGE_WIDTH
IMAGE_HEIGHT);//图像拷贝

//高斯平滑
cvSmooth(
m_srcImg,       //输入图像
m_gaussionImg,  //输出图像
CV_GAUSSIAN,    //高斯卷积
7, 7,           //卷积参数7x7
1.0, 1.0);      //参数标准差
//自适应阈值分割 自定义函数
the=cvThresholdOtsu(m_srcImg, m_cannyEdgeImg);

    //开运算
    cvMorphologyEx(m_cannyEdgeImg,img,temp,NULL, CV_MOP_OPEN,1);
//闭运算
cvMorphologyEx(img,img1,temp1,NULL,CV_MOP_CLOSE,1);

//Canny边缘检测
cvCanny(
img1,
m_cannyEdgeImg1,
50, the);

//得到组合的显示图像
/qsMerge4Image(
m_img_8u1_gray, 
(unsigned char
)(m_gaussionImg->imageData),  
(unsigned char
)(img1->imageData), 
(unsigned char
)(m_cannyEdgeImg1->imageData), 
IMAGE_WIDTH, IMAGE_HEIGHT, 
m_img_8u1_merge);/
qsGray2Rgb((unsigned char
)(m_cannyEdgeImg1->imageData), IMAGE_WIDTH, IMAGE_HEIGHT, m_image.bits());




gettimeofday(&tpend, NULL);
timeuse = 1000*(tpend.tv_sec - tpstart.tv_sec) + (tpend.tv_usec - tpstart.tv_usec)/1000.0;    //单位ms


//显示图像帧号及耗时信息
char text_buf[1024];
sprintf(text_buf, "第%d帧; 所有处理共耗时%.2f ms", m_camera.frame_index, timeuse);
QPainter painter(&m_image);
painter.setPen(QPen(QColor(255,0,0), 12, Qt::SolidLine, Qt::RoundCap));
painter.setFont(QFont("wenquanyi", 16));
painter.drawText(10, 40, QString::fromUtf8(text_buf));
painter.drawText(10, 460, QString::fromUtf8("实时处理图像"));





//通知paintEvent绘制图像
update();
}
int CMainDlg::cvThresholdOtsu(IplImage* src, IplImage* dst)
{
int height=src->height;
int width=src->width;

//histogram计算直方图并归一化
float histogram[256]={0};
for(int i=0;i<height;i++) {
unsigned char* p=(unsigned char*)src->imageData+src->widthStepi;
for(int j=0;j<width;j++) {
histogram[p++]++;
}
}
//normalize histogram
int size=height
width;
for(int i=0;i<256;i++) {
histogram[i]=histogram[i]/size;
}

//average pixel value计算图像灰度均值
float avgValue=0;
for(int i=0;i<256;i++) {
avgValue+=i
histogram[i];
}


int threshold;
float maxVariance=0;
float w=0,u=0;
for(int i=0;i<256;i++) {
w+=histogram[i];
u+=ihistogram[i];


float t=avgValue
w-u;
float variance=tt/(w(1-w));
if(variance>maxVariance) {
maxVariance=variance;
threshold=i;
}
}


cvThreshold(src,dst,threshold,255,CV_THRESH_BINARY);
return threshold;
}


IplImage CMainDlg::QImageToIplImage(const QImage * qImage)
{
    int width = qImage->width();
    int height = qImage->height();
    CvSize Size;
    Size.height = height;
    Size.width = width;
    IplImage IplImageBuffer = cvCreateImage(Size, IPL_DEPTH_8U, 1);
    for (int y = 0; y < height; ++y)
    {
        for (int x = 0; x < width; ++x)
        {
            QRgb rgb = qImage->pixel(x, y);
            cvSet2D(IplImageBuffer, y, x, CV_RGB(qRed(rgb), qGreen(rgb), qBlue(rgb)));
        }
    }
    return IplImageBuffer;
}
void CMainDlg::startInit()
{
    //初始化连续发送计时器计时间隔
    obotimerdly = OBO_TIMER_INTERVAL;


    //设置连续发送计时器
    obotimer = new QTimer(this);
obotimer->start(obotimerdly);
    connect(obotimer, SIGNAL(timeout()), this, SLOT(sendMsg()));



}
//打开串口
void CMainDlg::on_actionOpen_triggered()
{
    QString portName = "ttyUSB0";   //获取串口名
#ifdef Q_OS_LINUX
    myCom = new QextSerialPort("/dev/" + portName);
#elif defined (Q_OS_WIN)
    myCom = new QextSerialPort(portName);
#endif
    connect(myCom, SIGNAL(readyRead()), this, SLOT(readMyCom()));


    //设置波特率
    myCom->setBaudRate(BAUD9600);//波特率9600


    //设置数据位
    myCom->setDataBits(DATA_8);//8位数据位


    //设置校验
    myCom->setParity(PAR_NONE);//无奇偶校验
        
    //设置停止位
    myCom->setStopBits(STOP_1);//1位停止位
       
    //设置数据流控制
    myCom->setFlowControl(FLOW_OFF);//无数据流控制
    //设置延时
    myCom->setTimeout(TIME_OUT);


    if(myCom->open(QIODevice::ReadWrite))
{
        QMessageBox::information(this, tr("打开成功"), tr("已成功打开串口") + portName, QMessageBox::Ok);
    }
else
{
        QMessageBox::critical(this, tr("打开失败"), tr("未能打开串口 ") + portName + tr("\n该串口设备不存在或已被占用"), QMessageBox::Ok);
        return;
    }

}
//关闭串口
void CMainDlg::on_actionClose_triggered()
{
    myCom->close();
    delete myCom;
    myCom = NULL;
}
//关闭发送数据定时器
void CMainDlg::on_timeClose_triggered()
{
    obotimer->stop(); 
delete obotimer;
obotimer = NULL;
}
//读取数据
void CMainDlg::readMyCom()
{
    QByteArray temp = myCom->readAll();
    // QString buf;
// const char str1 = "00FF0001";
// const char str2 = "00FF0002";
// char dst = temp.data();
// QString tlength = QString::number(temp.length(), 10);
// QString dlength = QString::number(strlen(dst), 10);
//char txt[1024];
    // if(!temp.isEmpty()){
           // buf = temp;
 // QMessageBox::information(this, tr("读取成功"), tr("读取结果") + buf, QMessageBox::Ok);
 // sprintf(txt, temp);
 // QPainter painter10(this);
//painter10.setPen(QPen(QColor(255,0,0), 12, Qt::SolidLine, Qt::RoundCap));
//painter10.setFont(QFont("wenquanyi", 12));
//painter10.drawText(350, 400, QString::fromUtf8(txt));
    // }
// QMessageBox::information(this, tr("读取成功"), tr("读取结果") + tlength + ' ' + dlength, QMessageBox::Ok);
//QMessageBox::information(this, tr("读取成功"), tr("读取结果") + tempf.at(tempf.length() - 1), QMessageBox::Ok);
char
buffed;
    buffed=temp.data();
int i;
for(i=0;i<8;i++)
    {
        if(buffed[i]=='1')
        {
    m_camera.snap_mode = 1;
         break;//查找到第一个立即退出循环
        }
       else if(buffed[i]=='2')
        {
    m_camera.snap_mode = 2;
         break;//查找到第一个立即退出循环
        }
        else if(buffed[i]=='3')
        {
    m_camera.snap_mode = 3;
         break;//查找到第一个立即退出循环
        }
else if(buffed[i]=='4')
        {
    m_camera.snap_mode = 4;
         break;//查找到第一个立即退出循环
        }
    }
if(i==8)
{
//QMessageBox::information(this, tr("读取成功"), tr("读取结果") + temp, QMessageBox::Ok);
QMessageBox
box = new QMessageBox;
    QTimer::singleShot(500, box, SLOT(close()));
box->setWindowTitle(tr("返回结果"));
    box->setIcon(QMessageBox::Warning);
    box->setText(tr(temp));
box->show();
}
}


//发送数据
void CMainDlg::sendMsg()
{
    QByteArray buf;
QByteArray buf1;
QByteArray buf2;
QString str;
QString str1;
QString str2;
    bool ok;
    str = "05 30 30 46 46 57 52 30 44 30 30 30 30 30 31";//读D0的一点数据
buf=changetype(str);
sleep(30);
    myCom->write(buf);

// if(match!=0&&r0!=0)
// {
// int d=int(match
10000);
// int e=int(r0
100);  
// QString s4,s5;
// s4=jinzhizhuanhuan(d);
// s5=jinzhizhuanhuan(e);
// str2="05 30 30 46 46 57 57 30 44 30 30 30 34 30 32"+s4+s5;
// buf2=changetype(str2);
// sleep(30);
// myCom->write(buf2);
// }
}
//对写入数据进行进制转换
QString CMainDlg::jinzhizhuanhuan(int mn)
{
    char buffer[33];
    char
buffed;
    sprintf(buffer, "%x", mn);


    /*int length = 0;
    char *tmp = buffer;
    while(tmp != '\0') {
        ++length;
        ++tmp;
    }
/
   QString str=buffer;


   if(str.length() < 4)
   {
   for(int i=-1;i<4-str.length();i++)
   {
   str="0" + str;
   }
   }
   QByteArray ba = str.toLatin1();
   buffed=ba.data();


   int  tmp;
   QString pq,lmn,jkl;
   for(int i=0;i<4;i++)
   {
   if((buffed[i]>='0')&&(buffed[i]<='9'))
   {
   tmp = buffed[i]-'0'+30;
   }
   else if((buffed[i]>='A')&&(buffed[i]<='F'))
   {
   tmp = buffed[i]-'A'+41;
   }
   else if((buffed[i]>='a')&&(buffed[i]<='f'))
   {
   tmp = buffed[i]-'a'+61;
   }


   jkl=QString::number(tmp);
   lmn=" "+jkl;
   pq+=lmn;
   }


   return pq;
}


//把字符串转换成可写类型
QByteArray CMainDlg::changetype(QString str)
{
    QByteArray buf;
    char data;

展开
收起
kun坤 2020-06-07 16:27:10 601 0
1 条回答
写回答
取消 提交回答
  • 用valgrind运行看看

    你这么一大段代码,我觉得不大有人会去看,还是自己用工具吧

    ######最关键这是哪门子 "c"啊。。。。哈。######回复 @风城醉 : 你的标题不是说linux下么?我都没留心看你代码,如果是VS下的有其他办法检查,百度一下有很多方法的。比如这个:http://blog.csdn.net/windows_nt/article/details/8652191######我电脑是windows系统,valgrind有windows版本吗?而且我用了第三方库。######好像和Linux没什么关系######这代码也是没谁了,内存泄漏难道 qt检查不出来?断点总会吧######这代码也太长了 、、、 http://www.zuimoban.com/php/
    2020-06-07 16:27:21
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Alibaba Cloud Linux 3 技术揭秘 立即下载
Alibaba Cloud Linux 3 发布 立即下载
ECS系统指南之Linux系统诊断 立即下载