[Android] Qrcode generate by Google Chart API

[Android] Qrcode generate by Google Chart API

Recently I need to encode user information to Qrcode format, I search google and want to find any Qrcode generator library that can be used in my app. I found “Zxing Project”  and “onBarcode.com“, those are really useful for me, but those library have a little problem, those can’t encode UTF-8 strings to Qrcode, so I find another way out.


I finally use Google Chart API to encode strings, you could follow below codes step by step to implement Google Chart API in your Android app.

private Bitmap generateQrcode(String input) {
    URL aURL;
    try {
        aURL = new URL("http://chart.apis.google.com/chart? cht=qr&chs=300x300&chld=L&choe=UTF-8&chl=" + 
            URLEncoder.encode(input, "UTF-8"));

        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();

        return bm;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

Then you can use this Bitmap to your imageView:

ImageView qrcodeImage = (ImageView) findViewById(R.id.drawQrcode);
qrcodeImage.setImageBitmap(generateQrcode(vCardString));

It’s very simple to implement, but you need mentioned devices need network to generate Qrcode by google chart api.

(Visited 20 time, 1 visit today)
Facebooktwittergoogle_plusredditpinterestlinkedinmail
Comments are closed.