⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 captureform.~pas

📁 停车场收费系统停车场收费系统停车场收费系统停车场收费系统
💻 ~PAS
📖 第 1 页 / 共 2 页
字号:
    VideoDc := GetDc(VideoPanel.Handle);
    //分配帧缓存
    pBuf := AllocMem(BufLength);
    pBMIInfo := AllocMem(sizeof(TBITMAPINFO)+ (255 * sizeof(TRGBQuad)));
    try
      //设定BMP的头信息
      pBMIInfo^.bmiHeader.biSize := sizeof(BITMAPINFOHEADER);
      pBMIInfo^.bmiHeader.biWidth := VideoPanel.Width;
      pBMIInfo^.bmiHeader.biHeight := VideoPanel.Height;
      pBMIInfo^.bmiHeader.biPlanes := 1;
      if (ColorSpace = All8Bit)or(ColorSpace = Limited8Bit) then
      begin
        pBMIInfo^.bmiHeader.biBitCount := 8;
        for i :=0 to 255 do
        begin
          pBMIInfo^.bmiColors[i].rgbBlue := i;
          pBMIInfo^.bmiColors[i].rgbGreen := i;
          pBMIInfo^.bmiColors[i].rgbRed := i;
          pBMIInfo^.bmiColors[i].rgbReserved := 0;
        end;
      end
      else
         pBMIInfo^.bmiHeader.biBitCount := 24;
      pBMIInfo^.bmiHeader.biCompression := BI_RGB;
      pBMIInfo^.bmiHeader.biSizeImage := 0;
      pBMIInfo^.bmiHeader.biXPelsPerMeter := 0;
      pBMIInfo^.bmiHeader.biYPelsPerMeter := 0;
      pBMIInfo^.bmiHeader.biClrUsed := 0;
      pBMIInfo^.bmiHeader.biClrImportant := 0;

      if (ColorSpace = All8Bit)or(ColorSpace = Limited8Bit) then
      begin

      end;

      //开始启动SnapOne采集
      CG300SnapOneToMem(hcg, PhysMemAdrr, BlockSize * 4096, FRAME);
      nStatus := 1;
      SetStretchBltMode(VideoDC, COLORONCOLOR);
      while isSnapOne do
      begin
        Application.ProcessMessages;
        //不断得到图像的采集状态
        CG300GetSnappingStatus(hcg, nStatus);
        if nstatus = 3 then //采集完成
        begin
          Num:=1;
          CG300ReadfromMem(hcg, Pchar(LineAddr), BlockSize * 1024 * 4,Num-1, pBuf);
        end;
        if nstatus = 1 then //采集完成
        begin
          Num:=2;
          CG300ReadfromMem(hcg, Pchar(LineAddr), BlockSize * 1024 * 4,Num-1, pBuf);
        end;
        //8位数据使用调色板
        if (ColorSpace = All8Bit)or(ColorSpace = Limited8Bit) then
        begin
          SelectPalette(VideoDC, hPal, False);
          RealizePalette(VideoDC);
        end;
        //显示图像
        StretchDIBits(VideoDC, 0, 0, VideoPanel.Width, VideoPanel.Height,
                 0, 0, pBMIInfo^.bmiHeader.biWidth, pBMIInfo^.bmiHeader.biHeight,
                 pBuf, pBMIInfo^, DIB_RGB_COLORS, SRCCOPY);
      end;
    finally
      //释放内存
      FreeMem(pBMIInfo, sizeof(TBITMAPINFO)+ (255 * sizeof(TRGBQuad)));
      FreeMem(pBuf, BufLength);
      ReleaseDC(VideoPanel.Handle, VideoDC);
    end;
  end
  else
  begin
    Application.MessageBox('调用控制面版中分派的静态内存失败,停止抓图!', '提示', MB_OK);
  end;
end;

//弹出设置窗口
procedure TfrmCapture.btnSetupClick(Sender: TObject);
begin
  if hcg < 4 then exit;

  StopCapture();
  //调整frmSetup的显示内容
  case CryOSC of
    0: frmSetup.rdgCryOSC.ItemIndex := 0;
    1: frmSetup.rdgCryOSC.ItemIndex := 1;
  end;

  case VideoStandard of
    PAL:  frmSetup.rdgVideoStandard.ItemIndex := 0;
    NTSC: frmSetup.rdgVideoStandard.ItemIndex := 1;
  end;

  case ColorSpace of
    RGB555: frmSetup.rdgColor.ItemIndex := 0;
    RGB565: frmSetup.rdgColor.ItemIndex := 1;
    RGB888: frmSetup.rdgColor.ItemIndex := 2;
    RGB8888: frmSetup.rdgColor.ItemIndex := 3;
    All8Bit: frmSetup.rdgColor.ItemIndex := 4;
    Limited8Bit: frmSetup.rdgColor.ItemIndex := 5;
  end;
  case DispMode of
    FRAME: frmSetup.rdgDispMode.ItemIndex := 0;
    FIELD: frmSetup.rdgDispMode.ItemIndex := 1;
  end;
  case Source of
    0: frmSetup.rdgSource.ItemIndex := 0;
    1: frmSetup.rdgSource.ItemIndex := 1;
    2: frmSetup.rdgSource.ItemIndex := 2;
    3: frmSetup.rdgSource.ItemIndex := 4;
    4: frmSetup.rdgSource.ItemIndex := 3;
  end;

  frmSetup.updBrightness.Position := Brightness;
  frmSetup.edtBrightness.Text := IntToStr(frmSetup.updBrightness.Position);

  frmSetup.updContrast.Position := Contrast;
  frmSetup.edtContrast.Text := IntToStr(frmSetup.updContrast.Position);

  frmSetup.updHue.Position := Hue;
  frmSetup.edtHue.Text := IntToStr(frmSetup.updHue.Position);

  frmSetup.updSaturation.Position := Saturation;
  frmSetup.edtSaturation.Text := IntToStr(frmSetup.updSaturation.Position);

  //显示frmSetup
  frmSetup.ShowModal;
  //更新设置
  case frmSetup.rdgCryOSC.ItemIndex of
    0: CryOSC := 0;
    1: CryOSC := 1;
  end;

  case frmSetup.rdgVideoStandard.ItemIndex of
    0: VideoStandard := PAL;
    1: VideoStandard := NTSC;
  end;

  case frmSetup.rdgColor.ItemIndex of
    0: ColorSpace := RGB555;
    1: ColorSpace := RGB565;
    2: ColorSpace := RGB888;
    3: ColorSpace := RGB8888;
    4: ColorSpace := All8Bit;
    5: ColorSpace := Limited8Bit;
  end;
  case frmSetup.rdgDispMode.ItemIndex of
    0: DispMode := FRAME;
    1: DispMode := FIELD;
  end;
  case frmSetup.rdgSource.ItemIndex of
    0: Source := 0;
    1: Source := 1;
    2: Source := 2;
    4: Source := 3;
    3: Source := 4;
  end;

  Brightness := frmSetup.updBrightness.Position;
  Contrast := frmSetup.updContrast.Position;
  Hue := frmSetup.updHue.Position;
  Saturation := frmSetup.updSaturation.Position;

  CG300SelectCryOSC(hcg, CryOSC);
  CG300SetVideoStandard(hcg, VideoStandard);
  CG300SetColorSpace(hcg, ColorSpace);
  CG300SetDispMode(hcg, DispMode);
  CG300SetADParam(hcg, AD_SOURCE, Source);
  CG300SetADParam(hcg, AD_BRIGHTNESS, Brightness);
  CG300SetADParam(hcg, AD_CONTRAST, Contrast);
  CG300SetADParam(hcg, AD_HUE, Hue);
  CG300SetADParam(hcg, AD_SATURATION, Saturation);
  if VideoStandard = PAL then
     CG300SetInpVideoWindow(hcg, 0, 0, 768, 576)
  else
     CG300SetInpVideoWindow(hcg, 0, 0, 640, 480);
  AdjustWndPos;
end;

//采集图像到内存
procedure TfrmCapture.btnPlayBackClick(Sender: TObject);
var
  pBuf: Pchar;
  BufLength: Dword;
  BlockSize, PhysMemAdrr, MemHandle, LineAddr: Dword;
  pBMIInfo: PBITMAPINFO;
  VideoDC: HDC;
  //采集到内存中的图像数
  Sum: DWORD;
  //控制回放次数
  i,j : integer;
begin
  if hcg < 4 then exit;
  //停止播放
  StopCapture();
  //重新设置窗口
  AdjustWndPos();

  //使用控制面版中分派的静态内存保存图象
  if not(StaticMemAlloc(BlockSize, PhysMemAdrr, MemHandle, LineAddr)) then
  begin
    ShowMessage('访问静态保留内存失败!');
    exit;
  end;

  //动态分配Buf的大小是多少字节
  Case ColorSpace of
    RGB888 :
      BufLength := 768 * 576 * 3;
    RGB565 :
      BufLength := 768 * 576 * 3;
    RGB555 :
      BufLength := 768 * 576 * 3;
    RGB8888 :
      BufLength := 768 * 576 * 4;
    All8Bit :
      BufLength := 768 * 576 * 1;
    Limited8Bit :
      BufLength := 768 * 576 * 1;
  else
    BufLength := 768 * 576 * 4;
  end;

  //得到显示DC
  VideoDc := GetDc(VideoPanel.Handle);
  //分配帧缓存
  pBuf := AllocMem(BufLength);
  pBMIInfo := AllocMem(sizeof(TBITMAPINFO)+ (255 * sizeof(TRGBQuad)));
  try
    //设定BMP的头信息
    pBMIInfo^.bmiHeader.biSize := sizeof(BITMAPINFOHEADER);
    pBMIInfo^.bmiHeader.biWidth := VideoPanel.Width;
    pBMIInfo^.bmiHeader.biHeight := VideoPanel.Height;
    pBMIInfo^.bmiHeader.biPlanes := 1;
    if (ColorSpace = All8Bit)or(ColorSpace = Limited8Bit) then
    begin
      pBMIInfo^.bmiHeader.biBitCount := 8;
      for i :=0 to 255 do
      begin
        pBMIInfo^.bmiColors[i].rgbBlue := i;
        pBMIInfo^.bmiColors[i].rgbGreen := i;
        pBMIInfo^.bmiColors[i].rgbRed := i;
        pBMIInfo^.bmiColors[i].rgbReserved := 0;
      end;
    end
    else
         pBMIInfo^.bmiHeader.biBitCount := 24;
    pBMIInfo^.bmiHeader.biCompression := BI_RGB;
    pBMIInfo^.bmiHeader.biSizeImage := 0;
    pBMIInfo^.bmiHeader.biXPelsPerMeter := 0;
    pBMIInfo^.bmiHeader.biYPelsPerMeter := 0;
    pBMIInfo^.bmiHeader.biClrUsed := 0;
    pBMIInfo^.bmiHeader.biClrImportant := 0;

    //控制面版中分派的静态内存可以保存当前尺寸的采集图象的总数SUM
    case ColorSpace of
      RGB888 : Sum := (BlockSize * 1024 * 4) div Dword(VideoPanel.Width * VideoPanel.Height * 3);
      RGB565 : Sum := (BlockSize * 1024 * 4) div Dword(VideoPanel.Width * VideoPanel.Height * 2);
      RGB555 : Sum := (BlockSize * 1024 * 4) div Dword(VideoPanel.Width * VideoPanel.Height * 2);
      RGB8888 : Sum := (BlockSize * 1024 * 4) div Dword(VideoPanel.Width * VideoPanel.Height * 4);
      All8Bit : Sum := (BlockSize * 1024 * 4) div Dword(VideoPanel.Width * VideoPanel.Height);
      Limited8Bit :Sum := (BlockSize * 1024 * 4) div Dword(VideoPanel.Width * VideoPanel.Height);
    else
      Sum := 0;
    end;

    //采集到内存中
    Sum :=CG300CaptureToMem(hcg, PhysMemAdrr, BlockSize * 1024 * 4, 0, FRAME, Sum);
    //提示一共采集到内存中的图象数
    Application.MessageBox(pchar('共有' + IntToStr(Sum) + '帧图象采集到主机内存,开始从内存回放!'),
        '提示', MB_OK);

    // PlayBack播放十次
    for j := 1 to 10 do
    begin
      for i := 1 to Sum do
      begin
        //从内存中读取一图
        CG300ReadfromMem(hcg, Pchar(LineAddr), BlockSize * 1024 * 4, i - 1, pBuf);
        //8位数据使用调色板
        if (ColorSpace = All8Bit)or(ColorSpace = Limited8Bit) then
        begin
          SelectPalette(VideoDC, hPal, False);
          RealizePalette(VideoDC);
        end;
        //调用win32的API函数将Buf中的数据以图象形式显示在VideoPanel上
        SetStretchBltMode(VideoDC, COLORONCOLOR);
        StretchDIBits(VideoDC, 0, 0, VideoPanel.Width, VideoPanel.Height,
                0, 0, pBMIInfo^.bmiHeader.biWidth, pBMIInfo^.bmiHeader.biHeight,
                pBuf, pBMIInfo^, DIB_RGB_COLORS, SRCCOPY);
      end;
    end;
  finally
    //释放内存
    FreeMem(pBMIInfo, sizeof(TBITMAPINFO)+ (255 * sizeof(TRGBQuad)));
    FreeMem(pBuf, BufLength);
    ReleaseDC(VideoPanel.Handle, VideoDC);
  end;
end;

procedure TfrmCapture.SetPallete(var hPal: HPalette);
var
  //循环控制
  i: Integer;
  //程序使用的逻辑调色板
  lpPal: PLOGPALETTE;
  PaletteSize: Word;
begin
  lpPal := AllocMem(sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
  try
    PaletteSize := 256;
    lpPal^.palVersion := $300;
    lpPal^.palNumEntries := PaletteSize;
    for i :=0 to lpPal^.palNumEntries-1 do
    begin
      lpPal^.palPalEntry[i].peRed := i;
      lpPal^.palPalEntry[i].peGreen := i;
      lpPal^.palPalEntry[i].peBlue := i;
      lpPal^.palPalEntry[i].peFlags := PC_NOCOLLAPSE;
      hPal := CreatePalette(lpPal^);
    end;
  finally
    FreeMem(lpPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
  end;
end;

procedure TfrmCapture.UsePallete(hForm: HWND; hPal: HPalette);
var
  DC: HDC;
begin
  if hPal = 0 then exit;
  DC := GetDC(hForm);
  try
    SelectPalette(DC, hPal, False);
    RealizePalette(DC);
  finally
    ReleaseDC(hForm, DC);
  end;
end;

procedure TfrmCapture.DelPallete(hPal: HPalette);
begin
  if hPal = 0 then exit;
  DeleteObject(hPal);
end;

procedure TfrmCapture.GetDeskColor(var DeskColor: Word);
var
  h: HWND;
  DC: HDC;
begin
  h := GetDesktopWindow();
  DC := GetDC(h);
  DeskColor := GetDeviceCaps(DC, BITSPIXEL);
  ReleaseDC(h, DC);
end;

procedure TfrmCapture.btnSaveFromMemClick(Sender: TObject);
var
  pBuf: Pchar;
  BufLength: Dword;
  BlockSize, PhysMemAdrr, MemHandle, LineAddr: Dword;
  pBMIInfo: PBITMAPINFO;
  //采集到内存中的图像数
  Sum: DWORD;
  //控制回放次数
  i: integer;
  Bmp: TBitmap;
begin
  if hcg < 4 then exit;
  //停止播放
  StopCapture();
  //重新设置窗口
  AdjustWndPos();

  //使用控制面版中分派的静态内存保存图象
  if not(StaticMemAlloc(BlockSize, PhysMemAdrr, MemHandle, LineAddr)) then
  begin
    ShowMessage('访问静态保留内存失败!');
    exit;
  end;

  //动态分配Buf的大小是多少字节
  Case ColorSpace of
    RGB888 :
      BufLength := 768 * 576 * 3;
    RGB565 :
      BufLength := 768 * 576 * 3;
    RGB555 :
      BufLength := 768 * 576 * 3;
    RGB8888 :
      BufLength := 768 * 576 * 4;
    All8Bit :
      BufLength := 768 * 576 * 1;
    Limited8Bit :
      BufLength := 768 * 576 * 1;
  else
    BufLength := 768 * 576 * 4;
  end;

  //分配帧缓存
  pBuf := AllocMem(BufLength);
  pBMIInfo := AllocMem(sizeof(TBITMAPINFO)+ (255 * sizeof(TRGBQuad)));
  Bmp := TBitmap.Create;
  try
    bmp.Width:= VideoPanel.Width;
    bmp.Height := VideoPanel.Height;
    //设定BMP的头信息
    pBMIInfo^.bmiHeader.biSize := sizeof(BITMAPINFOHEADER);
    pBMIInfo^.bmiHeader.biWidth := VideoPanel.Width;
    pBMIInfo^.bmiHeader.biHeight := VideoPanel.Height;
    pBMIInfo^.bmiHeader.biPlanes := 1;
    if (ColorSpace = All8Bit)or(ColorSpace = Limited8Bit) then
    begin
      pBMIInfo^.bmiHeader.biBitCount := 8;
      for i :=0 to 255 do
      begin
        pBMIInfo^.bmiColors[i].rgbBlue := i;
        pBMIInfo^.bmiColors[i].rgbGreen := i;
        pBMIInfo^.bmiColors[i].rgbRed := i;
        pBMIInfo^.bmiColors[i].rgbReserved := 0;
      end;
      Bmp.PixelFormat := pf8bit;
      Bmp.Palette := hPal;
    end
    else
    begin
      pBMIInfo^.bmiHeader.biBitCount := 24;
      Bmp.PixelFormat := pf24bit;
    end;
    pBMIInfo^.bmiHeader.biCompression := BI_RGB;
    pBMIInfo^.bmiHeader.biSizeImage := 0;
    pBMIInfo^.bmiHeader.biXPelsPerMeter := 0;
    pBMIInfo^.bmiHeader.biYPelsPerMeter := 0;
    pBMIInfo^.bmiHeader.biClrUsed := 0;
    pBMIInfo^.bmiHeader.biClrImportant := 0;


    //控制面版中分派的静态内存可以保存当前尺寸的采集图象的总数SUM
    case ColorSpace of
      RGB888 : Sum := (BlockSize * 1024 * 4) div Dword(VideoPanel.Width * VideoPanel.Height * 3);
      RGB565 : Sum := (BlockSize * 1024 * 4) div Dword(VideoPanel.Width * VideoPanel.Height * 2);
      RGB555 : Sum := (BlockSize * 1024 * 4) div Dword(VideoPanel.Width * VideoPanel.Height * 2);
      RGB8888 : Sum := (BlockSize * 1024 * 4) div Dword(VideoPanel.Width * VideoPanel.Height * 4);
      All8Bit : Sum := (BlockSize * 1024 * 4) div Dword(VideoPanel.Width * VideoPanel.Height);
      Limited8Bit :Sum := (BlockSize * 1024 * 4) div Dword(VideoPanel.Width * VideoPanel.Height);
    else
      Sum := 0;
    end;

    //采集到内存中
    Sum :=CG300CaptureToMem(hcg, PhysMemAdrr, BlockSize * 1024 * 4, 0, FRAME, Sum);

    // PlayBack播放十次
    for i := 1 to Sum do
    begin
      //从内存中读取一图
      CG300ReadfromMem(hcg, Pchar(LineAddr), BlockSize * 1024 * 4, i - 1, pBuf);
      //调用win32的API函数将Buf中的数据以图象形式显示在VideoPanel上
      SetStretchBltMode(Bmp.Canvas.Handle, COLORONCOLOR);
      SetDIBitsToDevice(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height,
                0, 0, 0, pBMIInfo^.bmiHeader.biHeight,
                pBuf, pBMIInfo^, DIB_RGB_COLORS);
      Bmp.SaveToFile(IntToStr(i)+'.bmp');
        //提示一共采集到内存中的图象数
    end;
    Application.MessageBox(pchar('共有' + IntToStr(Sum) + '帧图象保存为BMP文件!'),
        '提示', MB_OK);
  finally
    //释放内存
    Bmp.Free;
    FreeMem(pBMIInfo, sizeof(TBITMAPINFO)+ (255 * sizeof(TRGBQuad)));
    FreeMem(pBuf, BufLength);
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -