|
Tutorial 2. AVI files: playing and processing Next | Previous
|
|
|
|
AVI file playing
|
|
A new feature of the TZBitmapLink component is that it can handle AVI files. This means that any application that uses this component can automatically work with AVI files. For example, try to open an AVI file in the demo project from Tutorial 1. Select the Sequence menu, then select a directory. In the File name mask dialog enter the name of an AVI file. Following this, the first frame will be shown, and all commands (Play, First, Next, Prev) can be used.
In this tutorial we create a similar application, but it will work only with AVI files. We place TToolBar with TToolButtons, TTrackBar, TImageList, TZBitmapLink and TZImageBox on the main form, as shown in the picture below.
TImageList serves only to store bitmaps for the toolbar’s buttons. For the second TToolButton we set its style in the Object Inspector to provide Play/Stop functions:
|
|
|
|
We also set
|
|
|
|
to play an AVI file as soon as its name is provided. To play an AVI file only once we set the following property:
|
|
|
|
Then we implement all five commands:
|
// Open an AVI file procedure TForm1.ToolButton1Click(Sender: TObject); begin with TOpenDialog.Create(nil) do //new dialog for file opening begin Filter:='AVI | *.avi'; DefaultExt:='avi'; if Execute then begin ZBitmapLink1.Sequence := true; ZBitmapLink1.FileName := FileName; // play file TrackBar1.Max:=ZBitmapLink1.AVILength; //Length of the AVI file end; Free; end; end;
procedure TForm1.ToolButton2Click(Sender: TObject); //play/stop begin ZBitmapLink1.SeqAction(saPlay); ToolButton2.Down:=false; end;
|
|
Other event handlers are the same as in Tutorial 1.
To make the trackbar move according to frame change, we implement the TZBitmapLink.OnChange event handler. In this event handler we will also visualize a current frame:
|
procedure TForm1.ZBitmapLink1Change(Sender: TObject); begin TrackBar1.Position:=ZBitmapLink1.AVIFrame; ZImageBox1.Bitmap:=ZBitmapLink1.Bitmap; end;
|
|
Now you can run the program and play AVI files!
|
|
AVI file processing
|
|
Note that in the TZBitmapLink.OnChange event handler it is not necessary to visualize an original frame. We can process an original frame and show the result of processing. The example below inverts each frame, so that we can see a ‘negative’ of the AVI file.
|
procedure TForm1.ZBitmapLink1Change(Sender: TObject); var x,Num:integer; P,P1:^byte; begin TrackBar1.Position:=ZBitmapLink1.AVIFrame; if (ZBitmapLink1.Bitmap<>nil) and ZBitmapLink1.Bitmap.Active then begin if NewBM=nil then // create a new image by cloning a frame from AVI file NewBM:=ZBitmapLink1.Bitmap.Clone(mmMemory,'Proc',false); with NewBM do if Palette<>nil then begin // If image is 8-bit indexed color, then // instead of inverting its pixels, we invert its palette // First, copy palette Move(ZBitmapLink1.Bitmap.Palette[0],Palette[0],Length(Palette)*sizeof(TRGBTriple)); // Then, copy image pixels move(ZBitmapLink1.Bitmap.PixelStart^,PixelStart^,abs(ScanLineWidth*DimY)); // Finally, invert palette for x:=0 to 255 do with Palette[x] do begin rgbRed:=255-rgbRed; rgbBlue:=255-rgbBlue; rgbGreen:=255-rgbGreen; end end else begin // invert image byte-by-byte P:=PixelStart; P1:=ZBitmapLink1.Bitmap.PixelStart; Num:=abs(ScanLineWidth*DimY); for x:=1 to Num do begin P^:=255-P1^; inc(P); inc(P1); end; end; // show inverted image ZImageBox1.Bitmap:=NewBM; end; end;
|
|
Converting AVI to a sequence of BMP files
|
|
Using the TZBitmapLink.OnChange event an AVI file can easily be converted to a sequence of BMP files:
|

|
|
procedure TForm1.ZBitmapLink1Change(Sender: TObject); var s:string; begin with ZBitmapLink1 do if (Bitmap<>nil) and Bitmap.Active then begin s:=IntToStr(AVIFrame); if AVIFrame<10 then s:='00'+s else if AVIFrame<100 then s:='0'+s; Bitmap.Save(FileMask+s+'.BMP'); end; end;
|
|
This procedure creates a sequence of files (File000.bmp, File001.bmp, File002.bmp ...) that correspond to frames of an AVI file. An example of the sequence is shown in the picture.
|
|
|
|
|