unit U_WorkDeptList_DKPZ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, DB, cxDBData, cxTextEdit, cxCalendar,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, ADODB, DBClient, cxButtonEdit,
  cxPC, StdCtrls, ExtCtrls, ExtDlgs, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdFTP, jpeg, IniFiles, DelphiTwain, Buttons,
  MMSystem;

type
  TFrmWorkDeptList_DKPZ = class(TForm)
    Order_Main: TClientDataSet;
    ADOQueryMain: TADOQuery;
    DataSource1: TDataSource;
    ADOQueryDel: TADOQuery;
    Panel1: TPanel;
    OpenPictureDialog1: TOpenPictureDialog;
    IdFTP1: TIdFTP;
    adoqueryImage: TADOQuery;
    ADOQuery1: TADOQuery;
    SaveDialog1: TSavePictureDialog;
    ScrollBox1: TScrollBox;
    Button2: TButton;
    Button3: TButton;
    Image1: TImage;
    Panel3: TPanel;
    Image2: TImage;
    Timer1: TTimer;
    Panel2: TPanel;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    ADOQuery2: TADOQuery;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    hWndC: THandle;
    CapturingAVI: bool;
    { Private declarations }
    ClickPos: TPoint;
    SelectedSource, PicLeft, PicTop, PicWidth, PicHeight: Integer;
    procedure initGrid();
    procedure SetStatus();
    procedure CreThumb(Width, Height: Integer);
    { Private declarations }
  public
    FilePath: string;
    FileName: string;
    FTFType: string;
    pat1: string;
    pic1: string;
    fkeyNo: string;
    fFlileFlag, FYGID, IFZT, IFSXB: string;
    { Public declarations }
    MyJpeg: TJPEGImage;
    { Public declarations }
  end;

var
  FrmWorkDeptList_DKPZ: TFrmWorkDeptList_DKPZ;

implementation

uses
  U_ZDYHelp, U_RTfun, U_DataLink;

const
  WM_CAP_START = WM_USER;

const
  WM_CAP_STOP = WM_CAP_START + 68;

const
  WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;

const
  WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;

const
  WM_CAP_SAVEDIB = WM_CAP_START + 25;

const
  WM_CAP_GRAB_FRAME = WM_CAP_START + 60;

const
  WM_CAP_SEQUENCE = WM_CAP_START + 62;

const
  WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;

const
  WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63;

const
  WM_CAP_SET_OVERLAY = WM_CAP_START + 51;

const
  WM_CAP_SET_PREVIEW = WM_CAP_START + 50;

const
  WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6;

const
  WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2;

const
  WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3;

const
  WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;

const
  WM_CAP_SET_SCALE = WM_CAP_START + 53;

const
  WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;

function capCreateCaptureWindowA(lpszWindowName: PCHAR; dwStyle: longint; x: integer; y: integer; nWidth: integer; nHeight: integer; ParentWin: HWND; nId: integer): HWND; stdcall external 'AVICAP32.DLL';

{$R *.dfm}
procedure TFrmWorkDeptList_DKPZ.CreThumb(Width, Height: Integer);
var
  Bitmap: TBitmap;
  Ratio: Double;
  ARect: TRect;
  AHeight, AHeightOffset: Integer;
  AWidth, AWidthOffset: Integer;
begin
  Bitmap := TBitmap.Create;
  try
    Ratio := Image1.Picture.Graphic.Width / Image1.Picture.Graphic.Height;
    if Ratio > 1.333 then
    begin
      AHeight := Round(Width / Ratio);
      AHeightOffset := (Height - AHeight) div 2;
      AWidth := Width;
      AWidthOffset := 0;
    end
    else
    begin
      AWidth := Round(Height * Ratio);
      AWidthOffset := (Width - AWidth) div 2;
      AHeight := Height;
      AHeightOffset := 0;
    end;
    Bitmap.Width := Width;
    Bitmap.Height := Height;
    Bitmap.Canvas.Brush.Color := clBtnFace;
    Bitmap.Canvas.FillRect(Rect(0, 0, Width, Height));
    ARect := Rect(AWidthOffset, AHeightOffset, AWidth + AWidthOffset, AHeight + AHeightOffset);
    Bitmap.Canvas.StretchDraw(ARect, Image1.Picture.Graphic);
    Image2.Picture.Assign(Bitmap);
  finally
    Bitmap.Free;
  end;
end;

procedure TFrmWorkDeptList_DKPZ.SetStatus();
begin

end;

procedure TFrmWorkDeptList_DKPZ.initGrid();
begin
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('select * from OA_YG_KQ ');
    sql.add('where convert(varchar(10),DKDate,120)=convert(varchar(10),getdate(),120)');
    open;
  end;
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

procedure TFrmWorkDeptList_DKPZ.FormShow(Sender: TObject);
var
  maxno: string;
begin
  {with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('select * from OA_YG_KQ ');
    sql.Add('where YGID='''+trim(YGID.Text)+'''');
    sql.Add(' and convert(varchar(10),DKDate,120)=convert(varchar(10),getdate(),120)');
    open;
  end;
  if ADOQueryMain.IsEmpty=false then
  begin
    Application.MessageBox('员工已打卡','提示');
    YGID.Text:='';
    Exit;
  end
  else }
  if GetLSNo(ADOQueryDel, maxno, 'KQ', 'OA_YG_KQ', 3, 1) = False then
  begin
    Application.MessageBox('网络断开,请联系软件服务商', '提示', 0);
    Exit;
  end;
  fkeyNo := Trim(maxno);
  with ADOQueryDel do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from OA_YG_DangAn ');
    sql.Add('where YGID=''' + Trim(FYGID) + '''');
    open;
  end;
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('select * from OA_YG_KQ ');
    sql.Add('where 1=2');
    open;
  end;
  with ADOQueryMain do
  begin
    Append;
    FieldByName('KQID').Value := Trim(maxno);
    FieldByName('YGName').Value := Trim(ADOQueryDel.fieldbyname('YGName').AsString);   
    FieldByName('YGID').Value := Trim(FYGID);
    FieldByName('YGEName').Value := Trim(ADOQueryDel.fieldbyname('YGEName').AsString);
    FieldByName('DKDate').Value := SGetServerDateTime(ADOQuery2); //now;   
    FieldByName('KQDay').Value := 1;
    FieldByName('SXBstatus').Value := Trim(IFSXB);
    post;
  end;
  if IFZT = '1' then
    Button2.Click
  else
  begin
    ModalResult := 1;
  end;
end;

procedure TFrmWorkDeptList_DKPZ.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  //Action:=caFree;
end;

procedure TFrmWorkDeptList_DKPZ.FormDestroy(Sender: TObject);
begin
  MyJpeg.Free;
  //FrmWorkDeptList_DKPZ:=nil;
end;

procedure TFrmWorkDeptList_DKPZ.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  ClickPos.x := X;
  ClickPos.y := Y;
end;

procedure TFrmWorkDeptList_DKPZ.Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
  NewPos: TPoint;
begin
  {The left button was pressed}
  if ssLeft in Shift then
  begin
    {Calculate new position}
    NewPos.X := Image1.Left + X - ClickPos.x;
    NewPos.Y := Image1.Top + Y - ClickPos.y;
    if NewPos.x + Image1.Width < ScrollBox1.Width then
      NewPos.x := ScrollBox1.Width - Image1.Width;
    if NewPos.y + Image1.Height < ScrollBox1.Height then
      NewPos.y := ScrollBox1.Height - Image1.Height;
    if NewPos.X > 0 then
      NewPos.X := 0;
    if NewPos.Y > 0 then
      NewPos.Y := 0;

    Image1.Top := NewPos.Y;
    Image1.Left := NewPos.X;
  end {if ssLeft in Shift}
end;

procedure TFrmWorkDeptList_DKPZ.FormCreate(Sender: TObject);
begin
  MyJpeg := TJpegImage.Create;
end;

procedure TFrmWorkDeptList_DKPZ.Button2Click(Sender: TObject);
begin
  hWndC := 0;
  try
    hWndC := capCreateCaptureWindowA('My Own Capture Window', WS_CHILD or WS_VISIBLE, ScrollBox1.Left, ScrollBox1.Top, ScrollBox1.Width, ScrollBox1.Height, FrmWorkDeptList_DKPZ.Handle, 0);
    if hWndC <> 0 then
    begin
      SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
      SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
      SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
      SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
      SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);
      SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);
      //SendMessage(hWndC, WM_CAP_SEQUENCE_NOFILE, 1, 0);
      SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);
      SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);
    end
    else
    begin
      application.MessageBox('连接摄像头失败！', '错误信息', MB_ICONERROR);
    end;
  except
  end;
  application.ProcessMessages;
  //YGID.SetFocus;
end;

procedure TFrmWorkDeptList_DKPZ.Button3Click(Sender: TObject);
var
  sFieldName: string;
  MBMP: TBitmap;
  MJPG: TJpegImage;
  myStream: TADOBlobStream;
  maxNo: string;
  fNewFileName: string;
begin
  sFieldName := 'D:\抓图';
  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);
  sFieldName := sFieldName + '\' + formatdateTime('yyyyMMddhhnnss', SGetServerDateTime(ADOQuery1));
  FileName := ExtractFileName(sFieldName);
  if hWndC <> 0 then
  begin
    SendMessage(hWndC, WM_CAP_SAVEDIB, 0, longint(pchar(sFieldName + '.BMP')));
    SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
    hWndC := 0;
    application.ProcessMessages;
    try
      MBMP := TBitmap.Create;
      MJPG := TJpegImage.Create;
      MBMP.LoadFromFile(pchar(sFieldName + '.BMP'));
      MJPG.assign(MBMP);
      Image1.Picture.Bitmap.Assign(MJPG);
      application.ProcessMessages;
      MJPG.SaveToFile(pchar(sFieldName + '.JPG'));
      CreThumb(240, 180);
    finally
      MBMP.Free;
      MJPG.Free;
      if Fileexists(pchar(sFieldName + '.BMP')) then
        DeleteFile(pchar(sFieldName + '.BMP'));
      FilePath := sFieldName + '.JPG';
      FileName := ExtractFileName(FilePath);
    end;
  end;
  pat1 := 'FileName';
  pic1 := 'Filesother';
  FTFType := '员工';
  fFlileFlag := self.fFlileFlag;
  fNewFileName := formatdatetime('yyyyMMddhhnnsszzz', now()) + ExtractFileExt(FilePath);
  try
    with adoqueryImage do
    begin
      close;
      sql.Clear;
      sql.Add('select * from TP_File where WBID=' + quotedstr(trim(fkeyNo)));
      sql.Add(' and TFType=''员工''');
      open;
      if RecordCount <= 0 then
      begin
        Append;
        if GetLSNo(ADOQuery1, maxNo, 'FJ', 'TP_File', 4, 1) = False then
        begin
          Application.MessageBox('取最大号失败！', '提示', 0);
          Exit;
        end;
        fieldByName('TFID').AsString := maxNo;
        fieldByName('WBID').AsString := fkeyNO;
      end
      else
      begin
        edit;
      end;
      fieldByName(pat1).AsString := trim(fNewFileName);
      fieldByName('TFType').AsString := '员工';
      myStream := TADOBlobStream.Create(TBlobField(FieldByName(pic1)), bmWrite);
      MyJpeg.Assign(Image1.Picture.Graphic);
      MyJpeg.SaveToStream(myStream);
      myStream.Free;
      Post;
    end;
    if FilePath <> '' then
    begin
      try
        IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
        IdFTP1.Username := 'three';
        IdFTP1.Password := '641010';
        IdFTP1.Connect();
        IdFTP1.Put(FilePath, fFlileFlag + '\' + Trim(fNewFileName));
        IdFTP1.Quit;
      except
        IdFTP1.Quit;
        Application.MessageBox('上传客户图样文件失败，请检查文件服务器！', '提示', MB_ICONWARNING);
      end;
    end;
    IdFTP1.Quit;
  except
    ;
    myStream.Free;
  end;
  //Button2.Click;
end;

procedure TFrmWorkDeptList_DKPZ.Timer1Timer(Sender: TObject);
begin
  if IFZT = '1' then
    Button3.Click;
  Panel2.Visible := true;
  label5.Caption := Trim(ADOQueryDel.fieldbyname('YGName').AsString);
  label6.Caption := Trim(FormatDateTime('yyyy-MM-dd HH:MM:SS', now));
  PlaySound('DKCG.wav', 0, SND_FILENAME or SND_ASYNC);
  Sleep(1000);
  ModalResult := 1;
end;

end.

