QT使用TCP发送相机拍的图片

本文介绍如何使用QT作为客户端,通过TCP协议将OpenCV捕获的图片发送到服务器。客户端代码实现了图像数据的转换和发送,服务器端则负责接收并处理这些图像数据。参考了两篇优快云博客进行实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

客户端:
在这里插入图片描述
代码:

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "ui\MainWindow.h"
#include <QTcpServer>  
#include <QBuffer>
#include <QTcpSocket>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <QThread>

using namespace std;
using namespace cv;

//客户端
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

	connect(ui->btnPhto, SIGNAL(clicked()), this, SLOT(BtnPhtoSlot()));  //拍照
	connect(ui->btnPhto_2, SIGNAL(clicked()), this, SLOT(BtnPhto2Slot()));  //拍照
	connect(ui->btnConnect, SIGNAL(clicked()), this, SLOT(BtnConnectSlot()));  //连接服务端
	connect(ui->btnOff, SIGNAL(clicked()), this, SLOT(BtnOffSlot()));  //断开服务端
	//初始化套接字对象
	mSocketM = new QTcpSocket();

	
}

MainWindow::~MainWindow()
{
    delete ui;
	delete mSocketM;
	//t->closeThread();
	//t->wait();
}

void MainWindow::BtnPhto2Slot()  //发送数据
{
	//QString s = ui->textEdit->toPlainText();
	//QByteArray qByteArray = s.toUtf8();
	//char* cStr = qByteArray.data();
	//int sendRe = mSocketM->write(dataArray);
	qDebug() << "sendPicture clicked" << endl;

	QBuffer buffer;
	buffer.open(QIODevice::ReadWrite);
	tempPixmapM.save(&buffer, "bmp");
	quint32 pix_len = (quint32)buff
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值