VideoDisp.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:5k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit VideoDisp;
  2. interface
  3.  uses Windows, Messages, SysUtils, Classes, Graphics, Controls,stdctrls,
  4.   ExtCtrls,vfw,mmsystem;
  5. //////////////////////////////////////////////////////////////////////////////////
  6. // Video  Display
  7. type ENoHDD  = class(Exception);
  8. type
  9.   TVideoDisp = class(TCustomControl)
  10.    private
  11.     Hdd:HDrawDib;   // Handle of the DrawDibDC
  12.     fBitmapInfoHeader:TBitmapinfoHeader; // Info Header of Frames
  13.     fstreaming:Boolean; // True when Video Stream is running
  14.     frate:integer; // Streaming Rate
  15.     fscale:boolean;   // Scale Bitmap to window
  16.     fprop:boolean;
  17.     fBiWidth:integer; // Height and Width for DrawDibDraw
  18.     fbiHeight:integer;
  19.     procedure SetInfoHeader(Header:TBitmapInfoHeader);  // Setting BitmapInfo Header
  20.     procedure SetStreaming(streaming:Boolean);    // Streaming On / Off
  21.     procedure SetRate(rate:integer);            // Rate of Streaming
  22.     procedure SetSize(var Msg:TMessage); message wm_size; // Handling Sizing
  23.     procedure calcSize(w,h:integer);  // calc size of Output
  24.     procedure SetScale(scaling:Boolean);  // Set Scaling
  25.     procedure SetProp(prop:Boolean);
  26.   public
  27.     constructor Create(AOwner: TComponent); override;
  28.     destructor destroy; override;
  29.     procedure DrawStream(Frame:Pointer; KeyFrame:Boolean);
  30.     procedure DrawStreamEx(X,Y,W,H: Integer; Frame:Pointer; Info:TBitmapInfoHeader; KeyFrame:Boolean);
  31.     property  BitMapInfoHeader:TBitmapInfoHeader read fbitmapInfoHeader write SetInfoHeader;
  32.     property  Streaming:boolean read fstreaming write SetStreaming;
  33.   published
  34.     property ScaleToWindow:boolean read FScale write setScale;
  35.     property StreamRate:integer read frate write setRate;
  36.     property ScaleProportional:boolean read fprop  write SetProp;
  37.     property align;
  38.     property color;
  39.     property visible;
  40.     property OnMouseMove;
  41.     property OnMouseUp;
  42.     property OnMouseDown;
  43.     property OnClick;
  44.     Property OnDblClick;
  45.  end;
  46.  Procedure Register;
  47. implementation
  48. constructor TVideoDisp.Create(AOwner: TComponent);
  49. var e:Exception;
  50.  begin
  51.    inherited Create(aOwner);
  52.    Width:= 100;
  53.    height:=75;
  54.    color := clblack;
  55.    fstreaming:= false;
  56.    frate:= 66667;
  57.    hdd:=DrawDibOpen;
  58.    fbitmapinfoheader.biWidth := 100;
  59.    fbitmapinfoheader.biHeight:= 100;
  60.    fbitmapInfoHeader.biSize:=0;
  61.    if hdd = 0 then
  62.     begin
  63.      e:=ENoHDD.Create('Can not Create HDRAWDIB');
  64.      raise e;
  65.     end;
  66.   end;
  67. destructor TVideoDisp.Destroy;
  68. begin
  69.  DrawDibClose(hdd);
  70.  inherited Destroy;
  71. end;
  72. procedure TVideoDisp.SetInfoHeader(Header:TBitmapInfoHeader);
  73. begin
  74.  fBitmapInfoHeader:= header;
  75.  calcSize(width,height);
  76. end;
  77. // Draw a new Picture of the Frame
  78. procedure TVideoDisp.DrawStream(Frame:Pointer;KeyFrame:Boolean);
  79. var Flags:word;
  80.   //  e:Exception;
  81. begin
  82.  if bitmapinfoHeader.bisize = 0 then exit;
  83.  flags := DDF_SAME_HDC or DDF_SAME_DRAW;
  84.  if not Keyframe then Flags:= flags or DDF_NOTKEYFRAME  ;
  85.  DrawDibDraw(hdd,canvas.handle,0,0,fbiwidth,fbiheight,@fBitmapInfoHeader,
  86.              frame,0,0,fBitmapInfoHeader.biWidth,fbitmapInfoHeader.biheight,flags);
  87. end;
  88. // Set Streaming Rate
  89. procedure TVideoDisp.SetRate(rate:integer);
  90. begin
  91.   if fstreaming then DrawDibStop(hdd);
  92.   frate := rate;
  93.   if Streaming then DrawDibStart(hdd,frate);
  94. end;
  95. // Toggeling Streaming mode
  96.  procedure TVideoDisp.SetStreaming(streaming:boolean);
  97.  begin
  98.    if streaming = fstreaming then exit;
  99.    if fstreaming then
  100.      DrawDibStop(hdd)
  101.    else
  102.      DrawDibStart(hdd,frate);
  103.     fstreaming := streaming;
  104.  end;
  105. procedure TVideoDisp.SetSize(var Msg:TMessage);
  106. begin
  107.    calcsize(LOWORD(msg.lParam),HIWORD(msg.lParam));
  108. end;
  109. procedure TVideoDisp.calcSize(w,h:integer);
  110.  var f,cf:double;
  111. begin
  112.  if fscale then
  113.    begin
  114.      if fprop then
  115.         begin
  116.         f:= W/h;
  117.         cf:= fBitmapInfoHeader.biWidth/fbitmapInfoHeader.biHeight;
  118.         if cf  <  f then
  119.          begin
  120.           fbiWidth:= round(h*cf);
  121.           fbiHeight:= h;
  122.          end
  123.         else
  124.           begin
  125.           fbiWidth:= w;
  126.           fbiHeight:= round(w*1/cf);
  127.          end
  128.    end
  129.    else
  130.     begin
  131.      fbiheight:= h;
  132.      fbiwidth:=  w;
  133.     end
  134.   end
  135.    else
  136.      begin
  137.       fbiheight:=fbitmapInfoHeader.biHeight;
  138.       fbiwidth:= fbitmapInfoHeader.biWidth;
  139.     end;
  140.   if fbitmapInfoHeader.biSize <> 0 then
  141.    DrawDibBegin(hdd,canvas.handle,fbiwidth,fbiheight,@fBitmapInfoHeader,
  142.             fBitmapInfoHeader.biWidth,fbitmapInfoHeader.biheight,0);
  143. end;
  144. procedure TVideoDisp.SetScale(scaling:Boolean);
  145.  begin
  146.   if scaling = fscale then exit;
  147.   fscale:= scaling;
  148.   calcSize(width,height);
  149. end;
  150. procedure TVideoDisp.SetProp(prop:Boolean);
  151.  begin
  152.   if fprop = prop then exit;
  153.   fprop:=prop;
  154.   calcSize(width,height);
  155. end;
  156. procedure TVideoDisp.DrawStreamEx(X, Y, W, H: Integer; Frame: Pointer; Info:TBitmapInfoHeader; KeyFrame: Boolean);
  157. var Flags:word;
  158.   //  e:Exception;
  159. begin
  160.  if bitmapinfoHeader.bisize = 0 then exit;
  161.  flags := DDF_SAME_HDC;// or DDF_SAME_DRAW;
  162.  if not Keyframe then Flags:= flags or DDF_NOTKEYFRAME  ;
  163.  DrawDibDraw(hdd,canvas.handle,X,Y,W,H,@Info,
  164.              frame,0,0,Info.biWidth,Info.biHeight,flags);
  165. end;
  166. procedure Register;
  167. begin
  168.   RegisterComponents( 'Video', [TVideoDisp]);
  169. end;
  170. end.
  171.