Delphi-OpenCV/component/uOCVCamera.pas

226 lines
6.3 KiB
ObjectPascal
Raw Normal View History

(* /*****************************************************************
// Delphi-OpenCV Demo
// Copyright (C) 2013 Project Delphi-OpenCV
// ****************************************************************
// Contributor:
// laentir Valetov
// email:laex@bk.ru
// ****************************************************************
// You may retrieve the latest version of this file at the GitHub,
// located at git://github.com/Laex/Delphi-OpenCV.git
// ****************************************************************
// The contents of this file are used with permission, subject to
// the Mozilla Public License Version 1.1 (the "License"); you may
// not use this file except in compliance with the License. You may
// obtain a copy of the License at
// http://www.mozilla.org/MPL/MPL-1_1Final.html
//
// Software distributed under the License is distributed on an
// "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
// implied. See the License for the specific language governing
// rights and limitations under the License.
******************************************************************* *)
unit uOCVCamera;
interface
uses
System.SysUtils,
System.Classes,
core.types_c,
highgui_c,
uOCVTypes;
type
TocvCameraCaptureSource = //
(CAP_ANY { = 0 } , // autodetect
CAP_MIL { = 100 } , // MIL proprietary drivers
CAP_VFW { = 200 } , // platform native
CAP_V4L { = 200 } , //
CAP_V4L2 { = 200 } , //
CAP_FIREWARE { = 300 } , // IEEE 1394 drivers
CAP_FIREWIRE { = 300 } , //
CAP_IEEE1394 { = 300 } , //
CAP_DC1394 { = 300 } , //
CAP_CMU1394 { = 300 } , //
CAP_STEREO { = 400 } , // TYZX proprietary drivers
CAP_TYZX { = 400 } , //
TYZX_LEFT { = 400 } , //
TYZX_RIGHT { = 401 } , //
TYZX_COLOR { = 402 } , //
TYZX_Z { = 403 } , //
CAP_QT { = 500 } , // QuickTime
CAP_UNICAP { = 600 } , // Unicap drivers
CAP_DSHOW { = 700 } , // DirectShow (via videoInput)
CAP_PVAPI { = 800 } , // PvAPI, Prosilica GigE SDK
CAP_OPENNI { = 900 } , // OpenNI (for Kinect)
CAP_OPENNI_ASUS { = 910 } , // OpenNI (for Asus Xtion)
CAP_ANDROID { = 1000 } , // Android
CAP_XIAPI { = 1100 } , // XIMEA Camera API
CAP_AVFOUNDATION { = 1200 } );
type
TocvOnDataNotify = procedure(const IplImage: pIplImage) of object;
TocvCameraThread = class(TThread)
private
FOnNotifyData: TocvOnDataNotify;
protected
FCapture: pCvCapture;
procedure Execute; override;
public
property OnNotifyData: TocvOnDataNotify Read FOnNotifyData write FOnNotifyData;
end;
TocvCamera = class(TocvDataSource)
private
FEnabled: Boolean;
FCameraCaptureSource: TocvCameraCaptureSource;
procedure SetEnabled(const Value: Boolean);
procedure SetCameraCaptureSource(const Value: TocvCameraCaptureSource);
protected
FCapture: pCvCapture;
FOpenCVCameraThread: TocvCameraThread;
procedure OnNotifyData(const IplImage: pIplImage);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Enabled: Boolean Read FEnabled write SetEnabled default False;
property CameraCaptureSource: TocvCameraCaptureSource read FCameraCaptureSource write SetCameraCaptureSource
default CAP_ANY;
end;
implementation
const
ocvCameraCaptureSource: array [TocvCameraCaptureSource] of Longint = //
(CV_CAP_ANY, // autodetect
CV_CAP_MIL, // MIL proprietary drivers
CV_CAP_VFW, // platform native
CV_CAP_V4L, //
CV_CAP_V4L2, //
CV_CAP_FIREWARE, // IEEE 1394 drivers
CV_CAP_FIREWIRE, //
CV_CAP_IEEE1394, //
CV_CAP_DC1394, //
CV_CAP_CMU1394, //
CV_CAP_STEREO, // TYZX proprietary drivers
CV_CAP_TYZX, //
CV_TYZX_LEFT, //
CV_TYZX_RIGHT, //
CV_TYZX_COLOR, //
CV_TYZX_Z, //
CV_CAP_QT, // QuickTime
CV_CAP_UNICAP, // Unicap drivers
CV_CAP_DSHOW, // DirectShow (via videoInput)
CV_CAP_PVAPI, // PvAPI; Prosilica GigE SDK
CV_CAP_OPENNI, // OpenNI (for Kinect)
CV_CAP_OPENNI_ASUS, // OpenNI (for Asus Xtion)
CV_CAP_ANDROID, // Android
CV_CAP_XIAPI, // XIMEA Camera API
CV_CAP_AVFOUNDATION);
ThreadSleepConst = 20;
{ TOpenCVCameraThread }
procedure TocvCameraThread.Execute;
Var
frame: pIplImage;
begin
while not Terminated do
if Assigned(FCapture) then
begin
try
frame := cvQueryFrame(FCapture);
if Assigned(frame) and Assigned(OnNotifyData) then
Synchronize(
procedure
begin
OnNotifyData(frame);
end)
except
end;
end
else
Suspend;
end;
{ TOpenCVCamera }
constructor TocvCamera.Create(AOwner: TComponent);
begin
inherited;
if not(csDesigning in ComponentState) then
begin
FOpenCVCameraThread := TocvCameraThread.Create(True);
FOpenCVCameraThread.OnNotifyData := OnNotifyData;
end;
end;
destructor TocvCamera.Destroy;
begin
if Assigned(FOpenCVCameraThread) then
begin
FOpenCVCameraThread.FCapture := nil;
FOpenCVCameraThread.Terminate;
FOpenCVCameraThread.Resume;
FOpenCVCameraThread.WaitFor;
FOpenCVCameraThread.Free;
end;
if Assigned(FCapture) then
cvReleaseCapture(FCapture);
inherited;
end;
procedure TocvCamera.OnNotifyData(const IplImage: pIplImage);
begin
NotifyRecipients(IplImage);
end;
procedure TocvCamera.SetCameraCaptureSource(const Value: TocvCameraCaptureSource);
Var
isEnabled: Boolean;
begin
if FCameraCaptureSource <> Value then
begin
isEnabled := Enabled;
if Assigned(FCapture) and FEnabled then
Enabled := False;
FCameraCaptureSource := Value;
Enabled := isEnabled;
end;
end;
procedure TocvCamera.SetEnabled(const Value: Boolean);
begin
if FEnabled <> Value then
begin
if not(csDesigning in ComponentState) then
begin
if Assigned(FCapture) and FEnabled then
begin
FOpenCVCameraThread.Suspend;
FOpenCVCameraThread.FCapture := nil;
cvReleaseCapture(FCapture);
FCapture := Nil;
end;
if Value then
begin
FCapture := cvCreateCameraCapture(ocvCameraCaptureSource[FCameraCaptureSource]);
FOpenCVCameraThread.FCapture := FCapture;
FOpenCVCameraThread.Resume;
end;
end;
FEnabled := Value;
end;
end;
end.