For those interested in storing components onto a stream

本文介绍了使用Delphi中的TStream来存储组件和对象的方法。通过实现TPlayer类继承TPersistent,并利用TWriter和TReader读写对象状态,实现了对象列表的文件存储与加载。文章提供了完整的代码示例。
From: keeper@mindspring.com (Mark R. Johnson)
Subject: Re: [Delphi] Solution:  Using TStream/TWri
Date: 19 May 1995 14:20:22 GMT

For those interested in storing components onto a stream, take a look
at TWriter.WriteRootComponent and TWriter.WriteComponent.  Then check
out the TReader.Read... counterparts.  Unfortunately, there appears
to be no documentation showing HOW to use these methods properly.
The docs keep mentioning the "root" component, but never clearly
explain what it is or how you are suppose to use the root property
to store your components.

If you are interested in writing objects to the stream that are
not components, I recommend doing something like the follow:

type
  TMyObject = class(TObject)
    ...
  protected
    procedure SaveToStream(writer : TWriter); virtual; 
    procedure LoadFromStream(writer : TWriter); virtual;
    ...
  end;

procedure TMyObject.SaveToStream(writer : TWriter);
begin
  with writer do begin
    WriteListBegin;
    {- write object state -}
    WriteListEnd;
  end;
end;

procedure TMyObject.LoadFromStream(reader : TReader);
begin
  with reader do begin
    ReadListBegin;
    while not EndOfList do begin
      {- read object state -}
    end;
    ReadListEnd;
  end;
end;

Somewhere in the initialization section of the unit in which this
object is declared, call RegisterObject('TMyObject').  (See
RegisterObject() below.)

In the main program, where you specify the file to read/write,
you can do something like this:

var
  RegisteredObjects : TStringList;

procedure RegisterObject(cname : string; ctype : TClass);
begin
  RegisteredObjects.AddObject(cname, ctype);
end;

procedure GetObject(cname : string) : TClass;
var
  i : integer;
begin
  i := RegisteredObjects.IndexOf(cname);
  if i > -1 then
    Result := TClass(RegisteredObjects.Objects[i])
  else
    Result := nil;
end;

procedure SaveFile(const filename : string; objlist : TList);
var
  stream : TFileStream;
  writer : TWriter;
  i      : integer;
begin
  stream := TFileStream.Create(filename, fmCreate or fmOpenWrite);
  try
    writer := TWriter.Create(stream, $ff);
    try
      with writer do begin
        WriteSignature;     {marker to indicate a Delphi filer object file.}
        WriteListBegin;     {outer list marker}
        for i := 0 to objlist.Count - 1 do begin
          WriteListBegin;   {object marker}
          WriteString(TMyObject(objlist[i]).ClassName);
          TMyObject(objlist[i]).SaveToStream(writer);
          WriteListEnd;     {object marker}
        end;
        WriteListEnd;       {outer list marker}
      end;
    finally
      writer.Free;
    end;
  finally
    stream.Free;
  end;
end;

procedure OpenFile(const filename : string; objlist : TList);
var
  stream : TFileStream;
  writer : TWriter;
  cname  : string;  {class name}
  ctype  : TClass;  {class type}
  obj    : TObject;
begin
  stream := TFileStream.Create(filename, fmOpenRead);
  try
    reader := TReader.Create(stream, $ff);
    try
      with reader do begin
        ReadSignature;     {check Delphi filer object signature.}
        ReadListBegin;     {outer list marker}
        while not EndOfList do begin
          ReadListBegin;   {object marker}
          while not EndOfList do begin
            cname := ReadString;
            ctype := GetObjectClass(cname);
            obj := TObject(TObjectClass(ctype).Create;
            try
              obj.LoadFromStream(reader);
            except
              obj.Free;
              raise;
            end;
            objlist.Add(obj);
          end;
          ReadListEnd;     {object marker}
        end;
        ReadListEnd;       {outer list marker}
      end;
    finally
      reader.Free;
    end;
  finally
    stream.Free;
  end;
end;


Well, I don't know how far this will get you.  I haven't tested ANY
of this code, so who knows if it could ever possibly work.  The most
doubtful part is the whole dynamic instantiation taking place in
LoadFromStream().  Delphi provides a bunch of great functions for
registering TPersistent descendants and getting their class types
from their class names, etc.:  RegisterClass(), FindFieldClass(),
FindClass(), GetClass(), etc.  (They use it for loading components
off of streams....no surprise there.)  However, if your objects
are not TPersistent descendants (and there's no reason they should
be), then you're basically out of luck (read: "you get to write
your own RegisteredClass()").

So, give this a try if you're feeling daring.  Just don't come running
after me with a shotgun complaining about little voices in your heads
if you do.  I suspect the above code will need a lot of polish before
it does what is expected of it...  Nonetheless, I hope you find it
interesting, if nothing else.

--Mark Johnson

______________________________________________________________________________
 Mark R. Johnson       _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_
 Atlanta, GA USA       _/  http://www.mindspring.com/~cityzoo/cityzoo.html  _/
 keeper@mindspring.com _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

------------------------------------------------------------------------------

From: keeper@mindspring.com (Mark Johnson)
Subject: Re: [delphi] Need a Tlist of pictures...
Date: Wed, 09 Aug 1995 23:47:16 -0500

Well, shoot.  After a bit of research and review, I came to realize just
how unnecessary all of this work with trying to store plain objects
on a stream is.  When I wrote up the TStream2 message back in May
(only shortly after Delphi came out), I did not have a good understanding
of the VCL class heirarchy.

Here's a quote from the Component Writer's Guide (TPersistent):

The TPersistent object is the abstract base object for all objects
stored and loaded on Delphi stream objects. In addition to the methods
it inherits from its ancestor, TObject, TPersistent defines three new
methods: AssignTo and DefineProperties, which are protected, and Assign,
which is public.

The GetClass() and RegisterClass() functions work for TPersistent objects.

So, after being in the dark for so long, I sat down and just took a
good long look at TPersistent and other related matters.  Then, after
looking back at what you wanted to do, I wrote up a little program
and tested it out to make sure it actually worked.  Below is the result.

Below I have included two units: Unit1, which is a form definition, and
Unit2, which contains the TPlayer and TObjectList classes.  The form
(Unit1) has for buttons labelled "Create," "Save," "Load," and "Exit."

        Create -- create 5 TPlayers and add them to the object list
        Save   -- save the object list to a file
        Load   -- load the object list from a file
        Exit   -- free the object list and exit

In Unit2, the TPlayer object is declared as a TPersistent descendant
and is given two methods:  ReadData() and WriteData().

        ReadData()  -- read property data with given TReader object
        WriteData() -- write property data with given TWriter object

The TPlayer class is registered with a call to RegisterClass in the
initialization section of Unit2 when the program first begins.

Also in Unit2, the TObjectList class is declared as a TList and
given the methods Clear(), SaveToStream(), LoadFromStream(),
SaveToFile(), and LoadFromFile().  The SaveToFile() and LoadFromFile()
just create a TFileStream object and then pass it to the corresponding
SaveToStream()/LoadFromStream() method, which do the actually accessing
via the TFiler objects (TWriter & TReader).  A destructor was also
added to TObjectList to ensure that it frees the items in the list
when it is destroyed.

I think it would be a good idea to go back into the TObjectList and
add a new kind of Items property that is specifically typed as
TPersistent or TObject instead of just Pointer since many of the
operations we perform on its Items property could cause problems
if a non-object were accidentally stored in the list.  It would also
reduce the need for all of the extra type casting.

Anyhow, here are the two units that worked for me.  Let me know what you
think.

--Mark Johnson

--------------------------------- UNIT1.PAS ---------------------------------

unit Unit1;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btnCreate: TButton;
    btnSave: TButton;
    btnLoad: TButton;
    btnExit: TButton;
    procedure btnCreateClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure btnLoadClick(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;


implementation

{$R *.DFM}

uses
  Unit2;

const
  ObjFilename = 'C:/players.dat';

var
  objList : TObjectList;

procedure TForm1.btnCreateClick(Sender: TObject);
var
  player : TPlayer;
  i      : integer;
begin
  {Creates five players and adds them to ObjList}
  objList.Clear;
  for i := 1 to 5 do begin
    player := TPlayer.Create;
    try
      with player do begin
        Name       := 'Name' + IntToStr(i);
        EmpireName := 'EmpireName' + IntToStr(i);
        Wins       := i;
        Losses     := 5 - i;
        Ranking    := 6 - i;
      end;
      objList.Add(player);
    except
      player.Free;
      raise;
    end;
  end;
end;

procedure TForm1.btnSaveClick(Sender: TObject);
begin
  {Stores objList to file}
  objList.SaveToFile(ObjFilename);
end;

procedure TForm1.btnLoadClick(Sender: TObject);
begin
  {Loads objList from file}
  objList.LoadFromFile(ObjFilename);
end;

procedure TForm1.btnExitClick(Sender: TObject);
begin
  {Frees objList (and everything in list) and exits}
  objList.Free;
  Close;
end;

initialization
  objList := TObjectList.Create;
end.

--------------------------------- UNIT2.PAS ---------------------------------

unit Unit2;

interface

uses
  Classes;

type
  TPlayer = class(TPersistent)
  private
    FName       : string;
    FEmpireName : string;
    FWins       : integer;
    FLosses     : integer;
    FRanking    : integer;
  public
    procedure ReadData(reader : TReader); dynamic;
    procedure WriteData(writer : TWriter); dynamic;
  published
    property Name : string read FName write FName;
    property EmpireName : string read FEmpireName write FEmpireName;
    property Wins : integer read FWins write FWins;
    property Losses : integer read FLosses write FLosses;
    property Ranking : integer read FRanking write FRanking;
  end;

  TObjectlist=class(TList)
  public
    destructor Destroy; override;
    procedure Clear;
    procedure SaveToStream(stream : TStream);
    procedure LoadFromStream(stream : TStream);
    procedure SaveToFile(const filename : string);
    procedure LoadFromFile(const filename : string);
  end;


implementation

uses
  SysUtils;

{TPlayer}

procedure TPlayer.ReadData(reader : TReader);
begin
  with reader do begin
    Name       := ReadString;
    EmpireName := ReadString;
    Wins       := ReadInteger;
    Losses     := ReadInteger;
    Ranking    := ReadInteger;
  end;
end;

procedure TPlayer.WriteData(writer : Twriter);
begin
  with writer do begin
    WriteString(Name);
    WriteString(EmpireName);
    WriteInteger(Wins);
    WriteInteger(Losses);
    WriteInteger(Ranking);
  end;
end;


{TObjectList}

destructor TObjectList.Destroy;
begin
  {deallocate objects in list before termination}
  Clear;
  inherited Destroy;
end;

procedure TObjectList.Clear;
var
  i : integer;
begin
  {This routine deallocates all resources inside this list}
  for i := 0 to Count - 1 do begin
    TObject(Items[0]).Free;
    Delete(0);
  end;
end;

procedure TObjectList.SaveToStream(stream : TStream);
var
  writer : TWriter;
  i      : integer;
begin
    writer := TWriter.Create(stream, $ff);
    try
      with writer do begin
        {mark beginning of file and beginning of object list}
        WriteSignature;
        WriteListBegin;
        {loop through this list}
        for i := 0 to Count - 1 do begin
          {Store any TPersistent objects}
          if TObject(Items[i]) is TPersistent then begin
            WriteString(TPersistent(Items[i]).ClassName);
            {Call WriteData() for TPlayer objects}
            if (TPersistent(Items[i]) is TPlayer) then
              TPlayer(Items[i]).WriteData(writer);
          end;
        end;
        {mark end of object list}
        WriteListEnd;
      end;
    finally
      writer.Free;
    end;
end;

procedure TObjectList.LoadFromStream(stream : TStream);
var
  reader : TReader;
  obj    : TPersistent;
  ctype  : TPersistentClass;
  cname  : string;
  i      : integer;
begin
  reader:=TReader.Create(stream,$ff);
  try
    with reader do begin
      {read beginning of file and beginning of object list markers}
      ReadSignature;
      ReadListBegin;
      {loop through file list of objects}
      while not EndOfList do begin
        {Load ClassName and use it to get ClassType}
        cname := ReadString;
        ctype := GetClass(cname);
        if Assigned(ctype) then begin  {"Assigned()" == " <> nil" but quicker}
          {If a ClassType was found, create an instance}
          obj := ctype.Create;
          try
            {if obj is a TPlayer, call its ReadData() method}
            if obj is TPlayer then
              TPlayer(obj).ReadData(reader);
          except
            obj.free;
            raise;
          end;
          {add object to this list}
          Add(obj);
        end;
      end;
      ReadListEnd;
    end;
  finally
    reader.Free;
  end;
end;

procedure TObjectList.SaveToFile(const filename : string);
var
  stream : TFileStream;
begin
  stream := TFileStream.Create(filename, fmCreate or fmOpenWrite);
  try
    SaveToStream(stream);
  finally
    stream.Free;
  end;
end;

procedure TObjectList.LoadFromFile(const filename : string);
var
  stream : TFileStream;
begin
  stream := TFileStream.Create(filename, fmOpenRead);
  try
    Clear;
    LoadFromStream(stream);
  finally
    stream.Free;
  end;
end;

initialization
  {register TPlayer class here when program begins}
  RegisterClass(TPlayer);
end.
______________________________________________________________________________
 Mark R. Johnson       _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_
 Atlanta, GA USA       _/  http://www.mindspring.com/~cityzoo/cityzoo.html  _/
 keeper@mindspring.com _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
本课题设计了一种利用Matlab平台开发的植物叶片健康状态识别方案,重点融合了色彩与纹理双重特征以实现对叶片病害的自动化判别。该系统构建了直观的图形操作界面,便于用户提交叶片影像并快速获得分析结论。Matlab作为具备高效数值计算与数据处理能力的工具,在图像分析与模式分类领域应用广泛,本项目正是借助其功能解决农业病害监测的实际问题。 在色彩特征分析方面,叶片影像的颜色分布常与其生理状态密切相关。通常,健康的叶片呈现绿色,而出现黄化、褐变等异常色彩往往指示病害或虫害的发生。Matlab提供了一系列图像处理函数,例如可通过色彩空间转换与直方图统计来量化颜色属性。通过计算各颜色通道的统计参数(如均值、标准差及主成分等),能够提取具有判别力的色彩特征,从而为不同病害类别的区分提供依据。 纹理特征则用于描述叶片表面的微观结构与形态变化,如病斑、皱缩或裂纹等。Matlab中的灰度共生矩阵计算函数可用于提取对比度、均匀性、相关性等纹理指标。此外,局部二值模式与Gabor滤波等方法也能从多尺度刻画纹理细节,进一步增强病害识别的鲁棒性。 系统的人机交互界面基于Matlab的图形用户界面开发环境实现。用户可通过该界面上传待检图像,系统将自动执行图像预处理、特征抽取与分类判断。采用的分类模型包括支持向量机、决策树等机器学习方法,通过对已标注样本的训练,模型能够依据新图像的特征向量预测其所属的病害类别。 此类课题设计有助于深化对Matlab编程、图像处理技术与模式识别原理的理解。通过完整实现从特征提取到分类决策的流程,学生能够将理论知识与实际应用相结合,提升解决复杂工程问题的能力。总体而言,该叶片病害检测系统涵盖了图像分析、特征融合、分类算法及界面开发等多个技术环节,为学习与掌握基于Matlab的智能检测技术提供了综合性实践案例。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值