Tuesday, August 21, 2012

Generate QR Code using Google Chart Tools APIs for Android

Google Chart Tools provide a perfect way to visualize data on your website. From simple line charts to complex hierarchical tree maps, the chart galley provides a large number of well-designed chart types. Populating your data is easy using the provided client- and server-side tools.

It's a example of Android app to generate QR Code using Google Chart Tools APIs.

Generate QR Code using Google Chart Tools APIs

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.AndroidQRCode;
 
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
 
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
public class AndroidQRCodeActivity extends Activity {
  
 final static String urlGoogleChart = "http://chart.apis.google.com/chart";
 final static String urlQRApi = "?chs=400x400&cht=qr&chl=";
 final static String urlMySite = "http://android-coding.blogspot.com/";
  
 ImageView QRCode;
 TextView MySite;
  
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       QRCode = (ImageView)findViewById(R.id.qrimage);
       MySite = (TextView)findViewById(R.id.mysite);
       MySite.setText(urlMySite);
       
       Bitmap bm = loadQRCode();
       if(bm == null){
        Toast.makeText(AndroidQRCodeActivity.this,
          "Problem in loading QR Code1",
          Toast.LENGTH_LONG).show();
       }else{
        QRCode.setImageBitmap(bm);
       }
       
   }
   
   private Bitmap loadQRCode(){
    Bitmap bmQR = null;
    InputStream inputStream = null;
     
    try {
   inputStream = OpenHttpConnection(urlGoogleChart + urlQRApi + urlMySite);
   bmQR = BitmapFactory.decodeStream(inputStream);
   inputStream.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    return bmQR;
   }
   
   private InputStream OpenHttpConnection(String strURL) throws IOException{
    InputStream is = null;
    URL url = new URL(strURL);
    URLConnection urlConnection = url.openConnection();
     
    try{
     HttpURLConnection httpConn = (HttpURLConnection)urlConnection;
     httpConn.setRequestMethod("GET");
     httpConn.connect();
      
     if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
      is = httpConn.getInputStream();
     }
     }catch (Exception ex){
     }
     
    return is;
   }
   
}


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<ImageView
   android:id="@+id/qrimage"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<TextView
   android:id="@+id/mysite"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
</LinearLayout>


Permission: "android.permission.INTERNET" is needed.

No comments:

Post a Comment