⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd_downdlg.cpp

📁 利用网络将一张图片的数据发送到指定IP上
💻 CPP
字号:
// Lcd_downDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Lcd_down.h"
#include "Lcd_downDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


unsigned char * image;
#define width 96*2
#define height 64*2
#define Headsize 54
int  width_old,height_old;
unsigned char header[Headsize];
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

/*******************************************************************************
unsigned char  bmp_read( const char *filename,unsigned char* image) {}
读一张图片的信息,并记录这张图片的宽和高与图片的数据。
*******************************************************************************/

unsigned char  bmp_read( const char *filename,unsigned char* image) 
 {

	FILE *fp;

	if (!(fp = fopen(filename, "rb"))) 
    return -1;
	
	fseek(fp,0,SEEK_SET);
	fread(header,sizeof(unsigned char),Headsize,fp);
	fread(image, sizeof(unsigned char), 192*128*2, fp);

	//获得文件宽度
	width_old=header[18];
	width_old+=(header[19]<<8);
	width_old+=(header[20]<<16);
	width_old+=(header[21]<<24);
//	printf("the width_old of the  picture %d\n",width_old);
      
    //获得文件高度
	height_old = header[22];
	height_old+=(header[23]<<8);
	height_old+=(header[24]<<16);
	height_old+=(header[25]<<16);
//	printf("the height_old of the picture %d\n",height_old);	
	
	fclose(fp);
	return 1;
 }
/*******************************************************************************
void Change_2_2_Data(unsigned char* image) {}
将一张图片的信息,转换成2*2的四张图片信息,并按照上面两张从左上取数据,下面两张从右下取数据
的形式将一张图片的所有信息都取出来放在image里面。
*******************************************************************************/
void Change_2_2_Data(unsigned char* image)
{
	unsigned char a [height][width*2];
	int i,j;

	for(j = height-1;j >= 0;j --)
	{
		for(i = 0;i< 2*width;i++)
		{	
			a[j][i] = *image++;
		}
		
	}

	image = image-width*height*2;
	
	for(j=0;j<height/2;j++)
		{	
			for(i=0;i<width;i=i+2)
			{
				*image=a[height/2-j][i];
				image++;
				*image++=a[height/2-j][i+1];
			
				*image++=a[height/2-j][i+width];
		
				*image++=a[height/2-j][i+width+1];
			
				*image++=a[j+height/2][width-2-i];
			 
				*image++=a[j+height/2][width-1-i];
		
				*image++=a[j+height/2][2*width-2-i];
			
				*image++=a[j+height/2][2*width-1-i];
			}
		}
	
}



//char *tstimage = "dddddddaaaaaaaaaaaaaaaaaaaaaafffffffffffffffffddddddddddfffffffffaaaaaaaaaaaaaaaaaaaaaaaa";

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLcd_downDlg dialog

CLcd_downDlg::CLcd_downDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLcd_downDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLcd_downDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLcd_downDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLcd_downDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLcd_downDlg, CDialog)
	//{{AFX_MSG_MAP(CLcd_downDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
	ON_BN_CLICKED(IDC_BTN_OPEN, OnBtnOpen)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_RECVDATA,OnRecvData)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLcd_downDlg message handlers

BOOL CLcd_downDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	InitSocket();

	RECVPARAM	*pRecvParam = new RECVPARAM;
	pRecvParam->sock = m_socket;
	pRecvParam->hwnd = m_hWnd;

	HANDLE hThread = CreateThread(NULL,0,RecvProc,(LPVOID)pRecvParam,0,NULL);
	CloseHandle (hThread);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLcd_downDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CLcd_downDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CLcd_downDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CLcd_downDlg::InitSocket()
{
	
	m_socket = socket(AF_INET,SOCK_DGRAM,0);
	if(INVALID_SOCKET == m_socket)
	{
		MessageBox("套接字创建失败!");
		return FALSE;
	}

	SOCKADDR_IN		addrSock;
	addrSock.sin_family = AF_INET;
	addrSock.sin_port = htons(5200);
	addrSock.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

	int re_val;
	re_val = bind(m_socket,(SOCKADDR *)&addrSock,sizeof(SOCKADDR));
	if(SOCKET_ERROR ==re_val)
	{
		closesocket(m_socket);
		MessageBox("绑定失败");
		return FALSE;
	}
return TRUE;
}


DWORD WINAPI CLcd_downDlg::RecvProc(LPVOID lpParameter)
{
	SOCKET sock=((RECVPARAM*)lpParameter)->sock;
	HWND hwnd =	((RECVPARAM*)lpParameter)->hwnd;
	
	SOCKADDR_IN	addrFrom;
	int len = sizeof(SOCKADDR);

	char recvBuf[200];
	char tempBuf[300];
	int	reval;
	while(TRUE)
	{
		reval=recvfrom(sock,recvBuf,200,0,(SOCKADDR *)&addrFrom,&len);
		if(SOCKET_ERROR==reval)
			break;
		sprintf(tempBuf,"%s 说 :%s",inet_ntoa(addrFrom.sin_addr),recvBuf);
		::PostMessage(hwnd,WM_RECVDATA,0,(LPARAM)tempBuf);
	}

	return 0;
}
void CLcd_downDlg::OnRecvData(WPARAM wParam,LPARAM lParam)
{
	CString str= (char*) lParam;
	CString strtemp;

	GetDlgItemText(IDC_EDIT_RECV,strtemp);
	str+="\r\n";
	str+=strtemp;
	SetDlgItemText(IDC_EDIT_RECV,str);
}

void CLcd_downDlg::OnBtnSend() 
{
	// TODO: Add your control notification handler code here
		DWORD dwIP;
	((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP);

	SOCKADDR_IN addrTo;
	addrTo.sin_family = AF_INET;
	addrTo.sin_port = htons(5200);
	addrTo.sin_addr.S_un.S_addr = htonl(dwIP);
	
	CString strSend = image;
	static int sendCnt = 0; //记录发送次数
	static int sendLen = 0; //记录发送大小
	while(1)
	{
		if(sendCnt++ >= 1)
		{
			if(width* 2*height - sendCnt*32 <= 0)
			{		
				strSend = image + sendLen*(sendCnt-1);
				sendLen = width* 2*height - (sendCnt-1)*32 ;
				strSend += sendLen;
				MessageBox("the image information have been translated");
				break;
			}
			sendLen = 32;
			strSend = image + sendLen*sendCnt;
		}
 	sendto(m_socket,strSend,sendLen,0,(SOCKADDR*)&addrTo,sizeof(SOCKADDR));
	}

	
}

void CLcd_downDlg::OnBtnOpen() 
{
	// TODO: Add your control notification handler code here
	CFileDialog filedlg(TRUE);
	filedlg.m_ofn.lpstrTitle="打开图片";
	filedlg.m_ofn.lpstrFilter="bmp Files(*.bmp)\0*.bmp\0ALL File(*.*)\0*.*\0\0";
	if(IDOK==filedlg.DoModal())
	{	
	image = (unsigned char *)malloc( width* 2*height);
		memset(image,0,width* 2*height);
		if(bmp_read(filedlg.GetFileName().GetBuffer(NULL),image))	//image  存放所有的图片数据。
		{
		FILE *fp2;
		if(!(fp2=fopen("1.bmp","wb+")))
		return ;
		fwrite(header	,sizeof(unsigned char)	,Headsize,fp2);
		fwrite(image,	sizeof(unsigned char) ,2*width*height, fp2);
		fclose(fp2);
		}
		else
		{
			MessageBox("Open file error!");
		}
	Change_2_2_Data(image);
	}	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -