[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();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        Bitmap bm = BitmapFactory.decodeStream(bis);

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

Then you can use this Bitmap to your imageView:

ImageView qrcodeImage = (ImageView) findViewById(R.id.drawQrcode);

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

(Visited 20 times, 1 visits today)
Comments are closed.