編輯:關於 Android 程式設計
本文整理 Android 端車牌識別 SDK 的集成要點,涵蓋授權激活的四種方式、拍攝要求、接口定義,以及三種授權模式下的完整接入步驟。
安裝文通車牌演示程序 plate_id_sample_service.apk 即可體驗效果。SDK 支持 Android 2.3 至 5.1 版本。
首次調用識別能力時,需要提供序列號或激活文件完成激活。共有四種方式:
(1)在線序列號激活
由文通公司提供含序列號的 Excel 文件。在調用程序中指定 sn 參數對應該設備的序列號,首次識別時設備必須聯網。可選擇綁定設備 deviceid 或 SIM 卡序號,默認綁定 deviceid。
(2)離線文件激活
用戶先提供含 deviceid 或 androidid 的 Excel 文件(第一列 deviceid,第二列 androidid),文通公司隨後提供對應的激活文件壓縮包,包內激活文件數量與設備數量一致,命名格式為 [deviceid]_cp.txt。調用時指定 authfile 參數(激活文件的路徑與名稱),首次識別無需聯網。若 authfile 為 null 或空字符串,系統會默認在根目錄查找 [deviceid]_cp.txt,存在則執行激活。
(3)TF 卡模式
需使用文通公司授權的 TF 卡,授權信息存放在卡內的 license 目錄下。
(4)項目授權方式
項目授權文件中限制了以下內容,每一項都會被逐一校驗:
項目授權文件(authmode.lsc)需放入項目的 assets 文件夾中。
注意:若採用方式 1 或 2,請只指定 sn 參數或 authfile 參數其中之一,切勿同時指定,否則系統只會按序列號方式激活。此外,採用項目授權時無需調用驗證授權服務。
SDK 支持的 JPEG 圖像分辨率為 2048×1536 像素及以下,圖像中車牌的默認實際寬度宜在 80–400 像素之間。
可通過 Activity 或 Service 兩種方式集成識別能力,均支持 JPG 格式圖片,官方推薦使用 Service 方式。識別參數通過函數調用設置,詳見後文。
集成時請注意:
| 類型 | 名稱 | 說明 | 示例 |
|---|---|---|---|
| String | cls | 接收識別返回值的包名類名 | com.wintone.demo.PlateIDRunner |
| String | pic | 圖片路徑 | |
| int | imageformat | 圖像格式 | 1 |
| int | width | 圖像寬度 | 420 |
| int | height | 圖像高度 | 232 |
| int | bVertFlip | 是否垂直翻轉 | 0 |
| int | bDwordAligned | 是否按 DWORD 對齊 | 1 |
| boolean | GetVersion | 是否需要獲取開發包版本信息 | true |
| String | sn | 序列號,首次調用時激活;不設置則需在激活頁面手動輸入 | 請輸入大寫序列號 |
| String | authfile | 激活文件路徑,不用時設置為 "" | |
| String | userdata | 用戶自定義字符串,會與識別結果一起返回 | |
| String | returntype | 接收識別結果的方式。默認 "" 為第一種;值為 "withvalue" 為第二種 |
nRet(識別結果標志):
GetFieldName(字段名數組):車牌號、車牌顏色、車牌顏色代碼、車牌類型代碼、整牌可信度、亮度評價、車牌運動方向、車牌左上點橫坐標、車牌左上點縱坐標、車牌右下點橫坐標、車牌右下點縱坐標、時間、車的亮度、車的顏色。
GetRecogResult(字段值數組),各枚舉編碼如下:
此外還會返回 ReturnGetVersion(版本信息,需 GetVersion 為 true)、ReturnUserData(用戶自定義數據)、ReturnLPFileName(用戶指定的圖像路徑)。
Service 方式涉及兩個服務:驗證服務 AuthService 與識別服務 RecogService。
驗證服務參數規則:序列號方式激活用 sn;激活文件方式用 authfile;兩者不要同時指定,否則只按序列號方式激活;兩者都不設置時,會在根目錄尋找激活文件。TF 卡授權方式無需調用驗證服務,直接調用識別服務即可。
驗證服務調用順序:bindService → getAuth → unbindService,即每次生成新的驗證服務實例完成校驗。
識別服務調用順序:在 ReturnAuthority 為 0(授權成功)的前提下,每次識別依次調用 bindService → getInitPlateIDSDK → setRecogArgu → doRecogDetail → unbindService。
// 識別 JPEG 圖片 public String[] doRecogDetail(PlateRecognitionParameter prp)
(1)在 AndroidManifest.xml 中聲明服務
<service
android:enabled="true"
android:name="com.wintone.plateid.AuthService" />
<service
android:enabled="true"
android:name="com.wintone.plateid.RecogService" />
(2)綁定授權驗證服務
Intent authIntent = new Intent(MainActivity.this, AuthService.class); bindService(authIntent, authConn, Service.BIND_AUTO_CREATE);
(3)驗證服務綁定成功後的處理
public ServiceConnection authConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
authBinder = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
authBinder = (AuthService.MyBinder) service;
try {
// sn:序列號方式激活時設置,否則寫 ""
// authfile:激活文件方式激活時設置,否則寫 ""
// 兩者都不為 "" 時按序列號方式激活;都為 "" 時在根目錄找
// [deviceid]_cp.txt
ReturnAuthority = authBinder.getAuth(sn, authfile);
if (ReturnAuthority != 0) {
recogButton.setEnabled(false);
getResult(new String[]{"" + ReturnAuthority});
Toast.makeText(getApplicationContext(), "授權驗證失敗",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "授權驗證成功",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (authBinder != null) {
unbindService(authConn); // 用完即解綁
}
}
}
};
(4)綁定識別服務並執行識別
Intent recogIntent = new Intent(MainActivity.this, RecogService.class);
bindService(recogIntent, recogConn, Service.BIND_AUTO_CREATE);
public ServiceConnection recogConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
recogConn = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
recogBinder = (RecogService.MyBinder) service;
iInitPlateIDSDK = recogBinder.getInitPlateIDSDK();
if (iInitPlateIDSDK != 0) {
getResult(new String[]{"" + iInitPlateIDSDK});
return;
}
// 設置識別參數
PlateCfgParameter cfgparameter = new PlateCfgParameter();
cfgparameter.armpolice = 4;
cfgparameter.armpolice2 = 16;
cfgparameter.embassy = 12;
cfgparameter.individual = 0;
cfgparameter.nContrast = 0;
cfgparameter.nOCR_Th = 2;
cfgparameter.nPlateLocate_Th = 5;
cfgparameter.onlylocation = 15;
cfgparameter.tworowyellow = 2;
cfgparameter.tworowarmy = 6;
cfgparameter.szProvince = "";
cfgparameter.onlytworowyellow = 11;
cfgparameter.tractor = 8;
cfgparameter.bIsNight = 0;
recogBinder.setRecogArgu(cfgparameter, imageformat, bVertFlip, bDwordAligned);
// 執行識別
PlateRecognitionParameter prp = new PlateRecognitionParameter();
prp.height = height;
prp.width = width;
prp.pic = recogPicPath;
fieldvalue = recogBinder.doRecogDetail(prp);
nRet = recogBinder.getnRet();
if (nRet != 0) {
getResult(new String[]{"" + nRet});
} else {
getResult(fieldvalue);
}
if (recogBinder != null) {
unbindService(recogConn);
}
}
};
public void setRecogArgu(PlateCfgParameter cfgparameter, int imageformat,
int bVertFlip, int bDwordAligned)
該函數位於識別 Service 內部,cfgparameter 的字段如下,其餘參數請勿修改。
| 參數名稱 | 說明 | 默認值 |
|---|---|---|
| nPlateLocate_Th | 定位閾值,取值 0–9;7 為默認,0 最寬鬆,9 最嚴格 | 5 |
| nOCR_Th | 識別閾值,取值 0–9;5 為默認,0 最寬鬆,9 最嚴格 | 2 |
| bIsAutoSlope | 是否進行傾斜校正 | 1 |
| nSlopeDetectRange | 傾斜校正範圍,取值 0–16 | 0 |
| szProvince | 省份順序 | |
| nContrast | 清晰度指數,0–9;1 為最模糊,9 為最清晰 | 0(不校驗) |
| bIsNight | 是否夜間模式:1 是,0 否 | 0 |
| individual | 是否開啟個性化車牌:0 是,1 否 | 0 |
| tworowyellow | 雙層黃色車牌:2 開啟,3 關閉 | 3 |
| armpolice | 單層武警車牌:4 開啟,5 關閉 | 5 |
| tworowarmy | 雙層軍隊車牌:6 開啟,7 關閉 | 7 |
| tractor | 農用車車牌:8 開啟,9 關閉 | 9 |
| onlytworowyellow | 只識別雙層黃牌:10 開啟,11 關閉 | 11 |
| embassy | 使館車牌:12 開啟,13 關閉 | 13 |
| onlylocation | 只定位車牌:14 開啟,15 關閉 | 15 |
| armpolice2 | 雙層武警車牌:16 開啟,17 關閉 | 17 |
doRecogDetail 的參數 prp
| 類型 | 參數名稱 | 說明 | 默認值 |
|---|---|---|---|
| byte[] | picByte | 相機獲取的字節數組數據 | |
| String | pic | 拍照識別時圖片的存儲路徑 | |
| int | width | 圖像寬度 | |
| int | height | 圖像高度 | |
| String | userdata | 用戶自定義數據 | |
| String | devCode | 開發碼 | |
| String | dataFile | 時間授權文件路徑 | |
| String | versionfile | 版本授權文件路徑 | |
| boolean | isCheckDevType | 是否檢查設備型號 | false |
| int | plateIDCfg.bRotate | 預覽圖片識別時的旋轉角度:0 不旋轉、1 順時針 90°、2 旋轉 180°、3 旋轉 270° | 0 |
| int | plateIDCfg.left / right / top / bottom | 掃描框在預覽圖片上對應的四個坐標點 | 0 |
識別完成後通過 getnRet() 獲取返回碼(含義同 4.2 節)。
把 assets、lib、libs 文件夾按目錄結構複製到集成項目中。此後在 Eclipse 中選中所有 jar 包並 add Path:右鍵 lib 文件夾 → Build Path → Configure Build Path → Order and Export,勾選 android_auth.jar、ksoap2-android-assembly-2.4-jar-with-dependencies.jar、plate_id_sdk.jar、bcprov-ext-jdk15-146.jar、pki.jar 等全部條目並確認。
同時把 res 的內容合併到項目中,並將 AndroidManifest.xml 與 res/values/strings.xml 的內容合併到項目相關文件。
需要加入的權限:
<!-- 讀取機器各種 ID --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 在 SDCard 中創建與刪除文件 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- 往 SDCard 寫入數據 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 關閉該包下的所有應用程序 --> <uses-permission android:name="android.permission.RESTART_PACKAGES" /> <!-- 訪問網絡 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.VIBRATE" /> <!-- 相機相關 --> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
需要聲明的組件:
<service android:name="com.wintone.plateid.AuthService" android:enabled="true" />
<service android:name="com.wintone.plateid.RecogService" android:enabled="true" />
<activity
android:name="com.wintone.plateid.MemoryCameraActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboard|keyboardHidden" />
<activity android:name="com.wintone.plateid.MemoryResultActivity" />
<activity
android:name="com.wintone.plateid.ResultActivity"
android:screenOrientation="portrait" />
註冊時請務必替換為自己的包名。
ReturnAuthority 是授權成功與否的標誌,為 0 表示成功。以下代碼需在進入拍照界面前調用。
public ServiceConnection authConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
authBinder = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
authBinder = (AuthService.MyBinder) service;
try {
PlateAuthParameter pap = new PlateAuthParameter();
pap.sn = sn; // 序列號方式時設置,否則寫 ""
pap.authFile = authfile; // 激活文件方式時設置,否則寫 ""
ReturnAuthority = authBinder.getAuth(pap);
if (ReturnAuthority != 0) {
Toast.makeText(getApplicationContext(),
getString(R.string.license_verification_failed) + ":" + ReturnAuthority,
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
R.string.license_verification_success,
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (authBinder != null) {
unbindService(authConn);
}
}
}
};
// 在 Activity 的 onCreate() 中調用
Intent authIntent = new Intent(MainActivity.this, AuthService.class);
bindService(authIntent, authConn, Service.BIND_AUTO_CREATE);
// 拍照識別
Intent cameraintent = new Intent(MainActivity.this, MemoryCameraActivity.class);
cameraintent.putExtra("camera", false);
startActivity(cameraintent);
// 視頻識別
Intent video_intent = new Intent();
video_intent.setClass(getApplicationContext(), MemoryCameraActivity.class);
video_intent.putExtra("camera", true);
startActivity(video_intent);
// 從系統相冊選擇圖片
Intent selectIntent = new Intent(Intent.ACTION_GET_CONTENT);
selectIntent.addCategory(Intent.CATEGORY_OPENABLE);
selectIntent.setType("image/*");
Intent wrapperIntent = Intent.createChooser(selectIntent, "Select Picture");
startActivityForResult(wrapperIntent, SELECT_RESULT_CODE);
綁定與回調結構與 5.2 節完全一致,此處不再重複貼出完整代碼。集成時若遇問題,請及時聯繫技術支持人員。
向文通科技銷售提供需要綁定的參數,取得項目授權文件 authmode.lsc。把 assets、lib、libs 按目錄結構複製到項目中,並把 authmode.lsc 放入項目的 assets 文件夾。jar 包的 Build Path 配置方式同 7.1 節。
權限與組件的聲明內容與 7.2 節相同,此處差異在於入口 Activity 需要額外聲明啟動模式與軟鍵盤行為:
<activity
android:name="com.wintone.plateid.MainActivity"
android:configChanges="keyboardHidden|orientation"
android:label="@string/app_name"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustUnspecified|stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
接著打開 res/values/strings.xml,加入以下兩項,內容需替換為用戶實際信息(項目授權會逐一比對這兩個值):
<string name="app_name">快號通開發包</string> <string name="company_name">北京文通科技有限公司</string>
拍照、視頻、相冊三種入口的代碼與 7.4 節相同;識別服務的綁定與回調代碼與 7.5 節相同。由於採用項目授權,無需調用 AuthService。
向文通科技銷售索要 authmode.lsc 文件與 TF 存儲器,把 assets、lib、libs 複製到項目中,並把 authmode.lsc 放入 assets 文件夾。jar 包的 Build Path 配置同前。
與 7.2 節完全一致,注意替換包名。
三種識別入口代碼同 7.4 節。授權文件部分,把示例代碼集成文件夾中「TF 卡授權」目錄裡的授權文件複製到項目的 assets 文件夾內。
TF 卡模式下跳過驗證服務,直接綁定 RecogService 並按 5.2 節的順序調用 getInitPlateIDSDK → setRecogArgu → doRecogDetail 即可。
SIM 卡授權的集成步驟請參照第 7 節序列號激活方式,差異僅在於綁定對象由 deviceid 換為 SIM 卡序號,此時設備必須具備通信能力並能檢測到 SIM 卡,否則會返回 -10500 錯誤碼。
Android Activity 詳解:生命週期、多種啟動方式、狀態保存與完全退出
一、什麼是 Activity?簡單地說,Activity 就是布滿整個視窗、或懸浮於其他視窗之上的互動介面。一個應用程式通常由多個 Activity 構成,且都會在 Ma
Android 元件樣式自訂方法詳解
前言Android 提供了許多現成的元件可以使用,但實際開發中,系統自帶元件的樣式往往與我們 App 的風格不一致。這時候就需要自訂一些樣式。本文將講解如何修改元件的樣式
ViewPager 動態加載 View 並實作偽無限循環的完整做法
ViewPager 大家都很熟,負責的就是左右滑動切頁。這裡要處理的是一個常見需求:頁數不是固定的,而是由資料量決定。典型場景是新聞詳情頁中那排「相關圖片」——有幾張圖就
Android 學習筆記之 ImageView 的 scaleType 屬性詳解
我們知道,ImageView 有一個屬性叫做 scaleType,它的取值一共有八種,分別是:matrix、fitXY、fitStart、fitCenter、fitEnd