2013-05-17 08:10:02 +02:00
|
|
|
(* /*****************************************************************
|
|
|
|
// 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.
|
|
|
|
******************************************************************* *)
|
|
|
|
|
|
|
|
// JCL_DEBUG_EXPERT_GENERATEJDBG OFF
|
|
|
|
// JCL_DEBUG_EXPERT_INSERTJDBG OFF
|
|
|
|
// JCL_DEBUG_EXPERT_DELETEMAPFILE OFF
|
|
|
|
program Class_VideoCapture;
|
|
|
|
|
|
|
|
{$APPTYPE CONSOLE}
|
|
|
|
{$POINTERMATH ON}
|
|
|
|
{$R *.res}
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
2013-05-26 10:50:18 +02:00
|
|
|
highgui,
|
|
|
|
highgui_c,
|
|
|
|
Mat;
|
2013-05-17 08:10:02 +02:00
|
|
|
|
|
|
|
Const
|
|
|
|
VK_ESCAPE = 27;
|
|
|
|
|
2013-06-07 20:09:41 +02:00
|
|
|
DESIRED_CAMERA_WIDTH: Integer = 320;
|
|
|
|
DESIRED_CAMERA_HEIGHT: Integer = 200;
|
2013-05-17 08:10:02 +02:00
|
|
|
|
|
|
|
windowName = 'Test VideoCapture'; // Name shown in the GUI window.
|
|
|
|
|
|
|
|
// Get access to the webcam.
|
|
|
|
procedure initWebcam(videoCapture: IVideoCapture; cameraNumber: Integer);
|
|
|
|
begin
|
|
|
|
// Get access to the default camera.
|
|
|
|
// Surround the OpenCV call by a try/catch block so we can give a useful error message!
|
|
|
|
videoCapture.open(cameraNumber);
|
|
|
|
if not videoCapture.isOpened() then
|
|
|
|
begin
|
|
|
|
Writeln('ERROR: Could not access the camera!');
|
|
|
|
Halt(1);
|
|
|
|
end;
|
|
|
|
Writeln('Loaded camera ', cameraNumber);
|
|
|
|
end;
|
|
|
|
|
|
|
|
Var
|
|
|
|
cameraNumber: Integer = CV_CAP_ANY;
|
|
|
|
camera: IVideoCapture;
|
2013-05-21 01:35:54 +02:00
|
|
|
cameraFrame: IMat = nil;
|
2013-05-17 08:10:02 +02:00
|
|
|
|
|
|
|
begin
|
|
|
|
try
|
|
|
|
camera := CreateVideoCapture;
|
|
|
|
initWebcam(camera, cameraNumber);
|
|
|
|
// Try to set the camera resolution. Note that this only works for some cameras on
|
|
|
|
// some computers and only for some drivers, so don't rely on it to work!
|
2013-05-21 01:35:54 +02:00
|
|
|
camera.setValue(CV_CAP_PROP_FRAME_WIDTH, DESIRED_CAMERA_WIDTH);
|
|
|
|
camera.setValue(CV_CAP_PROP_FRAME_HEIGHT, DESIRED_CAMERA_HEIGHT);
|
2013-05-17 08:10:02 +02:00
|
|
|
// Create a GUI window for display on the screen.
|
|
|
|
namedWindow(windowName); // Resizable window, might not work on Windows.
|
|
|
|
// Run forever, until the user hits Escape to "break" out of this loop.
|
|
|
|
while true do
|
|
|
|
begin
|
|
|
|
// Grab the next camera frame. Note that you can't modify camera frames.
|
2013-05-21 01:35:54 +02:00
|
|
|
camera.read(cameraFrame);
|
2013-06-07 20:09:41 +02:00
|
|
|
if Assigned(cameraFrame) then
|
2013-05-21 01:35:54 +02:00
|
|
|
begin
|
2013-06-07 20:09:41 +02:00
|
|
|
if (cameraFrame.empty()) then
|
|
|
|
begin
|
|
|
|
Writeln('ERROR: Couldn''t grab the next camera frame.');
|
|
|
|
Halt(1);
|
|
|
|
end;
|
|
|
|
imshow(windowName, cameraFrame);
|
2013-05-21 01:35:54 +02:00
|
|
|
end;
|
2013-05-17 08:10:02 +02:00
|
|
|
// IMPORTANT: Wait for atleast 20 milliseconds, so that the image can be displayed on the screen!
|
2013-05-21 01:35:54 +02:00
|
|
|
if (waitKey(20) = VK_ESCAPE) then // Escape Key
|
2013-05-17 08:10:02 +02:00
|
|
|
break; // Quit the program!
|
|
|
|
end;
|
|
|
|
|
|
|
|
camera.release;
|
|
|
|
|
|
|
|
cvDestroyAllWindows;
|
|
|
|
except
|
|
|
|
on E: Exception do
|
|
|
|
Writeln(E.ClassName, ': ', E.Message);
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|