sercap.cpp

来自「BCB下驱动摄像头的例程」· C++ 代码 · 共 74 行

CPP
74
字号
//---------------------------------------------------------------------------

#include <vcl.h>
#include <stdio.h>
#pragma hdrstop
#include "bmp.h"
#include "Unit1.h"
#include "sercap.h"
FILE *fpBMP,*fpDATA;
//#pragma package(smart_init)
//------------------------------------------------------------------------------
extern int index;
extern HANDLE com1_handle;;
extern unsigned char *BMPbuffer ,*CAMdata;
extern BMP_Header_T BmpHeader;
__fastcall TCOM1_Thread::TCOM1_Thread(bool CreateSuspended)
    : TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall TCOM1_Thread::Execute()
{
   unsigned long read_bytes;
   unsigned char com1_buf[20000];
   int x,y;
    while(!Terminated){
        read_bytes = 20000;

        ReadFile(com1_handle, com1_buf, read_bytes, &read_bytes, NULL);

        if(read_bytes != 0){
                memcpy(CAMdata + index, com1_buf, read_bytes);
                index += read_bytes;
               
        }

         Form1->StatusBar1->Panels->Items[0]->Text = "Receive bytes:"+IntToStr(index);
         
         if (index == 800*600){
         fpDATA = fopen("cam.dat","wb");
         fwrite(CAMdata,800*600,1,fpDATA);
         fclose(fpDATA);
         for(y = 0; y<600; y--)
                for(x = 0; x<800; x++){
                        if(y%2 == 0){
                                if(x%2 == 0)
                                        *(BMPbuffer + ((599 - y)*800 + x)*3+2)=*(CAMdata+y*800+x);
                                else
                                        *(BMPbuffer + ((599 - y)*800 + x)*3+1)=*(CAMdata+y*800+x);
                                }
                        else{
                                if(x%2 == 0)
                                        *(BMPbuffer + ((599 - y)*800 + x)*3+1)=*(CAMdata+y*800+x);
                                else
                                        *(BMPbuffer + ((599 - y)*800 + x)*3+0)=*(CAMdata+y*800+x);
                                }

                }
         fpBMP = fopen("cam.bmp","wb");
         fwrite(&BmpHeader,sizeof(BmpHeader),1,fpBMP);
         fwrite(BMPbuffer,800*600*3,1,fpBMP);
         fclose(fpBMP);
         CloseHandle(com1_handle);
         delete BMPbuffer;
         delete CAMdata;
         Form1->Image1->Picture->LoadFromFile("cam.bmp");
         break;         
         }
                
    }
    
}
//---------------------------------------------------------------------------
 

⌨️ 快捷键说明

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