delphi - Take screenshot in webcam using VFrames in Console Application -
i'm doing program take picture of webcam using delphi xe2 , vframes achieve this, problem have figured out, in graphic application works fine, when use unit in console application, returns me error saying
first chance exception @ $76b6b727. exception class eaccessviolation message 'access violation @ address 004a271b in module 'console.exe'. read of address 00000260'. process console.exe (3676)
my unit :
unit webcam; interface uses sysutils, windows, vcl.imaging.jpeg, vcl.graphics, vsample, vframes, classes; type twebcam = class private procedure newvideoframeevent(sender: tobject; width, height: integer; dataptr: pointer); public constructor create; destructor destroy; override; procedure capture_webcam(take_name: string); end; var web_image: tvideoimage; name_screen: string; implementation constructor twebcam.create; begin inherited create; end; destructor twebcam.destroy; begin inherited destroy; end; procedure twebcam.newvideoframeevent(sender: tobject; width, height: integer; dataptr: pointer); var bitmap: tbitmap; name: string; begin name := name_screen; if (fileexists(name)) begin deletefile(pchar(name)); end; bitmap := tbitmap.create; bitmap.pixelformat := pf24bit; web_image.getbitmap(bitmap); bitmap.savetofile(name); bitmap.free; web_image.videostop; web_image.free; end; procedure twebcam.capture_webcam(take_name: string); var list_cams: tstringlist; begin web_image := tvideoimage.create(); list_cams := tstringlist.create; web_image.getlistofdevices(list_cams); if not(list_cams.count = 0) begin name_screen := take_name; web_image.videostart(list_cams[0]); end; list_cams.free; web_image.onnewvideoframe := newvideoframeevent; end; end.
console :
program console; {$apptype console} {$r *.res} uses system.sysutils, webcam; var webcamz: twebcam; begin try webcamz := twebcam.create(); webcamz.capture_webcam('test.jpg'); webcamz.free(); except on e: exception writeln(e.classname, ': ', e.message); end; end.
what should ?
the relevent source code vframes
unit available on github:
https://github.com/heise/grblize/blob/edge/vframes.pas
https://github.com/heise/grblize/blob/edge/vsample.pas
the tvideoimage.videostart()
method has dependancy on application.mainform.handle
. console application not have mainform
default, alone crash code in console application unless create mainform
(which defeats purpose of making console app).
aside that, tvideoimage
has dependancy on message loop, creates hidden window receive video notifications used fire onnewvideoframe
event. console application not have message loop. , if did, freeing tvideoimage
object before event fire anyway, capture_webcam()
code not waiting event fire before exiting.
also, tvideosample
class (which tvideoimage
uses internally) uses directshow api capture images webcam's video stream. directshow com-based api. console application not initializing com before using tvideoimage
. alone cause getlistofdevices()
fail , return blank list. , if attempted ignore , provide device name anyway, videostart()
still crash when tries access com object tvideosample
not able create during construction.
Comments
Post a Comment