Google Chart API 之生成二维码

来源:互联网 发布:福建三千万美元知乎 编辑:程序博客网 时间:2024/03/29 04:53


完成源代码下载http://download.csdn.net/detail/wujiaqi168/6764395


Google提供的网络服务API中,最受好评的莫过于Google Chart API了。只要通过它,程序设计人员不需要具备太高深的功力,也不需要具备GD library的知识,就能够做出漂亮的图表、专业的分析报表。

Google chart API 地址如下:


http://chart.apis.google.com/chart


Google chart  API 提供下列类型的图表。

LIne charts

Bar charts

Pie charts

Venn diagrams 

Scatter plots

Radar charts

Maps

Google-0-meters

QR codes


Google chart API 提供的请求参数如下:

* cht(chart type):图表种类,cht=p3表示生成3D饼图。

* chs(chart size):图表面积,chs=250x100表示宽200像素,高100像素。

* chtt(chart title):图表标题,chtt=Hello+World表示标题是Hello World。

* chd(chart data):图表数据,chd=s:hW表示数据是普通字符串(simple string)hW。


下面我将就利用Google Chart API 来生成QR codes 二维码举例,来向大家展示如何使用Google chart API:

在布局文件中,我简单地设计了edittext,button,webview三个组件,edittext用来输入生成二维码中包含的信息,button用来点击产生二维码,webview用来显示生成的二维码。

布局文件main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:background="@drawable/white" 
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
    <EditText
        android:id="@+id/myEditText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal" >
        <requestFocus />
    </EditText>
    <Button
       android:id="@+id/myButton1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
     <WebView 
  android:id="@+id/myWebView1" 
  android:background="@drawable/white" 
  android:layout_height="wrap_content" 
  android:layout_width="fill_parent"
  />


</LinearLayout>


主程序代码如下:

package Demo.GoogleChartAPITest;
import java.net.URLEncoder;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;


public class GoogleChartAPITestActivity extends Activity {

private Button mButton01;
private EditText mEditText01;
private WebView mWebView01;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mWebView01 = (WebView)findViewById(R.id.myWebView1);
        
        mButton01 = (Button)findViewById(R.id.myButton1);
        mEditText01 = (EditText)findViewById(R.id.myEditText1);
        
        mButton01.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

///Log.i("halfcool", mEditText01.getText().toString());

if (mEditText01.getText().toString()!="") {

//Log.i("halfcool", mEditText01.getText().toString());
/*webview加载数据*/
mWebView01.loadData(GenGoogleQRChart(mEditText01.getText().toString(),250), "text/html", "utf-8");

}

    }
});
        
    }


protected String GenGoogleQRChart(String strToQRCode, int strWidth) {

String strReturn = "";

try {

strReturn = new String(strToQRCode.getBytes("utf-8"));
strReturn = "<html><body>" + "<img src=http://chart.apis.google.com/chart?chs="
+ strWidth + "x" +strWidth + "&chl=" +
URLEncoder.encode(strReturn, "utf-8") +
"&choe=UTF-8&cht=qr></body><html>";

}

 catch (Exception e) {

e.printStackTrace();

}

return strReturn;
}
}







完成源代码下载http://download.csdn.net/detail/wujiaqi168/6764395
0 0
原创粉丝点击