lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2012年01月31日

Android4.0系统浏览器支持window.performance API了

Filed under: Android,JavaScript,Web,前端开发 — 标签:, , — lizongbo @ 22:25

Android4.0系统浏览器支持window.performance API了

window.performance API是HTML5规范的一部分,
window.performance规范的官方文档在:
https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming/Overview.html
目前还是草稿阶段,中文介绍可以参考:
http://www.cnblogs.com/_franky/archive/2011/11/07/2238980.html
webkit的测试用例和属性列表可以参考:
http://svn.webkit.org/repository/webkit/trunk/LayoutTests/fast//dom/Window/window-properties-performance.html
http://svn.webkit.org/repository/webkit/trunk/LayoutTests/fast//dom/Window/window-properties-performance-expected.txt

Android4.0系统自带的浏览器实现了这一规范,并且还扩展实现window.performance.memory。

通过JavaScript实际测试结果如下:
window.performance.memory=[object MemoryInfo]
window.performance.timing=[object PerformanceTiming]
window.performance.navigation=[object PerformanceNavigation]
window.performance.timing.connectStart=1328016452403
window.performance.timing.responseStart=1328016452403
window.performance.timing.domLoading=1328016453857
window.performance.timing.connectEnd=1328016452403
window.performance.timing.domInteractive=0
window.performance.timing.fetchStart=1328016452403
window.performance.timing.secureConnectionStart=0
window.performance.timing.domainLookupStart=1328016452403
window.performance.timing.responseEnd=0
window.performance.timing.requestStart=1328016452403
window.performance.timing.loadEventEnd=0
window.performance.timing.domComplete=0
window.performance.timing.redirectStart=0
window.performance.timing.unloadEventEnd=1328016453646
window.performance.timing.domContentLoadedEventStart=0
window.performance.timing.domContentLoadedEventEnd=0
window.performance.timing.domainLookupEnd=1328016452403
window.performance.timing.navigationStart=1328016452402
window.performance.timing.unloadEventStart=1328016453646
window.performance.timing.loadEventStart=0
window.performance.timing.redirectEnd=0
window.performance.navigation.redirectCount=0
window.performance.navigation.type=1
window.performance.navigation.TYPE_NAVIGATE=0
window.performance.navigation.TYPE_RELOAD=1
window.performance.navigation.TYPE_BACK_FORWARD=2
window.performance.navigation.TYPE_RESERVED=255
window.performance.memory.totalJSHeapSize=0
window.performance.memory.usedJSHeapSize=0
window.performance.memory.jsHeapSizeLimit=0

PS:Android从2.1版本还扩展实现了navigator.connection:
通过JavaScript测试结果如下:
navigator.connection.type=4
navigator.connection.UNKNOWN=0
navigator.connection.ETHERNET=1
navigator.connection.WIFI=2
navigator.connection.CELL_2G=3
navigator.connection.CELL_3G=4

 

2011年05月5日

从google的json接口获取地理位置信息的Java代码

Filed under: IP,Java,JavaScript — 标签:, , , , , — lizongbo @ 22:55

从google的json接口获取地理位置信息的Java代码

google 提供了公开的接口,通过,这个接口,根据gps或基站信息或wifi热点信息来获取当前位置的地理信息.
gps信息是经纬度,基站信息是基站的cellid等信息,wifi热点信息是wifi的mac地址是热点名字,信号强度等。
因此可以通过java程序获取相应信息,在LBS项目中可以用上:

api接口定义描述在:

http://code.google.com/p/gears/wiki/GeolocationAPI

Java代码如下:

[code]

package com.lizongbo.geo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

import com.lizongbo.org.json.JSONArray;
import com.lizongbo.org.json.JSONException;
import com.lizongbo.org.json.JSONObject;

/**
* 从google的json接口获取地理位置信息的代码
*
* @author lizongbo
*
*/
public class GeoTest {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String url = “http://www.google.com/loc/json”;
JSONObject json = new JSONObject();
json.put(“version”, “1.1.0”);
json.put(“host”, “maps.618119.com”);
json.put(“home_mobile_country_code”, 460);// 国家代码
json.put(“home_mobile_network_code”, 0);// 移动运营商代码
json.put(“radio_type”, “gsm”);
json.put(“carrier”, “lizongbo”);
json.put(“request_address”, true);
json.put(“address_language”, “zh_CN”);
JSONArray jsoncells = new JSONArray();
json.put(“cell_towers”, jsoncells);
JSONArray jsonwifis = new JSONArray();
json.put(“wifi_towers”, jsonwifis);
JSONObject jsoncell = new JSONObject();
jsoncell.put(“mobile_country_code”, 460);// 国家代码,mcc
jsoncell.put(“mobile_network_code”, 0);// 移动运营商代码,mnc
jsoncell.put(“location_area_code”, 9364);// 位置区域代码,lac
jsoncell.put(“cell_id”, “3851”);// 移动基站id
// jsoncell.put(“age”, 0);
// jsoncell.put(“signal_strength”, -70);
// jsoncell.put(“timing_advance”, 7777);
jsoncells.put(jsoncell);
JSONObject jsonwifi = new JSONObject();
// jsonwifi.put(“mac_address”, “00-11-22-33-44-55”);
// jsonwifi.put(“signal_strength”, 8);
// jsonwifi.put(“age”, 0);
// jsonwifis.put(jsonwifi);
// jsonwifi = new JSONObject();
jsonwifi.put(“mac_address”, “00-55-44-33-22-11”);//
jsonwifi.put(“ssid”, “TPLINK_618119”);// 无线路由器的名字
jsonwifi.put(“signal_strength”, 8);// 信号强度
jsonwifi.put(“age”, 0);
// jsonwifis.put(jsonwifi);

System.out.println(json.toString());
System.out.println(downloadUrlbyPOST(url, json.toString(), null,
“UTF-8”));

}

public static String downloadUrlbyPOST(String urlStr, String query,
String referer, String encoding) throws Exception {
String line = “”;
StringBuilder sb = new StringBuilder();
HttpURLConnection httpConn = null;
try {
URL url = new URL(urlStr);
System.out.println(urlStr + “?” + query);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
“proxy.lizongbo.com”, 8080));
proxy = Proxy.NO_PROXY;
httpConn = (HttpURLConnection) url.openConnection(proxy);
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setRequestMethod(“POST”);
if (referer != null) {
httpConn.setRequestProperty(“Referer”, referer);
}
httpConn.setConnectTimeout(5000);
// httpConn.getOutputStream().write(
// java.net.URLEncoder.encode(query, “UTF-8”).getBytes());
httpConn.getOutputStream().write(query.getBytes());
httpConn.getOutputStream().flush();
httpConn.getOutputStream().close();

BufferedReader in = null;
if (httpConn.getResponseCode() != 200) {
System.err.println(“error:” + httpConn.getResponseMessage());
in = new BufferedReader(new InputStreamReader(httpConn
.getErrorStream(), “UTF-8”));
} else {
in = new BufferedReader(new InputStreamReader(httpConn
.getInputStream(), “UTF-8”));
}
while ((line = in.readLine()) != null) {
sb.append(line).append(‘\n’);
}
// 关闭连接
httpConn.disconnect();
return sb.toString();
} catch (Exception e) {
// 关闭连接
httpConn.disconnect();
System.out.println(e.getMessage());
throw e;
}
}
}

[/code]
运行结果如下:
{“address_language”:”zh_CN”,”wifi_towers”:[],”host”:”maps.618119.com”,”radio_type”:”gsm”,”home_mobile_country_code”:9364,”carrier”:”lizongbo”,”home_mobile_network_code”:460,”cell_towers”:[{“mobile_network_code”:0,”cell_id”:”3851″,”mobile_country_code”:460,”location_area_code”:9364}],”request_address”:true,”version”:”1.1.0″}
http://www.google.com/loc/json?{“address_language”:”zh_CN”,”wifi_towers”:[],”host”:”maps.618119.com”,”radio_type”:”gsm”,”home_mobile_country_code”:9364,”carrier”:”lizongbo”,”home_mobile_network_code”:460,”cell_towers”:[{“mobile_network_code”:0,”cell_id”:”3851″,”mobile_country_code”:460,”location_area_code”:9364}],”request_address”:true,”version”:”1.1.0″}
{“location”:{“latitude”:22.522185,”longitude”:113.925618,”address”:{“country”:”中国”,”country_code”:”CN”,”region”:”广东省”,”city”:”深圳市”,”street”:”滨海大道辅路”},”accuracy”:620.0},”access_token”:”2:618119:lizongbo”}

试了一下把无线路由器的mac地址发到 https://www.google.com/loc/json ,google果然返回很准的定位看来google是把我用手机测试定位的时候就把数据采集分析好了,确实强大。

2010年11月23日

使用zxing生成QRCode二维码,在Android的webview中调用条形码扫描

Filed under: Android,JavaScript,前端开发 — 标签:, , , , — lizongbo @ 21:41

使用zxing生成QRCode二维码,在Android的webview中调用条形码扫描
zxing是一个条形码和二维码生成及识别的java开源组件,QRcode的中文名字是:QR快速响应矩阵码。
1.下载zxing最新的包
到zxing的主页: http://code.google.com/p/zxing/
下载 http://zxing.googlecode.com/files/ZXing-1.6.zip
http://zxing.googlecode.com/files/BarcodeScanner3.52.apk
来源:http://code.google.com/p/zxing/downloads/list
其中BarcodeScanner3.5.2.apk是安装在Android操作系统的手机上的条码扫描器安装包。
条码扫描器的j2me版本下载地址为:
http://zxing.org/w/BarcodeReader.jad
http://zxing.org/w/BarcodeReader.jar

解压为:
D:\Java\zxing-1.6

在dos窗口下进入到:D:\Java\zxing-1.6\core,然后运行ant命令进行编译
(必须先安装配置了ant。
ant最新版的下载地址为:http://ant.apache.org/bindownload.cgi
下载解压后在系统的环境变量中配置ANT_HOME:例如:ANT_HOME=D:\Java\apache-ant-1.7.0;
Path中必须有ant的bin目录,例如:
Path=D:\Java\jdk1.6.0_21\bin;D:\Java\apache-maven-2.0.9\bin;D:\Perl\site\bin;D:\
Perl\bin;D:\Java\apache-ant-1.7.0\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\
System32\Wbem;D:\Programs\svn-win32-1.5.4\bin;C:\Program Files\TortoiseSVN\bin;C
:\Program Files\QuickTime\QTSystem\
)
编译结果如下:
D:\Java\zxing-1.6\core>ant
Buildfile: build.xml

clean:

build:

init:

compile:
[mkdir] Created dir: D:\Java\zxing-1.6\core\build
[javac] Compiling 171 source files to D:\Java\zxing-1.6\core\build
[jar] Building jar: D:\Java\zxing-1.6\core\core.jar

BUILD SUCCESSFUL
Total time: 6 seconds

编译之后得到core.jar这个包。
再进入D:\Java\zxing-1.6\javase目录使用ant进行编译,编译结果如下:
D:\Java\zxing-1.6\javase>ant
Buildfile: build.xml

init:

build:
[mkdir] Created dir: D:\Java\zxing-1.6\javase\build
[javac] Compiling 6 source files to D:\Java\zxing-1.6\javase\build
[jar] Building jar: D:\Java\zxing-1.6\javase\javase.jar

BUILD SUCCESSFUL
Total time: 1 second
编译之后得到javase.jar这个包。

在Eclipse里新建Java工程:

编写java代码如下:
package com.lizongbo.qrcode;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class QRCodeGen {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;

/**
* @param args
* @throws WriterException
* @throws IOException
*/
public static void main(String[] args) throws WriterException, IOException {
String picFormat = “png”;
StringBuilder sb = new StringBuilder(1024);
java.io.InputStreamReader fr = new InputStreamReader(
new FileInputStream(“d:\\quickli.vcf”), “GBK”);
BufferedReader br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line).append(“\r\n”);
}
String str = sb.toString();// 二维码内容
System.out.println(str.length() + “|str==” + str);
str = new String(str.getBytes(“GBK”), “ISO-8859-1”);
String path = “d:/lzb”;
Hashtable hints = new Hashtable();
// hints.put(EncodeHintType.CHARACTER_SET, “GBK”);
BitMatrix bitMatrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, 400, 400, hints);
/**
* begin 这段代码等同于就是 MatrixToImageWriter.writeToFile(bitMatrix, picFormat,
* file);
* 直接这样写就不用引用javase.jar
*/
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE); } } File file = new File(path + "." + picFormat); ImageIO.write(image, picFormat, file); /** * end 这段代码等同于就是 MatrixToImageWriter.writeToFile(bitMatrix, picFormat, * file); */ MatrixToImageWriter.writeToFile(bitMatrix, picFormat, file); } } 网址的QRcode生成如果不想编写代码,可以使用现成的google的api: http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=http://zxing.googlecode.com/files/BarcodeScanner3.5.2.apk&chld=L|1&choe=UTF-8 Vcard也可以生成QRcode: 参考:http://blog.yslifes.com/archives/686 在线对二维码进行解码的url: http://zxing.org/w/decode.jspx 在Webview中通过js也可以调用条形码扫描, 封装的java代码如下: final class BarCodeUtilHandler { public void tryGetBarCode() { Intent intent = new Intent("com.google.zxing.client.android.SCAN"); // intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); startActivityForResult(intent, 0); } }; @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { Log.v(TAG, "onActivityResult===" + requestCode + "," + resultCode + "," + intent); if (requestCode == 0) { if (resultCode == RESULT_OK) { String contents = intent.getStringExtra("SCAN_RESULT"); String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); webview.loadUrl("javascript:showBarCode(\'" + format + "|" + contents + "\')"); } else if (resultCode == RESULT_CANCELED) { webview.loadUrl("javascript:showBarCode(\'没有找到条码!\')"); } } } //webview.addJavascriptInterface(new BarCodeUtilHandler(), "barcodeutil"); //javascript调用:window.barcodeutil.tryGetBarCode(); //function showBarCode(str) { // try { // document.getElementById('data').value=str; // } catch (ee) { // } //}

Older Posts »

Powered by WordPress