SwetakeQRCode、BarCode4j、Zxing…… 生成二維碼的開源項很多,在下不才只目前只了解這幾個, 選擇Zxing的原因可能是因為google吧,還一個就是Zxing網絡上解決方案比較多
首先要在build.gradle中添加 compile ‘com.google.zxing:core:3.2.1’ Zxing官網地址點我
PRivate void Create2QR2(String urls,ImageView imageView) { String uri = urls; int mScreenWidth = 0; Bitmap bitmap; try { /** * 獲取屏幕信息的區別 * 只有activity可以使用WindowManager否則應該使用Context.getResources().getDisplayMetrics()來獲取。 * Context.getResources().getDisplayMetrics()依賴于手機系統,獲取到的是系統的屏幕信息; * WindowManager.getDefaultDisplay().getMetrics(dm)是獲取到Activity的實際屏幕信息。 */ DisplayMetrics dm = new DisplayMetrics(); aty.getWindowManager().getDefaultDisplay().getMetrics(dm); mScreenWidth = dm.widthPixels; bitmap = BitmapUtil.createQRImage(uri, mScreenWidth, BitmapFactory.decodeResource(getResources(), R.mipmap.applogo));//自己寫的方法 if (bitmap != null) { imageView.setImageBitmap(bitmap); } } catch (Exception e) { e.printStackTrace(); } }//生成二維碼圖片(不帶圖片)public static Bitmap createQRCode(String url, int widthAndHeight) throws WriterException { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight); int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; //畫黑點 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix.get(x, y)) { pixels[y * width + x] = BLACK; //0xff000000 } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }帶圖片的二維碼public static Bitmap createQRImage(String content, int heightPix, Bitmap logoBm) { try {// if (content == null || "".equals(content)) {// return false;// } //配置參數 Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //容錯級別 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //設置空白邊距的寬度 // hints.put(EncodeHintType.MARGIN, 2); //default is 4 // 圖像數據轉換,使用了矩陣轉換 BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, heightPix, heightPix, hints); int[] pixels = new int[heightPix * heightPix]; // 下面這里按照二維碼的算法,逐個生成二維碼的圖片, // 兩個for循環是圖片橫列掃描的結果 for (int y = 0; y < heightPix; y++) { for (int x = 0; x < heightPix; x++) { if (bitMatrix.get(x, y)) { pixels[y * heightPix + x] = 0xff000000; } else { pixels[y * heightPix + x] = 0xffffffff; } } } // 生成二維碼圖片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(heightPix, heightPix, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, heightPix, 0, 0, heightPix, heightPix); if (logoBm != null) { bitmap = addLogo(bitmap, logoBm); } //必須使用compress方法將bitmap保存到文件中再進行讀取。直接返回的bitmap是沒有任何壓縮的,內存消耗巨大! return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; } /** * 在二維碼中間添加Logo圖案 */ private static Bitmap addLogo(Bitmap src, Bitmap logo) { if (src == null) { return null; } if (logo == null) { return src; } //獲取圖片的寬高 int srcWidth = src.getWidth(); int srcHeight = src.getHeight(); int logoWidth = logo.getWidth(); int logoHeight = logo.getHeight(); if (srcWidth == 0 || srcHeight == 0) { return null; } if (logoWidth == 0 || logoHeight == 0) { return src; } //logo大小為二維碼整體大小的1/5 float scaleFactor = srcWidth * 1.0f / 5 / logoWidth; Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888); try { Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(src, 0, 0, null); canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2); canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null); canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); } catch (Exception e) { bitmap = null; e.getStackTrace(); } return bitmap; }新聞熱點
疑難解答