摄像头获取1寸照片

thisisxutao 2008-09-18 06:49:13
怎样改变原有的尺寸320 X 240 ,达到1寸照片的效果,我用代码调整尺寸,但照片变形,请帮我看看程序或用其他的方法来实现,感谢!
unit GetPhotoUnit;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls,jpeg,AviCap, OleCtrls;

type
TGetPhotoForm = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
Shape2: TShape;
Image1: TImage;
Shape1: TShape;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
private
{ Private declarations }
procedure BmpToJpg;
Procedure ChangePicSiz(SourceFileName,DescFileName:string);
public
{ Public declarations }
end;
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 'dll\AVICAP32.DLL';
var
GetPhotoForm: TGetPhotoForm;
hWndC : THandle;
implementation

{$R *.dfm}
uses ProMainUnit;
procedure TGetPhotoForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if hWndC <> 0 then begin
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
hWndC := 0;
end;
Action:=cafree;
end;

procedure TGetPhotoForm.BitBtn1Click(Sender: TObject);
begin
if hWndC <> 0 then begin
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
hWndC := 0;
end;
//
hWndC := capCreateCaptureWindowA('GetPhotoForm',WS_CHILD or WS_VISIBLE ,Panel1.Left,Panel1.Top,100,126,GetPhotoForm.Handle,0);
hWndC := capCreateCaptureWindowA('GetPhotoForm',WS_CHILD or WS_VISIBLE ,Panel1.Left,Panel1.Top,100,126,GetPhotoForm.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_SET_OVERLAY, 1, 0);
SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);
end;
end;

procedure TGetPhotoForm.BitBtn2Click(Sender: TObject);
var Dc: HDC;
MyCanvas: TCanvas;
MyRect: TRect;
begin
Dc := GetWindowDC(0);
MyCanvas := TCanvas.Create;
SendMessage(hWndC,WM_CAP_SAVEDIB,0,longint(pchar(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.bmp')));
try
MyCanvas.Handle := Dc;
MyRect:=Rect(0, 0,Image1.Width, Image1.Height);
Image1.Canvas.CopyRect(MyRect, MyCanvas, MyRect);
finally
MyCanvas.Handle := 0;
MyCanvas.Free;
ReleaseDC(0, Dc);
end;
BmpToJpg;
Image1.Picture.LoadFromFile(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg');
showmessage('照相成功!');
ProMainForm.Image1.Picture.LoadFromFile(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg');

close;
end;

procedure TGetPhotoForm.BmpToJpg;
var
Jpeg : TJPEGImage;
Bmp : TBitmap;
begin
Bmp := TBitmap.Create;
try
Bmp.LoadFromFile(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.bmp');
Jpeg := TJPEGImage.Create;
try
Jpeg.Assign(bmp);
jpeg.CompressionQuality :=30;
Jpeg.Compress;
//保存图片
Jpeg.SaveToFile(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg');
ChangePicSiz(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg',ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg')
finally
Jpeg.Free;
end;
finally
Bmp.Free;
end;

end;

procedure TGetPhotoForm.BitBtn3Click(Sender: TObject);
begin

close;
end;

procedure TGetPhotoForm.ChangePicSiz(SourceFileName, DescFileName: string);
var
jpg:TJPEGImage;
bmp:TBitmap;
SourceJpg:TJPEGImage;
FWidth,FHeight:Integer;
begin
FWidth := 100;
FHeight := 126;
bmp := TBitmap.Create;
SourceJpg := TJPEGImage.Create;
Jpg:= TJPEGImage.Create;
try
//读取源文件
SourceJpg.LoadFromFile(SourceFileName);
//缩小
bmp.Width := FWidth;
bmp.Height := FHeight;
bmp.PixelFormat := pf24bit;
bmp.Canvas.StretchDraw(Rect(0, 0, FWidth, FHeight), SourceJpg);
//保存
jpg.Assign(bmp);
jpg.SaveToFile(DescFileName);
finally
bmp.Free;
jpg.Free;
SourceJpg.Free;
end;

end;

end.
...全文
586 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
bob008 2008-09-19
  • 打赏
  • 举报
回复
你可以随便搞张图片..然后把想要的地方做上红色方框标记,然后把目标的大小搞个图片贴上来么
不得闲 2008-09-19
  • 打赏
  • 举报
回复
看楼主貌似是用VFW处理的,我写了一个这样的视频控件!如果有需要我就放出来吧。
不过VFW现在过时了!以前公司的视频用我这个的时候,有些摄相头的图象搞不出来
所以,建议楼主使用DirectShow吧!
不得闲 2008-09-19
  • 打赏
  • 举报
回复
不知道楼主使用的是什么进行视频捕获了!如果用VFW的话,可以会有一个回调函数返回每一贞的信息,这个时候
可以使用一个Image来把图片放到Image上,然后在在Image上调整就简单了,我以前就是这样取尺寸的。
手指风 2008-09-19
  • 打赏
  • 举报
回复
不懂,来装懂,帮顶.
mdejtod 2008-09-19
  • 打赏
  • 举报
回复
mdejtod 2008-09-19
  • 打赏
  • 举报
回复
//原图

//压缩图
http://tg1a128.mail.163.com/netfolder/servlet/nfapp/GetFile?sid=sCfrQiGGJvuKBjmRdKGGQFbVRKhOvles&mid=1tbi7h90METM3Rl2uAAAs3
mdejtod 2008-09-19
  • 打赏
  • 举报
回复
try
FGpBitmap := TGpBitmap.Create(FGpBitmapName);//FGpBitmapName = 文件名
self.Color := clBlack;
pg := TGPGraphics.Create(form.Canvas.Handle);
rc := GpRect(3,3,103,70);
pg.DrawImage(FGpBitmap,rc, 3, 3, FGpBitmap.Width, FGpBitmap.Height, utPixel);
finally
FGpBitmap.Free;
pg.Free;
end;
nhconch 2008-09-19
  • 打赏
  • 举报
回复
320 X 240是4:3,1寸照片不是这比例,必须裁切过再缩放
mdejtod 2008-09-19
  • 打赏
  • 举报
回复
如果是将摄像头拍的照片压缩显示的话,很方便,用 TGpImage 和TGpGraphics 就可以做到,而且效果非常好,不会有损
yangkunjie 2008-09-19
  • 打赏
  • 举报
回复
你的原始尺寸stretch到1英寸的时候比例不一样,剪裁成1英寸比例后然后再stretchDraw
fangsp 2008-09-19
  • 打赏
  • 举报
回复
学习 楼主搞这个也挺厉害的啊
无条件为你 2008-09-19
  • 打赏
  • 举报
回复
按比例调整尺寸,照片不会变形。
jacknes009 2008-09-19
  • 打赏
  • 举报
回复
Stretch进行调整不行嘛
shuihan20e 2008-09-19
  • 打赏
  • 举报
回复
按像素将图取出来
gwhdaxia 2008-09-18
  • 打赏
  • 举报
回复
学习一下,我同事在做图象方面的可惜我不会,只能帮顶了
mygodsos 2008-09-18
  • 打赏
  • 举报
回复
主要是输出设备的dpi决定的吧,应该输出控制才对吧,改原始图像好像意义不大
bob008 2008-09-18
  • 打赏
  • 举报
回复
貌似这家伙想要把一个面包压成鸡蛋大小
JeffChung 2008-09-18
  • 打赏
  • 举报
回复
学习
lihuasoft 2008-09-18
  • 打赏
  • 举报
回复
1、代码绕来绕去,貌似走了许多弯路

2、你的TmpPhoto.bmp、TmpPhoto.jpg是相同的尺寸,后来的TmpPhoto.jpg只是规定长宽之后进行了Stretch调整。

倒底是想达到什么结果?根据固定的长宽压缩?或是从原始图象中裁取固定长宽的一部分?

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi GAME,图形处理/多媒体
社区管理员
  • GAME,图形处理/多媒体社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧