線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2320
推到 Plurk!
推到 Facebook!

Webcam 照相存檔後再讀出發生問題

答題得分者是:aquarius
Patil
一般會員


發表:4
回覆:7
積分:2
註冊:2010-02-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2010-02-24 15:01:16 IP:114.42.xxx.xxx 訂閱
我使用的是VFW的函式去使webcam連接然後拍照存成bmp檔案
再透過Image物件將bmp檔案秀出並對檔案作處理
連接webcam 拍照都沒有問題
但是最後要利用 Image1->Picture->LoadFromFile("TEST.bmp")卻發生問題 (List Index Out of Bounds(0)
因為不知道如何用CALLBACK將stream資料即時秀在Image物件上 所以只好透過存檔讀檔方式
程式碼如下~ 希望能有前輩可以指導一下~
//---------------------------------------------------------------------------
  1. #include
  2. #pragma hdrstop
  3. #include <math.h></li>
  4. #include
  5. #include
  6. #include "Unit1.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma resource "*.dfm"
  10. HWND hwndCapture;
  11. LPVIDEOHDR VideoStr;
  12. TForm1 *Form1;
  13. //---------------------------------------------------------------------------
  14. __fastcall TForm1::TForm1(TComponent* Owner)
  15. : TForm(Owner)
  16. {
  17. }
  18. //---------------------------------------------------------------------------
  19. void __fastcall TForm1::Close1Click(TObject *Sender)
  20. {
  21. Close();// 程式結束
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
  25. int X, int Y)
  26. {
  27. int R=0,G=0,B=0;
  28. if(!Image1->Picture->Bitmap->Empty)
  29. {
  30. R=(Image1->Canvas->Pixels[X][Y] & 0x000000FF);
  31. G=(Image1->Canvas->Pixels[X][Y] & 0x0000FF00) >> 8;
  32. B=(Image1->Canvas->Pixels[X][Y] & 0x00FF0000) >> 16;
  33. StatusBar1->Panels->Items[0]->Text="X=" IntToStr(X);
  34. StatusBar1->Panels->Items[1]->Text="Y=" IntToStr(Y);
  35. StatusBar1->Panels->Items[2]->Text="RGB=" IntToStr(R) IntToStr(G) IntToStr(B);
  36. }//秀出影像中滑鼠位置所在座標與像素值
  37. }
  38. //---------------------------------------------------------------------------
  39. void __fastcall TForm1::Button1Click(TObject *Sender)
  40. {
  41. 此處圖像運算求平均Hue值
  42. }
  43. //---------------------------------------------------------------------------
  44. void __fastcall TForm1::Connect1Click(TObject *Sender)//此段為連接webcam
  45. {
  46. int device=0;
  47. hwndCapture=capCreateCaptureWindow((LPSTR) "Camera",WS_VISIBLE,0,0,640,480,(HWND) Form1->Panel1->Handle,(int) 0);
  48. for(device=0;device<10;device )
  49. {
  50. if(capDriverConnect(hwndCapture,device))
  51. break;
  52. }
  53. if(device==10)
  54. {
  55. ShowMessage("No webcam device exist!");
  56. exit(EXIT_FAILURE) ;
  57. }
  58. capOverlay(hwndCapture,true);
  59. capPreviewRate(hwndCapture,66);
  60. capPreview(hwndCapture,true);
  61. }
  62. //---------------------------------------------------------------------------
  63. void __fastcall TForm1::Disconnect1Click(TObject *Sender)//此段為webcam離線
  64. {
  65. capCaptureStop(hwndCapture);
  66. capPreview(hwndCapture,false);
  67. if(capDriverDisconnect(hwndCapture))
  68. {
  69. DestroyWindow(hwndCapture);
  70. ShowMessage("Webcam Disconnect !!");
  71. }
  72. else
  73. {
  74. ShowMessage("Webcam Disconnect Fail !!");
  75. }
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TForm1::Capture1Click(TObject *Sender)
  79. {
  80. capFileSaveDIB(hwndCapture,"C:\\LED.bmp");//拍照存檔
  81. capCaptureStop(hwndCapture);
  82. capPreview(hwndCapture,false);
  83. Sleep(1000);
  84. Image1->Picture->LoadFromFile("C:\\LED.bmp");//讀取bmp影像
  85. }
  86. //---------------------------------------------------------------------------





Patil
一般會員


發表:4
回覆:7
積分:2
註冊:2010-02-23

發送簡訊給我
#2 引用回覆 回覆 發表時間:2010-02-24 16:50:12 IP:114.42.xxx.xxx 訂閱
嗯...把StatusBar1那段程式拿掉就沒問題了~
不知道是不是因為StatusBar1的Index有限 因此我超過範圍就出現 out of bounds(0)
搞了個烏龍...

只是現在還搞不太懂如何將即時影像show在Image物件上~ 爬了很多文章
但是對於CALLBACK 函數還是不大知道原理跟參數型態等~
aquarius
資深會員


發表:3
回覆:347
積分:330
註冊:2003-05-21

發送簡訊給我
#3 引用回覆 回覆 發表時間:2010-02-25 21:32:39 IP:60.250.xxx.xxx 訂閱
不是很清楚你想問的問題是 statusbar 的錯誤, 還是想取得影像的方式?
取得影像, 用 VFW 應只有 capFileSaveDIB , 不過有在別的地方看到和你不同的用法, 你可參考看看


[code cpp]
CFileDialog fileopen(FALSE,_T("DIB"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("*.dib|*.dib|*.jpeg|*.jpeg|*.PNG|*.png|"));
if ( IDOK == fileopen.DoModal() ) {
CString path("");
CString ext("");
path = fileopen.GetPathName();
ext = fileopen.GetFileExt();
capGrabFrameNoStop(m_caphwnd);

capFileSaveDIB(m_caphwnd,path.GetString());

if ( ext != CString("DIB") && ext != CString("dib") ) {
CImage myImage;
myImage.Load(path.GetString());
myImage.Save(path.GetString());
}
}

[/code]

------
水瓶男的blog: http://791909.blogspot.com
Patil
一般會員


發表:4
回覆:7
積分:2
註冊:2010-02-23

發送簡訊給我
#4 引用回覆 回覆 發表時間:2010-03-01 20:13:47 IP:114.32.xxx.xxx 訂閱

===================引 用 aquarius 文 章===================
不是很清楚你想問的問題是 statusbar 的錯誤, 還是想取得影像的方式?
取得影像, 用 VFW 應只有 capFileSaveDIB , 不過有在別的地方看到和你不同的用法, 你可參考看看


[code cpp]
?CFileDialog fileopen(FALSE,_T("DIB"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("*.dib|*.dib|*.jpeg|*.jpeg|*.PNG|*.png|"));
?if ( IDOK == fileopen.DoModal() ) {
? CString path("");
? CString ext("");
? path = fileopen.GetPathName();
? ext = fileopen.GetFileExt();
? capGrabFrameNoStop(m_caphwnd);

? capFileSaveDIB(m_caphwnd,path.GetString());

? if ( ext != CString("DIB") && ext != CString("dib") ) {?
?? CImage myImage;
?? myImage.Load(path.GetString());
?? myImage.Save(path.GetString());
? }
?}

[/code]

那請問有用法是用指標來接Webcam中的即時影像資訊,然後再透過指標將資料送到Image1物件的方式嗎?
aquarius
資深會員


發表:3
回覆:347
積分:330
註冊:2003-05-21

發送簡訊給我
#5 引用回覆 回覆 發表時間:2010-03-02 22:58:25 IP:114.43.xxx.xxx 訂閱
試試這個, 沒拿去 BCB下編譯, 不過應該沒寫錯... :)
可以把 Panel 上的影像存到 bmp 中.


[code cpp]
Graphics::TBitmap *bmp = new Graphics::TBitmap;
bmp->Width = Panel1->Width;
bmp->Height = Panel1->Height;
HDC hDC=GetDC(Panel1->Handle);

BitBlt(bmp->Canvas->Handle,0,0,bmp->Width,bmp->Height,hDC,0,0,0xCC0020); //SRCCOPY

bmp->SaveToFile("panel.bmp");
ReleaseDC(Panel1->Handle , hDC);
delete bmp;
[/code]

------
水瓶男的blog: http://791909.blogspot.com
編輯記錄
aquarius 重新編輯於 2010-03-02 23:00:14, 註解 無‧
aquarius 重新編輯於 2010-03-02 23:00:59, 註解 無‧
aquarius 重新編輯於 2010-03-02 23:01:25, 註解 無‧
系統時間:2024-04-24 17:16:35
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!