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; }
0 comments:
Post a Comment