輸入原理:
1. capCreateCaptureWindow 先指定視窗handle給裝置
2. capDriverConnect 連結裝置
3. 設定&拷貝 capDriverGetCaps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//啟動 | |
bool __fastcall TForm_main::OpenOCR() | |
{ | |
int i,width=320,height=240; | |
bool a = false; | |
if(!hCapWnd) | |
{ // Form_main->Handle //0 | |
hCapWnd = capCreateCaptureWindow("My Capture Window", WS_CHILD | WS_VISIBLE,0, 0, 320, 240 ,PanelOCR->Handle, (int)1 );//建立一個擷取視窗 | |
//以下的For迴圈是為了去連結裝置 | |
for(i=0;i<10;i++) | |
{ | |
a=capDriverConnect(hCapWnd,i); //連接裝置 | |
if(a) break; | |
} | |
//如果沒有啟動成功,則a為false,反之則為true | |
if(!a) | |
{ | |
//ShowMessage("Connect Camera Error!!"); | |
Panel5->Caption="CAM ERR!"; | |
return false; | |
} | |
PanelOCR->Width=width; | |
PanelOCR->Height=height; | |
capOverlay(hCapWnd,true); //開啟覆疊 | |
//capPreviewScale(hCapWnd,true); //開啟比例 | |
capPreviewRate(hCapWnd, 1000/10); //每秒10畫格 | |
capPreview(hCapWnd,true); //開啟預覽 | |
//capPreview(hCapWnd,false); //關閉預覽 | |
//20161114++ | |
CAPDRIVERCAPS *psCaps; | |
psCaps= new CAPDRIVERCAPS; | |
capDriverGetCaps (hCapWnd,psCaps,sizeof(CAPDRIVERCAPS)); | |
capOverlay(hCapWnd,true); | |
delete psCaps; | |
//20161114++ | |
} | |
TimerOCR->Enabled=true; | |
return true; | |
} | |
//--------------------------------------------------------------------------- | |
//關閉 | |
bool __fastcall TForm_main::CloseOCR() | |
{ | |
TimerOCR->Enabled=false; | |
if(hCapWnd) | |
{ | |
capDriverDisconnect(hCapWnd); //停止裝置 | |
hCapWnd=NULL; | |
} | |
return true; | |
} | |
//--------------------------------------------------------------------------- | |
//擷取 | |
void __fastcall TForm_main::GetImageOCR() | |
{ | |
//20161130++ | |
Graphics::TBitmap*Source=new Graphics::TBitmap(); | |
Source->PixelFormat = pf24bit; //2009/1/13 add for iroi important | |
//開始抓取 | |
SendMessage(hCapWnd,WM_CAP_GRAB_FRAME,0,0); | |
capGrabFrameNoStop(hCapWnd); | |
capEditCopy(hCapWnd); | |
try | |
{ | |
//用剪貼簿的方式讀取 | |
if(Clipboard()->HasFormat(CF_BITMAP))Source->LoadFromClipboardFormat(CF_BITMAP,Clipboard()->GetAsHandle(CF_BITMAP),0); | |
//務必要設定大小 因為新的攝影機輸入640*480會當掉 | |
Source->Width=PanelOCR->Width; | |
Source->Height=PanelOCR->Height; | |
//剪裁成適當尺寸 | |
BitBlt(Source->Canvas->Handle, 0, 0, PanelOCR->Width, PanelOCR->Height, Source->Canvas->Handle, 0, 0, SRCCOPY); | |
//存圖測試 | |
AnsiString VFilename = "tmp.bmp"; //for store | |
Source->SaveToFile(VFilename.c_str()); | |
//目標顯示 | |
ImageOCR->Picture->Assign(Source); | |
//再放回已剪裁的圖 | |
Clipboard()->Assign(Source); | |
}catch(Exception &E){WriteLog("ERR"+E.Message);} | |
delete Source; | |
} | |
//--------------------------------------------------------------------------- |
輸出到eVision的結構中使用:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void __fastcall TForm_Learn::SpeedButton1Click(TObject *Sender) | |
{ | |
TCanvas *csBuf=new TCanvas; | |
HDC hDC=GetDC(Form_main->PanelOCR->Handle); | |
csBuf->Handle=hDC; | |
Graphics::TBitmap*Source=new Graphics::TBitmap(); | |
Source->Width=Form_main->PanelOCR->Width; | |
Source->Height=Form_main->PanelOCR->Height; | |
Source->PixelFormat = pf24bit; //2009/1/14 important | |
Source->Canvas->CopyRect(Rect(0, 0, Source->Width, Source->Height),csBuf, Rect(0, 0, Form_main->PanelOCR->Width, Form_main->PanelOCR->Height)); | |
byte* pSrc; | |
byte* pDest; | |
SrcImg->SetSize(320,240); | |
for(int row = 0; row < Source->Height; row++) | |
{ | |
pSrc = (Byte *)Source->ScanLine[row]; | |
pDest = (Byte *)SrcImg->m_pImagePtr + row * Source->Width * 3; | |
CopyMemory(pDest, pSrc, Source->Width * 3); | |
} | |
//for 灰階影像 | |
BWSrc.SetSize(320,240); | |
BWDst.SetSize(320,240); | |
ClrGetComponent(SrcImg,&BWSrc, 1); | |
ReleaseDC(0,hDC); | |
delete csBuf; | |
delete Source; | |
RedrawWindow(Handle, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW); | |
} |
沒有留言:
張貼留言