1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
Google ZXing Call demo
03.
04. Delphi Version: Delphi XE5 Version 19.0.13476.4176
05.
06. By: flcop(zylove619@hotmail.com)
07.}
08.
09.unit UMain;
10.
11.interface
12.
13.uses
14. System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
15. FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
16. FMX.Layouts, FMX.Memo,
17.
18. System.Rtti,
19. FMX.Helpers.Android,
20. Androidapi.JNI.Net,
21. Androidapi.JNI.GraphicsContentViewText,
22. Androidapi.JNI.JavaTypes,
23. FMX.platform,
24. FMX.Platform.Android;
25.
26.type
27. TFrmMain = class(TForm)
28. Memo1: TMemo;
29. Button1: TButton;
30. Button2: TButton;
31. Button3: TButton;
32. PanelOpt: TPanel;
33. Panel2: TPanel;
34. SBTitle: TSpeedButton;
35. procedure Button1Click(Sender: TObject);
36. procedure FormCreate(Sender: TObject);
37. procedure SBTitleClick(Sender: TObject);
38. private
39. { Private declarations }
40. FClipboardService: IFMXClipboardService;
41. FClipboardValue: TValue;
42. FZXingCalled: Boolean;
43. procedure CallZXing(const ACodeMode: string);
44. function IsIntentCallable(const AIntent: JIntent): Boolean;
45. function GetZXingIntent: JIntent;
46. procedure ClipboardSave;
47. procedure ClipboardBack;
48. procedure ShowInfo(const AInfo: string);
49. function HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
50. procedure CheckEnvironment;
51. procedure OpenURL(const AURL: string);
52. public
53. { Public declarations }
54. end;
55.
56.var
57. FrmMain: TFrmMain;
58.
59.implementation
60.
61.{$R *.fmx}
62.
63.const
64. CodeModes: array[0..2] of string = ('PRODUCT_MODE', 'QR_CODE_MODE', 'SCAN_MODE');
65.
66.procedure TFrmMain.Button1Click(Sender: TObject);
67.begin
68. // 0, 1, 2
69. CallZXing(CodeModes[TButton(Sender).Tag]);
70.end;
71.
72.procedure TFrmMain.CallZXing(const ACodeMode: string);
73.var
74. LIntent: JIntent;
75.begin
76. ClipboardSave;
77. FClipboardService.SetClipboard('');
78. LIntent := GetZXingIntent();
79. LIntent.putExtra(StringToJString('SCAN_MODE'), StringToJString(ACodeMode));
80. SharedActivity.startActivityForResult(LIntent, 0);
81. FZXingCalled := True;
82.end;
83.
84.procedure TFrmMain.CheckEnvironment;
85.var
86. LFMXApplicationEventService: IFMXApplicationEventService;
87. LIsZXingCallable: Boolean;
88. LStr: string;
89.begin
90. if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService,
91. IInterface(LFMXApplicationEventService)) then
92. LFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)
93. else
94. LStr := '调用失败,不支持IFMXApplicationEventService!';
95.
96. if not TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService,
97. IInterface(FClipboardService)) then
98. LStr := LStr + sLineBreak + '调用失败, 不支持IFMXClipboardService!';
99.
100. LIsZXingCallable := IsIntentCallable(GetZXingIntent);
101. if not LIsZXingCallable then
102. begin
103. SBTitle.Text := '点我去ZXing下载页...';
104. SBTitle.OnClick := SBTitleClick;
105. LStr := LStr + sLineBreak + '未发现ZXing, 请安装ZXing后重新启动本程序';
106. end else
107. SBTitle.Text := 'Google ZXing Call';
108.
109. ShowInfo(LStr.Trim);
110.
111. PanelOpt.Enabled := Assigned(LFMXApplicationEventService) and
112. Assigned(FClipboardService) and LIsZXingCallable;
113.end;
114.
115.procedure TFrmMain.ClipboardBack;
116.begin
117. FClipboardService.SetClipboard(FClipboardValue);
118.end;
119.
120.procedure TFrmMain.ClipboardSave;
121.begin
122. FClipboardValue := FClipboardService.GetClipboard;
123.end;
124.
125.procedure TFrmMain.FormCreate(Sender: TObject);
126.begin
127. CheckEnvironment;
128.end;
129.
130.function TFrmMain.GetZXingIntent: JIntent;
131.const
132. GOOGLE_ZXING = 'com.google.zxing.client.android.SCAN';
133. GOOGLE_ZXING_PACKAGE = 'com.google.zxing.client.android';
134.begin
135. Result := TJIntent.JavaClass.init(StringToJString(GOOGLE_ZXING));
136. Result.setPackage(StringToJString(GOOGLE_ZXING_PACKAGE));
137.end;
138.
139.function TFrmMain.HandleAppEvent(AAppEvent: TApplicationEvent;
140. AContext: TObject): Boolean;
141.var
142. LResult: string;
143.begin
144. if FZXingCalled and (AAppEvent = TApplicationEvent.aeBecameActive) then
145. begin
146. LResult := FClipboardService.GetClipboard.ToString;
147. if LResult.IsEmpty then
148. ShowInfo('扫描取消')
149. else
150. ShowInfo(LResult);
151. ClipboardBack;
152. FZXingCalled := False;
153. end;
154. Result := True;
155.end;
156.
157.function TFrmMain.IsIntentCallable(const AIntent: JIntent): Boolean;
158.var
159. LJPackageManager: JPackageManager;
160.begin
161. Result := False;
162. if Assigned(AIntent) then
163. begin
164. LJPackageManager := SharedActivityContext.getPackageManager;
165. Result := LJPackageManager.queryIntentActivities(AIntent,
166. TJPackageManager.JavaClass.MATCH_DEFAULT_ONLY).size <> 0;
167. end;
168.end;
169.
170.procedure TFrmMain.OpenURL(const AURL: string);
171.var
172. LIntent: JIntent;
173.begin
174. LIntent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
175. TJnet_Uri.JavaClass.parse(StringToJString(AURL)));
176. SharedActivity.startActivity(LIntent);
177.end;
178.
179.procedure TFrmMain.SBTitleClick(Sender: TObject);
180.begin
181. OpenURL('http://code.google.com/p/zxing/downloads/list');
182.end;
183.
184.procedure TFrmMain.ShowInfo(const AInfo: string);
185.begin
186. Memo1.Text := AInfo;
187.end;
188.
189.end. |