Delphi XE8 利用HttpClient实现的一个App自动更新组件

第一个版本,分享了。
unit AutoUpdate;

interface

uses
   System.SysUtils, System.Types, System.UITypes, System.Classes,
   System.Variants, System.IOUtils, System.JSON, System.Net.HttpClient

{$IFDEF MSWINDOWS}
       , Winapi.Windows,
{$ENDIF}
{$IFDEF IOS}
, FMX.Platform.iOS,
   iOSapi.Foundation,
   Macapi.ObjectiveC,
{$ELSE}
{$IFDEF MACOS}
, FMX.Platform.Mac,
   Macapi.Foundation,
   Macapi.ObjectiveC,
{$ELSE}
{$IFDEF ANDROID}
, Androidapi.JNI.GraphicsContentViewText,
   Androidapi.JNI.JavaTypes,
   FMX.Helpers.Android, Androidapi.Helpers,
   Androidapi.JNI.Net,
{$ENDIF ANDROID}
{$ENDIF MACOS}
{$ENDIF IOS}
   FMX.Dialogs;

type
   TAutoUpdate = class(TComponent)
   private
       FServerVersion, FInstallFileUrl, FDescription: String;
       FInstallFileName: string;
       FCheckURL: String;
       procedure SetCheckURL(const Value: String);
   public
       constructor Create(AOwner: TComponent); override;
       function Check: Boolean;
       procedure DownLoad;
       procedure Install;
       property CheckURL: String read FCheckURL write SetCheckURL;
       property ServerVersion: string read FServerVersion;
       property Description: String read FDescription;
   end;

function GetAppVersion: String;

implementation

function GetAppVersion: String;
{$IFDEF MSWINDOWS}
   function GetBuildInfoAsString: string;
   var
       V1, V2, V3, V4: word;
       procedure GetBuildInfo(var V1, V2, V3, V4: word);
       var
           VerInfoSize, VerValueSize, Dummy: DWORD;
           VerInfo: Pointer;
           VerValue: PVSFixedFileInfo;
       begin
           VerInfoSize := GetFileVersionInfoSize(PChar(ParamStr(0)), Dummy);
           if VerInfoSize > 0 then
           begin
               GetMem(VerInfo, VerInfoSize);
               try
                   if GetFileVersionInfo(PChar(ParamStr(0)), 0, VerInfoSize, VerInfo)
                   then
                   begin
                       VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);
                       with VerValue^ do
                       begin
                           V1 := dwFileVersionMS shr 16;
                           V2 := dwFileVersionMS and $FFFF;
                           V3 := dwFileVersionLS shr 16;
                           V4 := dwFileVersionLS and $FFFF;
                       end;
                   end;
               finally
                   FreeMem(VerInfo, VerInfoSize);
               end;
           end;
       end;

   begin
       GetBuildInfo(V1, V2, V3, V4);
       result := IntToStr(V1) + '.' + IntToStr(V2) + '.' + IntToStr(V3) + '.' +
           IntToStr(V4);
   end;

begin
   result := GetBuildInfoAsString;
end;
{$ENDIF}
{$IFDEF ANDROID}

var
   PackageInfo: JPackageInfo;
   PackageName: JString;
begin

   PackageName := SharedActivityContext.getPackageName;
   PackageInfo := SharedActivityContext.getPackageManager.getPackageInfo
       (PackageName, 0);
   result := JStringToString(PackageInfo.versionName);

end;
{$ENDIF}
{$IFDEF MACOS}

var
   AppNameKey: Pointer;
   AppBundle: NSBundle;
   NSAppName: NSString;
begin
   AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);

   AppNameKey := (NSSTR('CFBundleName') as ILocalObject).GetObjectID;
   NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey
       (AppNameKey));
   // Memo1.Lines.Add('CFBundleName : ' + UTF8ToString(NSAppName.UTF8String));

   AppNameKey := (NSSTR('CFBundleDisplayName') as ILocalObject).GetObjectID;
   NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey
       (AppNameKey));
   // Memo1.Lines.Add('CFBundleDisplayName : ' + UTF8ToString(NSAppName.UTF8String));

   AppNameKey := (NSSTR('CFBundleIdentifier') as ILocalObject).GetObjectID;
   NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey
       (AppNameKey));
   // Memo1.Lines.Add('CFBundleIdentifier : ' + UTF8ToString(NSAppName.UTF8String));

   AppNameKey := (NSSTR('CFBundleVersion') as ILocalObject).GetObjectID;
   NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey
       (AppNameKey));
   // Memo1.Lines.Add('CFBundleVersion : ' + UTF8ToString(NSAppName.UTF8String));
   result := UTF8ToString(NSAppName.UTF8String);

end;
{$ENDIF}
{ TAutoUpdate }

function TAutoUpdate.Check: Boolean;
var
   LocalVersion: string;
   hc: THTTPClient;
   ss: TStringStream;
   jo: TJsonObject;
begin

   hc := THTTPClient.Create;
   ss := TStringStream.Create;
   try

       hc.Get(FCheckURL, ss);

       jo := TJsonObject.ParseJSONValue(ss.DataString) as TJsonObject;
       try
           FServerVersion := jo.GetValue('Version').Value;
           FDescription := jo.GetValue('Description').Value;
           FInstallFileUrl := jo.GetValue('URL').Value;
       finally
           jo.Free;
       end;

   finally
       hc.Free;
       ss.Free;
   end;

   LocalVersion := GetAppVersion;
   result := CompareText(FServerVersion, LocalVersion) > 0;

end;

constructor TAutoUpdate.Create(AOwner: TComponent);
begin
   inherited;
   FInstallFileName := TPath.GetSharedDocumentsPath + PathDelim + 'install.apk';
end;

procedure TAutoUpdate.DownLoad;
var
   Stream: TMemoryStream;
   hc: THTTPClient;
begin
   Stream := TMemoryStream.Create;
   hc := THTTPClient.Create;
   try
       try
           hc.Get(FInstallFileUrl, Stream);
           Stream.SaveToFile(FInstallFileName);
       except
           on e: Exception do
               ApplicationShowException (e);
       end;
   finally
       hc.Free;
       Stream.Free;
   end;
end;

procedure TAutoUpdate.Install;
{$IFDEF ANDROID}
var
   Intent: JIntent;
   aUrl: string;
begin

   aUrl := 'file://' + FInstallFileName;

   Intent := TJIntent.Create;
   Intent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
   Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
   Intent.setDataAndType(TJnet_Uri.JavaClass.parse(StringToJString(aUrl)),
       StringToJString('application/vnd.android.package-archive'));
   try
       SharedActivity.startActivity(Intent);
   except
       on e: Exception do
       begin
           // if DisplayError then
           ShowMessage('Error: ' + e.Message);
       end;
   end;
end;
{$ELSE}

begin

end;
{$ENDIF}

procedure TAutoUpdate.SetCheckURL(const Value: String);
begin
   FCheckURL := Value;
end;

end.

使用了网友的取版本号及调用apk,这里谢过!

调用方法:

procedure TMainForm.Button1Click(Sender: TObject);
begin

   TThread.CreateAnonymousThread(
       procedure()
       var
           au: TAutoUpdate;
       begin

           au := TAutoUpdate.Create(Self);
           try
               au.CheckURL := 'http://wx.test.cc/test.inf';
               if au.Check then
               begin
                   au.DownLoad;
                   au.Install;
               end;
           finally
               au.Free;
           end;
       end).Start;

end;


转自:http://blog.sina.com.cn/s/blog_44fa172f0102vgd1.html

FaceSDK需要联网进行人脸识别和人脸跟踪,因为它需要访问FaceSDK服务器上的人脸库和算法。如果您需要在离线环境下使用FaceSDK,您需要先下载并安装FaceSDK的离线版。 FaceSDK的离线版包含了所有必要的人脸识别和人脸跟踪算法及其所需的数据,可以在本地计算机上运行,无需联网。以下是使用FaceSDK离线版在Delphi XE中实现人脸识别的一般步骤: 1. 下载并安装FaceSDK的离线版。 2. 在Delphi XE中创建一个新项目,并将FaceSDK的Delphi包文件添加到项目中。 3. 在代码中引用FaceSDK的Delphi单元,例如FaceSDK.Pas。 4. 使用FaceSDK提供的函数和类来实现人脸识别和人脸跟踪功能。例如,您可以使用TFSVideoDevice组件捕获视频流,并使用TFSRecognitionEngine组件识别人脸。 以下是一个简单的示例代码,用于在Delphi XE中使用FaceSDK离线版进行人脸识别: ``` uses FaceSDK; var RecognitionEngine: TFSRecognitionEngine; FaceDetector: TFSFaceDetector; VideoDevice: TFSVideoDevice; Frame: TFSImage; Faces: TFSFaceList; begin RecognitionEngine := TFSRecognitionEngine.Create; FaceDetector := TFSFaceDetector.Create; VideoDevice := TFSVideoDevice.Create; Frame := TFSImage.Create; try // 初始化RecognitionEngine和FaceDetector RecognitionEngine.InitializeOffline('path/to/facesdk/data'); FaceDetector.InitializeOffline('path/to/facesdk/data'); // 打开视频设备并捕获视频流 VideoDevice.Open; while True do begin // 从视频设备中获取一帧图像 VideoDevice.GetFrame(Frame); // 检测图像中的人脸 Faces := FaceDetector.DetectFaces(Frame); // 对检测到的每个人脸进行识别 for I := 0 to Faces.Count - 1 do begin Face := Faces[I]; // 提取人脸特征并与已知的人脸库进行比较 RecognitionEngine.Recognize(Face); if RecognitionEngine.MatchFound then begin // 识别成功,显示人脸的标识信息 ShowMessage(RecognitionEngine.MatchID); end; end; end; finally FreeAndNil(RecognitionEngine); FreeAndNil(FaceDetector); FreeAndNil(VideoDevice); FreeAndNil(Frame); end; end; ``` 上述代码演示了如何使用FaceSDK离线版在Delphi XE中实现人脸识别和人脸跟踪功能。当然,FaceSDK还提供了其他的功能和类,您可以根据自己的需求进行调整和修改。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值