智能车制作

 找回密码
 注册

扫一扫,访问微社区

查看: 4395|回复: 8
打印 上一主题 下一主题

C语言写的摄像头上位机源码

[复制链接]

4

主题

22

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
257
威望
146
贡献
67
兑换币
80
注册时间
2013-3-15
在线时间
22 小时
毕业学校
USTC
跳转到指定楼层
1#
发表于 2013-3-15 22:24:13 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
#include <windows.h>
//#include <debug.h>
#include <math.h>
//#include <stdio.h>
#define NFIELD 2000
#define NHEIGHT 50

#define NWIDTHBYTES 16
#define NWIDTH 128//16*8=128
POINT point,point1;
FLOAT SweepAngle;
UINT ntimer;
RECT rect1;
TCHAR sztimer[10]={0};
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void fnInit(void);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                        PSTR szCmdLine, int iCmdShow)
{
        static TCHAR szAppName[] = TEXT ("HelloWin") ;
        HWND   hwnd ;
        MSG    msg ;
        WNDCLASS wndclass ;
        fnInit();
        wndclass.style        = CS_HREDRAW | CS_VREDRAW ;
        wndclass.lpfnWndProc  = WndProc ;
        wndclass.cbClsExtra   = 0 ;
        wndclass.cbWndExtra   = 0 ;
        wndclass.hInstance    = hInstance ;
        wndclass.hIcon        = LoadIcon (NULL, IDI_APPLICATION) ;
        wndclass.hCursor      = LoadCursor (NULL, IDC_ARROW) ;
        wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
        wndclass.lpszMenuName = NULL ;
        wndclass.lpszClassName= szAppName ;
        if (!RegisterClass (&wndclass))
        {
                MessageBox (  NULL, TEXT ("This program requires Windows NT!"),
                        szAppName, MB_ICONERROR) ;
                return 0 ;
        }
        hwnd = CreateWindow( szAppName,      // window class name
                TEXT ("GDIDraw"),   // window caption
                WS_OVERLAPPEDWINDOW,  // window style
                CW_USEDEFAULT,// initial x position
                CW_USEDEFAULT,// initial y position
                800,// initial x size
                600,// initial y size
                NULL,                 // parent window handle
                NULL,            // window menu handle
                hInstance,   // program instance handle
                NULL) ;      // creation parameters
        ShowWindow (hwnd, iCmdShow) ;
        UpdateWindow (hwnd) ;
        while (GetMessage (&msg, NULL, 0, 0))
        {
                TranslateMessage (&msg) ;
                DispatchMessage (&msg) ;
        }
        return msg.wParam ;
}
void fnInit(void){
        point.x=0;point.y=0;point1.x=0;point1.y=0;
        SweepAngle=0;
        ntimer=0;
        rect1.bottom=20;
        rect1.left=20;
        rect1.right=20;
        rect1.top=20;
}
void fnDrawBezier(HDC hdc,POINT *apt){
        PolyBezier(hdc,apt,4);
        MoveToEx(hdc,apt[0].x,apt[0].y,NULL);
        LineTo(hdc,apt[1].x,apt[1].y);
        MoveToEx(hdc,apt[2].x,apt[2].y,NULL);
        LineTo(hdc,apt[3].x,apt[3].y);
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        HDC                   hdc ;
        INT iLength,ix,iy,i,j,k;
        static int cxClient,cyClient;
        static int cxPerRect;
        static INT cxChar,cyChar;
//FILE *pFile=NULL;
        OFSTRUCT OfStruct;
        HFILE hFile=NULL;
        DWORD w;
        static byte arrMovie[NFIELD][NHEIGHT][NWIDTHBYTES];
        static byte arrInfo[512];
        TCHAR szBuffer[100];
        static int curField=0;
        PAINTSTRUCT ps ;
        TEXTMETRIC tm;
        RECT          rect;
        POINT aptsin[1000];
        static POINT apt[4];
        GetClientRect(hwnd,&rect);
        switch (message)
        {
        case WM_CREATE:
//                PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
                SetTimer(hwnd,1,1000,NULL);
                hdc=GetDC(hwnd);
                GetTextMetrics(hdc,&tm);//得到设备字体
                cxChar=tm.tmAveCharWidth;//作为宽度
                cyChar=tm.tmHeight+tm.tmExternalLeading;//作为高度
                ReleaseDC(hwnd,hdc);

                //读文件放到大数组里面
               
                /*if(!(pFile=fopen("datatest.hex","r")))exit(1);*/
//                fseek(pFile,512,SEEK_SET);

                hFile=OpenFile("data.hex",(LPOFSTRUCT)&OfStruct,OF_READ);
                if(!hFile)exit(1);
               
                INT nSectorPerField,nBytesPerField;
                nSectorPerField=NHEIGHT*NWIDTHBYTES/512+1;
                nBytesPerField=nSectorPerField*512;


                if(!ReadFile((HANDLE)hFile,(LPVOID)arrInfo,512,&w,NULL))exit(2);

                for(i=0;i<NFIELD;i++){
                        //fread((byte *)arrMovie[i],nBytesPerField,nBytesPerField,pFile);
                        if(!ReadFile((HANDLE)hFile,(LPVOID)arrMovie[i],NWIDTHBYTES*NHEIGHT,&w,NULL))exit(2);
                        //if(!ReadFile((HANDLE)hFile,(LPVOID)arrInfo,nBytesPerField-NWIDTHBYTES*NHEIGHT,&w,NULL))exit(3);
                        SetFilePointer((HANDLE)hFile,nBytesPerField-NWIDTHBYTES*NHEIGHT,NULL,FILE_CURRENT);
                }
               
                CloseHandle((HANDLE)hFile);
                OutputDebugStr("Completed read file");


                return 0 ;
        case WM_SIZE:
                cxClient=LOWORD(lParam);
                cyClient=HIWORD(lParam);
                cxPerRect=cxClient/NWIDTH-1;

//                 apt[0].x=cxClient/4;
//                 apt[0].y=cyClient/2;
//                 apt[1].x=cxClient/2;
//                 apt[1].y=cyClient/4;
//                 apt[2].x=cxClient/2;
//                 apt[2].y=3*cyClient/4;
//                 apt[3].x=3*cxClient/4;
//                 apt[3].y=cyClient/2;
                return 0;
//         case WM_LBUTTONDOWN:
//         case WM_RBUTTONDOWN:
//         case WM_MOUSEMOVE:
//                 if(wParam & MK_LBUTTON || wParam & MK_RBUTTON){
//                         hdc=GetDC(hwnd);
//                         SelectObject(hdc,GetStockObject(WHITE_PEN));
//                         fnDrawBezier(hdc,apt);
//
//                 if(wParam & MK_LBUTTON){
//                         apt[1].x=LOWORD(lParam);
//                         apt[1].y=HIWORD(lParam);
//                 }
//                 if(wParam & MK_RBUTTON){
//                         apt[2].x=LOWORD(lParam);
//                         apt[2].y=HIWORD(lParam);
//                 }
//                 SelectObject(hdc,GetStockObject(BLACK_PEN));
//                 fnDrawBezier(hdc,apt);
//                 ReleaseDC(hwnd,hdc);
//                 InvalidateRect(hwnd,NULL,TRUE);
//                 }
//                
//                 return 0;


        case   WM_PAINT:
                hdc = BeginPaint (hwnd, &ps) ;
                /*                GetClientRect (hwnd, &rect) ;*/
                //                 DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
                //                         DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
                POINT point_middle;               
                HPEN hPen,hOldPen;
                HBRUSH hBrush,hOldBrush;
                LOGBRUSH logBrush;
                logBrush.lbColor=RGB(0xff,0,0);
                logBrush.lbStyle=BS_SOLID;
                logBrush.lbHatch=HS_HORIZONTAL;
                hPen=CreatePen(PS_SOLID,2,RGB(128,0,0));
                hBrush=CreateBrushIndirect(&logBrush);
                hOldPen=(HPEN)SelectObject(hdc,hPen);
                hOldBrush=(HBRUSH)SelectObject(hdc,hBrush);
                point_middle.x=(rect.right+rect.left)/2;
                point_middle.y=(rect.top+rect.bottom)/2;

                SetTextColor(hdc,RGB(0xff,0,0));
                /*SetROP2(hdc,R2_NOT);*/
                /*SetBkMode(hdc,TRANSPARENT);*/

                MoveToEx(hdc,point_middle.x,point_middle.y,NULL);
                /*                LineTo(hdc,point.x,point.y);*/
                /*AngleArc(hdc,point_middle.x,point_middle.y,100,0,SweepAngle);*/
                Rectangle(hdc,rect1.left,rect1.top,rect1.right,rect1.bottom );
                /*                Rectangle(hdc,10,10,50,50);*/
                TCHAR bfcxPerRect[20];

                RECT rectPerDot;
                BYTE byteColor;
                UINT iHozn,iVert;
//                 SetRect(&rectPerDot,0,0,30*cxChar,3*cyChar);
//                 hBrush=(HBRUSH)GetStockObject(WHITE_BRUSH);
//                 FillRect(hdc,&rectPerDot,hBrush);
//                 DeleteObject(hBrush);

                HRGN hRgnMap;
                HRGN hRgnText;
                hRgnMap=CreateRectRgn(0,3*cyChar,cxClient,cyClient);
                hRgnText=CreateRectRgn(0,0,cxClient,cyClient);
                SelectClipRgn(hdc,hRgnMap);               
                SetViewportOrgEx(hdc,0,3*cyChar,NULL);

                for(iVert=0;iVert<NHEIGHT;iVert++){
                        SetRect(&rectPerDot,0,0,cxPerRect-1,cxPerRect-1);
                        OffsetRect(&rectPerDot,0,iVert*cxPerRect);
                        for(i=0;i<NWIDTH;i++){
                                OffsetRect(&rectPerDot,cxPerRect,0);
                                byteColor=  ((arrMovie[curField][iVert][i/8]) & (1<<i%8)) ? 0:255;
                                hBrush=CreateSolidBrush(RGB(byteColor,byteColor,byteColor));
                                FillRect(hdc,&rectPerDot,hBrush);//do NOT need to select the brush to hdc
                                DeleteObject(hBrush);
                                if(i%8==0){
                                        MoveToEx(hdc,(1+i)*cxPerRect,0,NULL);
                                        LineTo(hdc,(1+i)*cxPerRect,cxPerRect*NHEIGHT);
                                }
                        }
                }

                DeleteObject(hRgnMap);
                SelectClipRgn(hdc,hRgnText);
               
//                 for(ix=0;ix<rect.right;ix+=20){
//                         MoveToEx(hdc,ix,0,NULL);
//                         LineTo(hdc,ix,rect.bottom);
//                 }
//                 for(iy=0;iy<rect.bottom;iy+=20){
//                         MoveToEx(hdc,0,iy,NULL);
//                         LineTo(hdc,rect.right,iy);
//                 }
//                 int i;
//                 for(i=0;i<1000;i++){
//                         aptsin[i].x=i*rect.right/1000;
//                         aptsin[i].y=(int)(rect.bottom/2*(1-sin(2*3.1415926*i/1000)));
//                 }
//                 Polyline(hdc,aptsin,1000);

                /*fnDrawBezier(hdc,apt);*/
                SetViewportOrgEx(hdc,0,0,NULL);

               
                TextOut(hdc,0,cyChar,szBuffer,strlen(strcat(strcpy(szBuffer,"time elapsed:"),sztimer)));
                TextOut(hdc,0,2*cyChar,bfcxPerRect,wsprintf(bfcxPerRect,"cxPerRect:%d",cxPerRect));
                TextOut(hdc,20*cxChar,2*cyChar,szBuffer,wsprintf(szBuffer,"Current Field:%4d",curField));
                /*                DrawText(hdc,TEXT(sztimer),-1,&rect,DT_SINGLELINE);*/
                iLength=wsprintf(szBuffer,TEXT("It is %d,cxChar %d,cyChar %d"),399,cxChar,cyChar);//类似printf,不过是输出到字符串中。
                TextOut(hdc,0,0,szBuffer,iLength);//iLength是长度

                DeleteObject(hPen);
                DeleteObject(hBrush);
                SelectObject(hdc,hOldBrush);
                SelectObject(hdc,hOldPen);
                DeleteObject(hRgnText);
                EndPaint (hwnd, &ps) ;


                return 0 ;
        case WM_TIMER:
                /*point.x+=10;*/
                /*SweepAngle!=360?SweepAngle+=10:SweepAngle=0;*/
                ntimer++;
                _itoa_s(ntimer,sztimer,10);
                InvalidateRect(hwnd,&rect,0);
                return 0;
        case WM_KEYDOWN:
                switch(wParam){
                                        case VK_UP:
                                                SweepAngle+=10;
                                                break;
                                        case VK_DOWN:
                                                SweepAngle-=10;
                                                break;
                                        case VK_ESCAPE:
                                                PostQuitMessage(1);
                                                break;
                                        case VK_RIGHT:
                                                curField++;
                                                if(curField==NFIELD){
                                                        curField=0;
                                                }
                                                break;
                                        case VK_LEFT:
                                                curField--;
                                                if(curField==-1){
                                                        curField=NFIELD-1;
                                                }
                                                break;
                                        default:
                                                ;
                }
                InvalidateRect(hwnd,&rect,0);
                return 0;
        case WM_CHAR:
                /*ntimer=0;*/
                switch(wParam){
        case '1':
                rect1.bottom=50;rect1.left=10;rect1.right=50;rect1.top=10;
                break;
        case 'a':
                rect1.left += 10;
                break;
        case 'd':
                rect1.right += 10;
                break;
        case 'w':
                rect1.top += 10;
                break;
        case 's':
                rect1.bottom += 10;
                break;
        default:
                ;
                }
                InvalidateRect(hwnd,&rect,0);
                return 0;
        case   WM_DESTROY:
                KillTimer(hwnd,1);
                PostQuitMessage (0) ;
                wsprintf(szBuffer,"test %d",399);
                OutputDebugStr(szBuffer);
                return 0 ;
        }
        return DefWindowProc (hwnd, message, wParam, lParam) ;
}


0

主题

53

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1528
威望
789
贡献
343
兑换币
468
注册时间
2012-9-24
在线时间
198 小时
9#
发表于 2013-4-29 13:38:48 | 只看该作者
厉害啊
回复 支持 反对

使用道具 举报

4

主题

22

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
257
威望
146
贡献
67
兑换币
80
注册时间
2013-3-15
在线时间
22 小时
毕业学校
USTC
8#
 楼主| 发表于 2013-3-28 16:11:18 | 只看该作者
彬⌒_⌒/wx 发表于 2013-3-19 17:41
楼主好人啊,厉害!
在VC里编译出现一个错误:Error spawning cl.exe
这是怎么回事?

好像是VC的设置问题。百度知道里看看:
http://zhidao.baidu.com/question/20787147.html
还有,据说就现在看来,VC6.0有些问题。我用的是VS2008.还比较好用。
回复 支持 反对

使用道具 举报

1

主题

15

帖子

0

精华

高级会员

Rank: 4

积分
646
威望
349
贡献
159
兑换币
126
注册时间
2012-4-16
在线时间
69 小时
毕业学校
广东海洋大学
7#
发表于 2013-3-19 17:41:39 | 只看该作者
楼主好人啊,厉害!
在VC里编译出现一个错误:Error spawning cl.exe
这是怎么回事?
回复 支持 反对

使用道具 举报

4

主题

22

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
257
威望
146
贡献
67
兑换币
80
注册时间
2013-3-15
在线时间
22 小时
毕业学校
USTC
6#
 楼主| 发表于 2013-3-18 23:24:32 | 只看该作者
Frozenflame 发表于 2013-3-18 23:11
在devcpp和vs2008里都试过,可以用。不过那个数据的格式比较复杂。。其实是用512*2个字节,像素是128*50。 ...

for(iVert=0;iVert<NHEIGHT;iVert++){
                        SetRect(&rectPerDot,0,0,cxPerRect-1,cxPerRect-1);
                        OffsetRect(&rectPerDot,0,iVert*cxPerRect);
                        for(i=0;i<NWIDTH;i++){
                                OffsetRect(&rectPerDot,cxPerRect,0);
                                byteColor=  ((arrMovie[curField][iVert][i/8]) & (1<<i%8)) ? 0:255;
                                hBrush=CreateSolidBrush(RGB(byteColor,byteColor,byteColor));
                                FillRect(hdc,&rectPerDot,hBrush);//do NOT need to select the brush to hdc
                                DeleteObject(hBrush);
                                if(i%8==0){
                                        MoveToEx(hdc,(1+i)*cxPerRect,0,NULL);
                                        LineTo(hdc,(1+i)*cxPerRect,cxPerRect*NHEIGHT);
                                }
                        }
                }
}
这个是画图的。
回复 支持 反对

使用道具 举报

4

主题

22

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
257
威望
146
贡献
67
兑换币
80
注册时间
2013-3-15
在线时间
22 小时
毕业学校
USTC
5#
 楼主| 发表于 2013-3-18 23:11:14 | 只看该作者
I'M_Not_The 发表于 2013-3-16 14:44
不行啊

在devcpp和vs2008里都试过,可以用。不过那个数据的格式比较复杂。。其实是用512*2个字节,像素是128*50。
回复 支持 反对

使用道具 举报

45

主题

761

帖子

0

精华

常驻嘉宾

Rank: 8Rank: 8

积分
5023
威望
2903
贡献
558
兑换币
1271
注册时间
2012-10-16
在线时间
781 小时
4#
发表于 2013-3-16 14:44:17 | 只看该作者
不行啊
回复 支持 反对

使用道具 举报

68

主题

1086

帖子

0

精华

常驻嘉宾

Rank: 8Rank: 8

积分
4578

活跃会员奖章优秀会员奖章论坛元老奖章论坛骨干奖章在线王奖章

QQ
威望
2515
贡献
1213
兑换币
1382
注册时间
2012-9-19
在线时间
425 小时
3#
发表于 2013-3-16 10:34:54 | 只看该作者
好厉害啊
回复 支持 反对

使用道具 举报

8

主题

206

帖子

0

精华

高级会员

Rank: 4

积分
808

活跃会员奖章优秀会员奖章

QQ
威望
418
贡献
300
兑换币
174
注册时间
2011-12-23
在线时间
45 小时
2#
发表于 2013-3-16 09:16:22 | 只看该作者
真是厉害!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关于我们|联系我们|小黑屋|智能车制作 ( 黑ICP备2022002344号

GMT+8, 2024-9-29 05:23 , Processed in 0.091231 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表