コード例 #1
0
  public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded) return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
      hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
      hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    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;
  }
コード例 #2
0
ファイル: GeneratorQR.java プロジェクト: wildeivid/fermat
  /**
   * Encode a string into a QR Code and return a bitmap image of the QR code
   *
   * @param contentsToEncode String to be encoded, this will often be a URL, but could be any string
   * @param imageWidth number of pixels in width for the resultant image
   * @param imageHeight number of pixels in height for the resultant image
   * @param marginSize the EncodeHintType.MARGIN parameter into zxing engine
   * @param color data color for QR code
   * @param colorBack background color for QR code
   * @return bitmap containing QR code image
   * @throws com.google.zxing.WriterException zxing engine is unable to create QR code data
   * @throws IllegalStateException when executed on the UI thread
   */
  public static Bitmap generateBitmap(
      @NonNull String contentsToEncode,
      int imageWidth,
      int imageHeight,
      int marginSize,
      int color,
      int colorBack)
      throws WriterException, IllegalStateException {

    if (Looper.myLooper() == Looper.getMainLooper()) {
      // throw new IllegalStateException("Should not be invoked from the UI thread");
    }

    Map<EncodeHintType, Object> hints = null;
    if (marginSize != MARGIN_AUTOMATIC) {
      hints = new EnumMap<>(EncodeHintType.class);
      // We want to generate with a custom margin size
      hints.put(EncodeHintType.MARGIN, marginSize);
    }

    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result =
        writer.encode(contentsToEncode, BarcodeFormat.QR_CODE, imageWidth, imageHeight, hints);

    final int width = result.getWidth();
    final 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) ? color : colorBack;
      }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
  }
コード例 #3
0
 public Bitmap generateQR(String text) {
   Display display = getWindowManager().getDefaultDisplay();
   Point size = new Point();
   display.getSize(size);
   int width = size.x * 3 / 4;
   int height = width;
   MultiFormatWriter writer = new MultiFormatWriter();
   Hashtable hints = new Hashtable();
   hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
   BitMatrix matrix = null;
   Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
   try {
     matrix = writer.encode(text, BarcodeFormat.QR_CODE, width, height, hints);
   } catch (WriterException e) {
     return bmp;
   }
   for (int x = 0; x < width; x++) {
     for (int y = 0; y < height; y++) {
       bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE);
     }
   }
   return bmp;
 }