Read Barcode 有粉多種方法,趕在明天上 Gordon 大師的技術研討會前,先這次介紹其中比較簡單使用的一種(也許並非最好的,相信 Gordon 大師會有更好的分享)。
在 Read Barcode 時,真正讀取的部分都是運用現成、別人寫好的東西,不管是透過呼叫 apk 或是 java class 的方式都一樣,否則要重頭到尾自己把解析 barcode 這段邏輯搞出來,就已經可以搞到退休了,畢竟 barcode 那麼多種,解析影像或是圖片的方式,也不是常人可以理解的(包含筆者我啦)。這次要運用的,是一個很多手機出廠時就會預先安裝的 app,名稱為「條碼掃描器」
所以在完成後續這個 app 之前,一定要先確認有安裝「條碼掃描器」這個 app,否則是沒辦法執行正確的…
接下來就在主 Form 上各擺一個 TMemo、TButton、TTimer,不囉唆,直接把 code 完整 port 上
首先是畫面
接下來是 code
unit uUITest; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.platform, fmx.helpers.android, androidapi.JNI.GraphicsContentViewText, androidapi.jni.JavaTypes, FMX.StdCtrls, FMX.Edit, FMX.Layouts, FMX.Memo; type TForm1 = class(TForm) btnReadBarcode: TButton; Timer1: TTimer; Memo1: TMemo; procedure FormCreate(Sender: TObject); procedure btnReadBarcodeClick(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private declarations } FClipService: IFMXClipboardService; FTimes : Integer; public { Public declarations } end; var Form1: TForm1; implementation {$R *.fmx} procedure TForm1.btnReadBarcodeClick(Sender: TObject); var Intent: JIntent; begin Memo1.Text := ''; if Assigned(FClipService) then begin FTimes := 0; FClipService.SetClipboard('nil'); Intent := TJIntent.Create; Intent.setAction(StringToJString('com.google.zxing.client.android.SCAN')); Intent.putExtra(tjintent.JavaClass.EXTRA_TEXT, stringtojstring('"SCAN_MODE", "CODE_39"')); SharedActivity.startActivityForResult(Intent,0); Timer1.Enabled := true; end; end; procedure TForm1.FormCreate(Sender: TObject); begin if not TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService, IInterface(FClipService)) then FClipService := nil; end; procedure TForm1.Timer1Timer(Sender: TObject); begin if (FClipService.GetClipboard.ToString <> 'nil') then begin Timer1.Enabled := False; FTimes := 0; Memo1.Text := FClipService.GetClipboard.ToString; end else begin if FTimes > 100 then begin Timer1.Enabled := False; FTimes := 0; end else FTimes := FTimes + 1; end; end; end. |
執行結果
先看一下條碼範本....7-11 的發票
app 執行結果
上面是讀取一維條碼的部分,你也可以嘗試去讀取 QRCode,會獲得更多資訊
to be continued...
文章標籤
全站熱搜