• 二维码扫描
二维码扫描有很多实用场景,比如BBM通过二维码扫描添加好友、扫描产品二维码标识、扫描二维码ID等等。在Demo应用中使用二维码扫描订单ID,并检查该订单状态。BlackBerry二维码扫描关键的类包括:
BarcodeDecoder - 能够解析1D、2D条形码
BarcodeDecoderListener – 二维码扫描后的回调接口,用户处理获取的二维码
BarcodeScanner – 启动摄像头进行扫描
实现扫描的步骤:
(1). 创建BarcodeDecoder,配置使用哪一种解码,参考ViewFinderScreen的构造函数
// 初始化BarcodeDecoder,配置解码识别为QR编码格式
Hashtable hints = new Hashtable(1);
Vector formats = new Vector(1);
formats.addElement(BarcodeFormat.QR_CODE);
hints.put(DecodeHintType.POSSIBLE_FORMATS, formats);
// 使用配置的解码格式初始化 BarcodeDecoder
BarcodeDecoder decoder = new BarcodeDecoder(hints);
(2). 实现解码识别回调接口 BarcodeDecoderListener,在该接口中处理已获取的二维码,参考ViewFinderScreen构造函数。
// 创建BarcodeDecoderListener来处理识别的QR编码
BarcodeDecoderListener decoderListener = new BarcodeDecoderListener() {
public void barcodeDecoded(final String rawText) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// 处理识别的QR编码
}
}
}
(3). 创建BarcodeScanner,启动扫描,参考ViewFinderScreen构造函数。
try {
// 使用创建的decoder与decoderListener来初始化BarcodeScanner,然后与view
// finder 关联并添加到当前屏幕上。
_scanner = new BarcodeScanner(decoder, decoderListener);
_scanner.getVideoControl().setDisplayFullScreen(true);
add(_scanner.getViewfinder());
} catch (Exception e) {
displayMessage("Error: " + e.getMessage());
}
(4). 初始化完毕,启动扫描,参考ViewFinderScreen.startScan()。
public void startScan() {
try {
_scanner.startScan();// 启动扫描
} catch (MediaException me) {
displayMessage("Error: " + me.getMessage());
}
}
• 获取地理位置、调用Google地图显示位置
地理位置的获取主要通过LocationProvider来实现,主要的类包括:
Criteria – 配置地理位置定位模式,如产生费用、精确度、反应时间、GPS还是基站定位等
LocationProvider – 根据配置的Criteria获取地理位置的经度、纬度
MapField – 通过获取的经度、纬度调用Google地图,显示在MapField上
实现的步骤如下:
(1). 配置地理位置定位标准Criteria,参考LocationThread.setupCriteria()
private void setupCriteria() {
criteria = new Criteria();
criteria.setPreferredResponseTime(15); // 设置相应时间 15s
if (OptionsData.getInstance().getGPSMode() == OptionsData.GPS_GPS) {
// 配置GPS模式
criteria.setCostAllowed(false);
criteria.setHorizontalAccuracy(20); // 20 meter;
Logger.log("Criteria set for Standalone");
} else if (OptionsData.getInstance().getGPSMode() ==
OptionsData.GPS_CELL) {
// 配置基站模式
criteria.setCostAllowed(true);
criteria.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);
criteria.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
Logger.log("Criteria set for Cellsite(s)");
}
}
(2). 配置LocationProvider,参考LocationThread.setupProvider()
private void setupProvider() {
try {
try {
Thread.sleep(2000);
} catch (Throwable e) {}
// 使用criteria配置LocationProvider
provider = LocationProvider.getInstance(criteria);
Logger.log("LocationProvider initialized");
if (provider != null) {
singleFixLocationUpdate();
} else {
Logger.log("Provider unavailable for that Criteria");
}
} catch (LocationException le) {
Logger.log(le.toString());
}
}
(3). 获取地理位置信息、经度、纬度,参考LocationThread.singleFixLocationUpdate()
private void singleFixLocationUpdate() {
Location location = null;
try {
location = provider.getLocation(10000);
} catch (InterruptedException ie) {
Logger.log("InterruptedException thrown by getLocation(): "
+ ie.getMessage());
} catch (LocationException le) {
Logger.log("LocationException thrown by getLocation(): "
+ le.getMessage());
}
if (location != null) {
if (location.isValid()) {
// 获取经度、纬度
double latitude=location.getQualifiedCoordinates().getLatitude();
double longitude=location.getQualifiedCoordinates().getLongitude();
this.doc.setLatitude(latitude);
this.doc.setLongitude(longitude);
this.doc.setHasLocation(true);
String ll=this.doc.getLocationString(); //为Google Map组建位置信息
String url = "http://maps.google.com/maps/api/staticmap?center=" +ll;
url += "&zoom=15";
url += "&size=" + Display.getWidth() + "x250";
url += "&&sensor=false&markers=color:blue|label:C|" +ll;
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run(){
Screen screen =
UiApplication.getUiApplication().getActiveScreen();
if (screen instanceof GPSDocumentScreen){
GPSDocumentScreen realScreen=(GPSDocumentScreen)screen;
realScreen.updateScreen();
}
}
});
} else {
shutdownProvider();
setupProvider();
}
} else {
Logger.log("Location is null");
}
}
(4). 调用Google地图,在MapField中显示,参考GPSDocumentScreen.updateScreen()
public void updateScreen(){
if (doc.isHasLocation()){
longtitudeField.setText("经度:"+doc.getLongitude());
latitudeField.setText("纬度:"+doc.getLatitude());
}
else{
longtitudeField.setText("经度:暂无GPS信息");
latitudeField.setText("纬度:暂无GPS信息");
}
if (this.doc.isHasLocation()){
// 获得Google Map格式的位置信息
String ll=this.doc.getLocationString();
// 组装调用Google Map的URL格式
String url = "http://maps.google.com/maps/api/staticmap?center=" +ll;
url += "&zoom=15";
url += "&size=" + Display.getWidth() + "x250";
url += "&&sensor=false&markers=color:blue|label:C|" +ll;
// 使用MapField调用Google Map,并显示
mapField.requestContent(url);
}
}