二维码扫描效果

生成二维码的两种效果,第一种没有白色背景

/**
	  * 生成二维码图片方法
	  * */
	  public Bitmap Create2DCode(String str) throws WriterException {  
	  BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, 320,320);
	  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] = 0xff000000;  
	  }  
	  }  
	  }  
	  Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);  
	  bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
	return bitmap;  
	 
	  }  

 第二种,有白色背景的效果

	// 生成QR图
//	private void createImage() {//-======================================================================================================================
//		try {
//			// 需要引入core包
//			QRCodeWriter writer = new QRCodeWriter();
//
//			String text = log_username.getText().toString();
//
//			Log.i(TAG, "生成的文本:" + text);
////			if (text == null || "".equals(text) || text.length() < 1) {
////				return;
////			}
//			if (!"".equals(text) && text.length() == 8) {
//			// 把输入的文本转为二维码
//			BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE,
//					QR_WIDTH, QR_HEIGHT);
//
//			System.out.println("w:" + martix.getWidth() + "h:"
//					+ martix.getHeight());
//
//			Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
//			hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//			BitMatrix bitMatrix = new QRCodeWriter().encode(text,
//					BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
//			int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
//			for (int y = 0; y < QR_HEIGHT; y++) {
//				for (int x = 0; x < QR_WIDTH; x++) {
//					if (bitMatrix.get(x, y)) {
//						pixels[y * QR_WIDTH + x] = 0xff000000;
//					} else {
//						pixels[y * QR_WIDTH + x] = 0xffffffff;
//					}
//
//				}
//			}
//
//			Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,
//					Bitmap.Config.ARGB_8888);
//
//			bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
//
//			}

//
//		} catch (WriterException e) {
//			e.printStackTrace();
//		}
//	}

 demo是扫描二维码效果:如

猜你喜欢

转载自274137570-qq-com.iteye.com/blog/2034527