How To Generate QR Code In Android

Step1: Download zxing-core-2.0.jar

(paste it in lib folder)

Step2: Add following lines

ImageView imageView = (ImageView) findViewById(R.id.qrcode);
      try {
          Bitmap bitmap = encodeAsBitmap(":" + id + "?");
          imageView.setImageBitmap(bitmap);
      } catch (WriterException e) {
          e.printStackTrace();
      }


Bitmap encodeAsBitmap(String str) throws WriterException {
     BitMatrix result;
     try {
         result = new MultiFormatWriter().encode(str, 
             BarcodeFormat.QR_CODE, 300, 300, null);
     } catch (IllegalArgumentException iae) {
         // Unsupported format
         return null;
     }
     int w = result.getWidth();
     int h = result.getHeight();
     int[] pixels = new int[w * h];
     for (int y = 0; y < h; y++) {
         int offset = y * w;
         for (int x = 0; x < w; x++) {
             pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
         }
     }
     Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
     bitmap.setPixels(pixels, 0, 300, 0, 0, w, h);
     return bitmap;
 }


Share on Google Plus

About Mayank Sharma

Mayadi is a knowlage source. You can learn, explore and play many things in a proper way. Mayadi Provides Best, Perfect and Quality Information. subscribe our youtube channel to be updated and and don't forget to take a look on our blog within every week, because we are here to upgrade your knowladge. We love to see you again and again and again...
    Blogger Comment
    Facebook Comment

0 comments:

Post a comment