2014-04-24 55 views
1

我目前在我的Android应用程序中生成QR码,但客户抱怨我们需要生成High error correction的代码。生成适用于Android的QR码,适用于高纠错

我目前使用的代码是下面的,但问题是,我不知道如何真正与某些错误连接生成QR图像:

private Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException { 
     String contentsToEncode = contents; 
     if (contentsToEncode == null) { 
      return null; 
     } 
     Map<EncodeHintType, Object> hints = null; 
     String encoding = guessAppropriateEncoding(contentsToEncode); 
     if (encoding != null) { 
      hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); 
      hints.put(EncodeHintType.CHARACTER_SET, encoding); 
     } 
     MultiFormatWriter writer = new MultiFormatWriter(); 
     BitMatrix result; 
     try { 
      result = writer.encode(contentsToEncode, format, img_width, img_height, hints); 
     } catch (IllegalArgumentException iae) { 
      // Unsupported format 
      return null; 
     } 
     int width = result.getWidth(); 
     int height = result.getHeight(); 
     int[] pixels = new int[width * height]; 
     for (int y = 0; y < height; y++) { 
      int offset = y * width; 
      for (int x = 0; x < width; x++) { 
       pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; 
      } 
     } 

     Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     bitmap.setPixels(pixels, 0, width, 0, 0, width, height); 
     return bitmap; 
    } 

非常感谢了提前帮助!

+0

嗯,什么是BarcodeFormat?这不在Android SDK中。你在使用图书馆吗? – CommonsWare

+0

@CommonsWare我正在使用ZXing – noloman

回答

0

我刚刚添加的行hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);我的方法,以产生高校正水平,并采取一看this,我可以看到生成一个具有较高水平的修正。

0

如果您需要在线qrcode生成器,请使用Google Chart Api。

https://chart.googleapis.com//chart?chs="+ width+"x"+ width+"&cht=qr&chl=yourqrcodetext"