用Delphi调整任意分辨率的jpeg格式图像
摘 要:使用Delphi编写程序,批量处理图像文件,将所读取的图像文件形成分辨率符合要求的文件。
关键词:Delphi ;API函数
在计算机及网络迅速发展的现代社会,计算机越来越多地改变了我们的生活。比如在许多考试报名中,需要采集电子照片,而且许多单位职工的信息存档整理中,都对照片有一定的要求。如果只是少量的照片,大家可以用Photoshop 去做一个处理,但对于大量的图片来说,一张张的调整不仅仅枯燥无味,也容量产生一些人为的偏差,在此,可以教大家一个比较方便的方法。利用编写好的程序来对图像进行批量处理,就可以很方便地得到符合要求的图片了。
经过一段时间的研究,我们通过DELPHI编写一程序,利用windows的API函数处理数据,完全可以实现图像的采集和处理,一次达到用户的要求。具体过程如下:
1、首先在uses 加入Jpeg单元
2、声明公共娈量和函数
public
hWndC:THandle;
FOldX,FOldY:Integer;
trace,Flag:Boolean;
x1,x2,y1,y2,oldx,oldy:integer;
ip1:integer;
left1,top1,width1,height1:integer;
function fStartCap(aWidth,aHeight:Integer;aHandle:THandle):Boolean;
{ Public declarations }
end;
3、在var中定义中所需参数
const wm_cap_start = wm_user;
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_callback_videostream=wm_cap_start +6;
const wm_cap_driver_connect= wm_cap_start + 10;
const wm_cap_driver_disconnect = wm_cap_start + 11;
const wm_cap_file_set_capture_filea = wm_cap_start + 20;
const wm_cap _savedib = wm_cap_start + 25;
const wm_cap _set_preview = wm_cap_start + 50 ;
const wm_cap _set_overlay = wm_cap_start + 51 ;
const wm_cap _set_previewrate= wm_cap_start + 52 ;
const wm_cap _set_scale= wm_cap_start + 53 ;
const wm_cap _grab_frame = wm_cap_start + 60;
const wm_cap _sequence = wm_cap_start + 62;
const wm_cap_sequence_nofile = wm_cap_start +63 ;
const wm_cap_stop = wm_cap_start + 68;
begin
Result:=False;
hWndC := capCreateCaptureWindowA('My Own Capture Window',WS_CHILD or WS_VISIBLE,0,0,aWidth,aHeight,aHandle,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);
Result:=True;
end;
end;
4、添加一按钮,完成分辨率的设置, 并保存
procedure 1Click(Sender: TObject);
const
BufferSize = 50;
DPI = 1; //inch
DPC = 2; //cm
var
Buffer: string;
index: INTEGER;
FileStream: TFileStream;
xResolution: WORD;
yResolution: WORD;
_type: Byte;
name1:string;
jpg:TJpegImage;
dpix,dpiy:integer;
begin
jpg:=;
();
if e then
File(me);
:=me ;
File();
;
dpix:=strtoint();
dpiy:=strtoint();
name1:=;
if name1<>'' then
begin
FileStream:=(name1, mOpenReadWrite);
try
SetLength(Buffer, BufferSize);
(buffer[1], BufferSize);
index := POS('JFIF' + #$00, buffer);
if index > 0
then begin
(index + 6, soFromBeginning);
_type := DPI;
(_type, 1);
xresolution := swap(dpix);
(xresolution, 2);
yresolution := swap(dpiy);
(yresolution, 2);
end
finally
;
showmessage('保存成功');
e:=true;
d:=true;
us;
d:=false;
end;
end;
end;
有了以上程序,就可以批量处理图像文件,将图像文件形分辨率符合要求的图像。