Delphi-OpenCV/samples/opencv_classes/clsVideoCapture/clsVideoCapture.dpr

101 lines
3.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.
// *******************************************************************
program clsVideoCapture;
{$APPTYPE CONSOLE}
{$POINTERMATH ON}
{$R *.res}
uses
System.SysUtils,
ocv.mat,
ocv.highgui,
ocv.highgui_c;
Const
VK_ESCAPE = 27;
DESIRED_CAMERA_WIDTH: Integer = 320;
DESIRED_CAMERA_HEIGHT: Integer = 200;
windowName = 'Test VideoCapture'; // Name shown in the GUI window.
// Get access to the webcam.
procedure initWebcam(videoCapture: TccvVideoCapture; cameraNumber: Integer = CV_CAP_ANY);
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: TccvVideoCapture;
cameraFrame: TccvMat = nil;
begin
try
camera := TccvVideoCapture.Create;
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!
camera.setValue(CV_CAP_PROP_FRAME_WIDTH, DESIRED_CAMERA_WIDTH);
camera.setValue(CV_CAP_PROP_FRAME_HEIGHT, DESIRED_CAMERA_HEIGHT);
// 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.
if camera.read(cameraFrame) then
begin
if Assigned(cameraFrame) and (cameraFrame.empty()) then
begin
Writeln('ERROR: Couldn''t grab the next camera frame.');
Halt(1);
end;
if Assigned(cameraFrame) then
imshow(windowName, cameraFrame);
end;
// IMPORTANT: Wait for atleast 20 milliseconds, so that the image can be displayed on the screen!
if (waitKey(20) = VK_ESCAPE) then // Escape Key
break; // Quit the program!
end;
camera.Free;
cvDestroyAllWindows;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.