示例#1
0
  public static Bitmap bitmap(@Nonnull final String content, final int size) {
    try {
      final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
      hints.put(EncodeHintType.MARGIN, 0);
      hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
      final BitMatrix result =
          QR_CODE_WRITER.encode(content, BarcodeFormat.QR_CODE, size, size, hints);

      final int width = result.getWidth();
      final int height = result.getHeight();
      final int[] pixels = new int[width * height];

      for (int y = 0; y < height; y++) {
        final int offset = y * width;
        for (int x = 0; x < width; x++) {
          pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT;
        }
      }

      final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
      return bitmap;
    } catch (final WriterException x) {
      log.info("problem creating qr code", x);
      return null;
    }
  }
示例#2
0
 public static Bitmap cretaeBitmap(Context c, String str, Bitmap mBitmap) throws WriterException {
   // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
   int qr_width = ScreenUtils.getScreenWidth(c) * 2 / 3;
   int qr_height = qr_width;
   BitMatrix matrix =
       new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, qr_width, qr_height);
   //        BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 300,
   // 300);//如果要指定二维码的边框以及容错率,最好给encode方法增加一个参数:hints 一个Hashmap
   int width = matrix.getWidth();
   int height = matrix.getHeight();
   // 二维矩阵转为一维像素数组,也就是一直横着排了
   int halfW = width / 2;
   int halfH = height / 2;
   int[] pixels = new int[width * height];
   for (int y = 0; y < height; y++) {
     for (int x = 0; x < width; x++) {
       if (x > halfW - Const.IMAGE_HALFWIDTH
           && x < halfW + Const.IMAGE_HALFWIDTH
           && y > halfH - Const.IMAGE_HALFWIDTH
           && y < halfH + Const.IMAGE_HALFWIDTH) {
         pixels[y * width + x] =
             mBitmap.getPixel(
                 x - halfW + Const.IMAGE_HALFWIDTH, y - halfH + Const.IMAGE_HALFWIDTH);
       } else {
         // 此处可以修改二维码的颜色,可以分别制定二维码和背景的颜色;
         pixels[y * width + x] = matrix.get(x, y) ? 0xff000000 : 0xfffffff;
       }
     }
   }
   Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
   // 通过像素数组生成bitmap
   bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
   return bitmap;
 }
示例#3
0
  public static Bitmap getMinimalQRCodeBitmap(String url) {
    Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
    hints.put(EncodeHintType.MARGIN, 5);

    try {
      final BitMatrix result = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, 0, 0, hints);

      final int width = result.getWidth();
      final int height = result.getHeight();
      final int[] pixels = new int[width * height];

      for (int y = 0; y < height; y++) {
        final int offset = y * width;
        for (int x = 0; x < width; x++) {
          pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
        }
      }

      final Bitmap smallBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      smallBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
      return smallBitmap;
    } catch (final WriterException x) {
      x.printStackTrace();
      return null;
    }
  }
  private static BitMatrix extractPureBits(BitMatrix bitmatrix) throws NotFoundException {
    int ai[] = bitmatrix.getTopLeftOnBit();
    int ai1[] = bitmatrix.getBottomRightOnBit();
    if (ai == null || ai1 == null) {
      throw NotFoundException.getNotFoundInstance();
    }
    int l = moduleSize(ai, bitmatrix);
    int i1 = ai[1];
    int i = ai1[1];
    int j1 = ai[0];
    int k1 = ((ai1[0] - j1) + 1) / l;
    int l1 = ((i - i1) + 1) / l;
    if (k1 <= 0 || l1 <= 0) {
      throw NotFoundException.getNotFoundInstance();
    }
    int i2 = l >> 1;
    BitMatrix bitmatrix1 = new BitMatrix(k1, l1);
    for (int j = 0; j < l1; j++) {
      for (int k = 0; k < k1; k++) {
        if (bitmatrix.get(k * l + (j1 + i2), i1 + i2 + j * l)) {
          bitmatrix1.set(k, j);
        }
      }
    }

    return bitmatrix1;
  }
示例#5
0
  public BufferedImage getQRCode(String codeText, Integer size) {
    try {
      Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
      hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
      QRCodeWriter qrCodeWriter = new QRCodeWriter();
      byteMatrix = qrCodeWriter.encode(codeText, BarcodeFormat.QR_CODE, size, size, hintMap);
      int imageWidth = byteMatrix.getWidth();
      image = new BufferedImage(imageWidth, imageWidth, BufferedImage.TYPE_INT_RGB);
      image.createGraphics();

      Graphics2D graphics = (Graphics2D) image.getGraphics();
      graphics.setColor(Color.WHITE);
      graphics.fillRect(0, 0, imageWidth, imageWidth);
      graphics.setColor(Color.BLACK);

      for (int i = 0; i < imageWidth; i++) {
        for (int j = 0; j < imageWidth; j++) {
          if (byteMatrix.get(i, j)) {
            graphics.fillRect(i, j, 1, 1);
          }
        }
      }
      return image;
    } catch (WriterException e) {
      return null;
    }
  }
 // 要转换的地址或字符串,可以是中文
 public void createQRImage(String url) {
   try {
     // 判断URL合法性
     if (url == null || "".equals(url) || url.length() < 1) {
       return;
     }
     Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
     hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
     // 图像数据转换,使用了矩阵转换
     BitMatrix bitMatrix =
         new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
     int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
     // 下面这里按照二维码的算法,逐个生成二维码的图片,
     // 两个for循环是图片横列扫描的结果
     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;
         }
       }
     }
     // 生成二维码图片的格式,使用ARGB_8888
     Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
     bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
     // 显示到一个ImageView上面
     sweepIV.setImageBitmap(bitmap);
   } catch (WriterException e) {
     e.printStackTrace();
   }
 }
示例#7
0
  private static MatrixToImageResult matrixToImage(BitMatrix matrix, BarcodeFormat type) {
    if (matrix != null) {
      try {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

        int onColor = 0xFF000000;
        int offColor = 0xFFFFFFFF;
        int[] pixels = new int[width * height];
        int index = 0;
        for (int y = 0; y < height; y++) {
          for (int x = 0; x < width; x++) {
            pixels[index++] = matrix.get(x, y) ? onColor : offColor;
          }
        }
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

        File file =
            new File(
                Extension.mainContext.getApplicationInfo().dataDir,
                "code_" + type.toString() + ".jpg");
        FileOutputStream out = new FileOutputStream(file);
        if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
          out.flush();
          out.close();
          return new MatrixToImageResult(true, file.getAbsolutePath());
        } else return new MatrixToImageResult(false, null);
      } catch (Exception e) {
        e.printStackTrace();
        return new MatrixToImageResult(false, null);
      }
    }
    return new MatrixToImageResult(false, null);
  }
  public final BitMatrix sampleGrid(
      BitMatrix bitmatrix, int i, int j, PerspectiveTransform perspectivetransform)
      throws NotFoundException {
    if (i <= 0 || j <= 0) {
      throw NotFoundException.getNotFoundInstance();
    }
    BitMatrix bitmatrix1 = new BitMatrix(i, j);
    float af[] = new float[i << 1];
    for (i = 0; i < j; i++) {
      int i1 = af.length;
      float f = i;
      for (int k = 0; k < i1; k += 2) {
        af[k] = (float) (k >> 1) + 0.5F;
        af[k + 1] = f + 0.5F;
      }

      perspectivetransform.transformPoints(af);
      checkAndNudgePoints(bitmatrix, af);
      int l = 0;
      while (l < i1) {
        try {
          if (bitmatrix.get((int) af[l], (int) af[l + 1])) {
            bitmatrix1.set(l >> 1, i);
          }
        }
        // Misplaced declaration of an exception variable
        catch (BitMatrix bitmatrix) {
          throw NotFoundException.getNotFoundInstance();
        }
        l += 2;
      }
    }

    return bitmatrix1;
  }
  private Bitmap createBitmap(String content, final int size) {
    final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
    hints.put(EncodeHintType.MARGIN, 0);
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    BitMatrix result;
    try {
      result = sQRCodeWriter.encode(content, BarcodeFormat.QR_CODE, size, size, hints);
    } catch (WriterException ex) {
      mLogger.warn("qr encoder failed: " + ex.toString());
      return null;
    }

    final int width = result.getWidth();
    final int height = result.getHeight();
    final int[] pixels = new int[width * height];

    for (int y = 0; y < height; y++) {
      final int offset = y * width;
      for (int x = 0; x < width; x++) {
        pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT;
      }
    }

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

    return bitmap;
  }
  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;
  }
示例#11
0
  public Bitmap encodeAsBitmap() 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.class);
      hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    BitMatrix result;
    try {
      result =
          new MultiFormatWriter().encode(contentsToEncode, format, dimension, dimension, 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;
  }
 protected Bitmap createQrCodeBitmap(String input, int size) {
   Log.d(Config.LOGTAG, "qr code requested size: " + size);
   try {
     final QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
     final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
     hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
     final BitMatrix result =
         QR_CODE_WRITER.encode(input, BarcodeFormat.QR_CODE, size, size, hints);
     final int width = result.getWidth();
     final int height = result.getHeight();
     final int[] pixels = new int[width * height];
     for (int y = 0; y < height; y++) {
       final int offset = y * width;
       for (int x = 0; x < width; x++) {
         pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT;
       }
     }
     final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
     Log.d(Config.LOGTAG, "output size: " + width + "x" + height);
     bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
     return bitmap;
   } catch (final WriterException e) {
     return null;
   }
 }
 private void testMask(DataMask mask, int dimension, MaskCondition condition) {
   BitMatrix bits = new BitMatrix(dimension);
   mask.unmaskBitMatrix(bits, dimension);
   for (int i = 0; i < dimension; i++) {
     for (int j = 0; j < dimension; j++) {
       assertEquals("(" + i + ',' + j + ')', condition.isMasked(i, j), bits.get(j, i));
     }
   }
 }
 private static BufferedImage toBufferedImage(BitMatrix matrix) {
   int width = matrix.getWidth();
   int height = matrix.getHeight();
   BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
   for (int x = 0; x < width; x++) {
     for (int y = 0; y < height; y++) {
       image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
     }
   }
   return image;
 }
 {
     if (i > j)
     {
         break; /* Loop/switch isn't completed */
     }
     if (image.get(k, i))
     {
         continue; /* Loop/switch isn't completed */
     }
     i++;
 } while (true);
示例#16
0
  public void encode(String str, String path, int width, int height) {
    try {
      Map hints = new HashMap();
      hints.put(EncodeHintType.MARGIN, 0);
      // hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
      hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
      hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
      BitMatrix byteMatrix =
          new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, width, height, hints);

      /** tianboalei 2015-1-22 去掉生成二维码时周围的白圈 */
      // 1
      int[] rec = byteMatrix.getEnclosingRectangle();
      int resWidth = rec[2] + 1;
      int resHeight = rec[3] + 1;
      BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
      resMatrix.clear();
      for (int i = 0; i < resWidth; i++) {
        for (int j = 0; j < resHeight; j++) {
          if (byteMatrix.get(i + rec[0], j + rec[1])) {
            resMatrix.set(i, j);
          }
        }
      }
      // 2
      int widthT = resMatrix.getWidth();
      int heightT = resMatrix.getHeight();
      BufferedImage image = new BufferedImage(widthT, heightT, BufferedImage.TYPE_INT_ARGB);
      for (int x = 0; x < widthT; x++) {
        for (int y = 0; y < heightT; y++) {
          image.setRGB(
              x, y, resMatrix.get(x, y) == true ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
        }
      }
      /** 去掉二维码周围白圈结束 */
      File file = new File(path);
      writeToFile(resMatrix, "png", file);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
示例#17
0
 /**
  * Convert the symbols in the row to codewords. Each PDF417 symbol character consists of four bar
  * elements and four space elements, each of which can be one to six modules wide. The four bar
  * and four space elements shall measure 17 modules in total.
  *
  * @param rowNumber the current row number of codewords.
  * @param codewords the codeword array to save codewords into.
  * @param next the next available index into the codewords array.
  * @return the next available index into the codeword array after processing this row.
  */
 int processRow(int rowNumber, int[] codewords, int next) throws FormatException {
   int width = bitMatrix.getWidth();
   int columnNumber = 0;
   long symbol = 0;
   for (int i = 0; i < width; i += MODULES_IN_SYMBOL) {
     for (int mask = MODULES_IN_SYMBOL - 1; mask >= 0; mask--) {
       if (bitMatrix.get(i + (MODULES_IN_SYMBOL - 1 - mask), rowNumber)) {
         symbol |= 1L << mask;
       }
     }
     if (columnNumber > 0) {
       int cw = getCodeword(symbol);
       if (cw < 0 && i < width - MODULES_IN_SYMBOL) {
         // Skip errors on the Right row indicator column
         if (eraseCount >= erasures.length) {
           throw FormatException.getFormatInstance();
         }
         erasures[eraseCount] = next;
         next++;
         eraseCount++;
       } else {
         if (next >= codewords.length) {
           throw FormatException.getFormatInstance();
         }
         codewords[next++] = cw;
       }
     } else {
       // Left row indicator column
       int cw = getCodeword(symbol);
       if (ecLevel < 0 && rowNumber % 3 == 1) {
         leftColumnECData = cw;
       }
     }
     symbol = 0;
     // columns = columnNumber;
     columnNumber++;
   }
   if (columnNumber > 1) {
     // Right row indicator column is in codeword[next]
     // columns--;
     // Overwrite the last codeword i.e. Right Row Indicator
     --next;
     if (ecLevel < 0 && rowNumber % 3 == 2) {
       rightColumnECData = codewords[next];
       if (rightColumnECData == leftColumnECData && leftColumnECData > 0) {
         // ecLevel = ((rightColumnECData % 30) - rows % 3) / 3;
         ecLevel = (rightColumnECData % 30) / 3;
       }
     }
     codewords[next] = 0;
   }
   return next;
 }
  /**
   * Counts the number of black/white transitions between two points, using something like
   * Bresenham's algorithm.
   */
  private ResultPointsAndTransitions transitionsBetween(ResultPoint from, ResultPoint to) {
    // See QR Code Detector, sizeOfBlackWhiteBlackRun()
    int fromX = (int) from.getX();
    int fromY = (int) from.getY();
    int toX = (int) to.getX();
    int toY = (int) to.getY();
    boolean steep = Math.abs(toY - fromY) > Math.abs(toX - fromX);
    if (steep) {
      int temp = fromX;
      fromX = fromY;
      fromY = temp;
      temp = toX;
      toX = toY;
      toY = temp;
    }

    int dx = Math.abs(toX - fromX);
    int dy = Math.abs(toY - fromY);
    int error = -dx >> 1;
    int ystep = fromY < toY ? 1 : -1;
    int xstep = fromX < toX ? 1 : -1;
    int transitions = 0;
    boolean inBlack = image.get(steep ? fromY : fromX, steep ? fromX : fromY);
    for (int x = fromX, y = fromY; x != toX; x += xstep) {
      boolean isBlack = image.get(steep ? y : x, steep ? x : y);
      if (isBlack != inBlack) {
        transitions++;
        inBlack = isBlack;
      }
      error += dy;
      if (error > 0) {
        if (y == toY) {
          break;
        }
        y += ystep;
        error -= dx;
      }
    }
    return new ResultPointsAndTransitions(from, to, transitions);
  }
  /**
   * This method detects a code in a "pure" image -- that is, pure monochrome image which contains
   * only an unrotated, unskewed, image of a code, with some white border around it. This is a
   * specialized method that works exceptionally fast in this special case.
   *
   * @see com.google.zxing.pdf417.PDF417Reader#extractPureBits(BitMatrix)
   * @see com.google.zxing.datamatrix.DataMatrixReader#extractPureBits(BitMatrix)
   */
  private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException {

    int[] leftTopBlack = image.getTopLeftOnBit();
    int[] rightBottomBlack = image.getBottomRightOnBit();
    if (leftTopBlack == null || rightBottomBlack == null) {
      throw NotFoundException.getNotFoundInstance();
    }

    int moduleSize = moduleSize(leftTopBlack, image);

    int top = leftTopBlack[1];
    int bottom = rightBottomBlack[1];
    int left = leftTopBlack[0];
    int right = rightBottomBlack[0];

    if (bottom - top != right - left) {
      // Special case, where bottom-right module wasn't black so we found
      // something else in
      // the last row
      // Assume it's a square, so use height as the width
      right = left + (bottom - top);
    }

    int matrixWidth = (right - left + 1) / moduleSize;
    int matrixHeight = (bottom - top + 1) / moduleSize;
    if (matrixWidth <= 0 || matrixHeight <= 0) {
      throw NotFoundException.getNotFoundInstance();
    }
    if (matrixHeight != matrixWidth) {
      // Only possibly decode square regions
      throw NotFoundException.getNotFoundInstance();
    }

    // Push in the "border" by half the module width so that we start
    // sampling in the middle of the module. Just in case the image is a
    // little off, this will help recover.
    int nudge = moduleSize >> 1;
    top += nudge;
    left += nudge;

    // Now just read off the bits
    BitMatrix bits = new BitMatrix(matrixWidth, matrixHeight);
    for (int y = 0; y < matrixHeight; y++) {
      int iOffset = top + y * moduleSize;
      for (int x = 0; x < matrixWidth; x++) {
        if (image.get(left + x * moduleSize, iOffset)) {
          bits.set(x, y);
        }
      }
    }
    return bits;
  }
示例#20
0
 public static BufferedImage get2CodeImage(String url) throws Exception {
   BitMatrix bitMatrix =
       new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 300, 300, getEncodeHints());
   int height = bitMatrix.getHeight();
   int width = bitMatrix.getWidth();
   BufferedImage qrCodeImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
   for (int x = 0; x < width; x++) {
     for (int y = 0; y < height; y++) {
       qrCodeImg.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
     }
   }
   return qrCodeImg;
 }
示例#21
0
 private static int moduleSize(int ai[], BitMatrix bitmatrix) throws NotFoundException {
   int j = bitmatrix.getWidth();
   int i = ai[0];
   for (int k = ai[1]; i < j && bitmatrix.get(i, k); i++) {}
   if (i == j) {
     throw NotFoundException.getNotFoundInstance();
   }
   i -= ai[0];
   if (i == 0) {
     throw NotFoundException.getNotFoundInstance();
   } else {
     return i;
   }
 }
 public static Bitmap generate(String data) throws WriterException {
   Bitmap bmp;
   QRCodeWriter writer = new QRCodeWriter();
   BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.QR_CODE, 512, 512);
   int width = bitMatrix.getWidth();
   int height = bitMatrix.getHeight();
   bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
   for (int x = 0; x < width; x++) {
     for (int y = 0; y < height; y++) {
       bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
     }
   }
   return bmp;
 }
示例#23
0
  /*
   * 去白边
   */
  public static BitMatrix deleteWhite(BitMatrix matrix) {
    int[] rec = matrix.getEnclosingRectangle();
    int resWidth = rec[2] + 1;
    int resHeight = rec[3] + 1;

    BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
    resMatrix.clear();
    for (int i = 0; i < resWidth; i++) {
      for (int j = 0; j < resHeight; j++) {
        if (matrix.get(i + rec[0], j + rec[1])) resMatrix.set(i, j);
      }
    }
    return resMatrix;
  }
示例#24
0
 private float crossCheckHorizontal(int paramInt1, int paramInt2, int paramInt3, int paramInt4)
 {
   BitMatrix localBitMatrix = this.image;
   int i = localBitMatrix.getWidth();
   int[] arrayOfInt = getCrossCheckStateCount();
   for (int j = paramInt1; (j >= 0) && (localBitMatrix.get(j, paramInt2)); j--) {
     arrayOfInt[2] = (1 + arrayOfInt[2]);
   }
   if (j < 0) {
     return (0.0F / 0.0F);
   }
   while ((j >= 0) && (!localBitMatrix.get(j, paramInt2)) && (arrayOfInt[1] <= paramInt3))
   {
     arrayOfInt[1] = (1 + arrayOfInt[1]);
     j--;
   }
   if ((j < 0) || (arrayOfInt[1] > paramInt3)) {
     return (0.0F / 0.0F);
   }
   while ((j >= 0) && (localBitMatrix.get(j, paramInt2)) && (arrayOfInt[0] <= paramInt3))
   {
     arrayOfInt[0] = (1 + arrayOfInt[0]);
     j--;
   }
   if (arrayOfInt[0] > paramInt3) {
     return (0.0F / 0.0F);
   }
   for (int k = paramInt1 + 1; (k < i) && (localBitMatrix.get(k, paramInt2)); k++) {
     arrayOfInt[2] = (1 + arrayOfInt[2]);
   }
   if (k == i) {
     return (0.0F / 0.0F);
   }
   while ((k < i) && (!localBitMatrix.get(k, paramInt2)) && (arrayOfInt[3] < paramInt3))
   {
     arrayOfInt[3] = (1 + arrayOfInt[3]);
     k++;
   }
   if ((k == i) || (arrayOfInt[3] >= paramInt3)) {
     return (0.0F / 0.0F);
   }
   while ((k < i) && (localBitMatrix.get(k, paramInt2)) && (arrayOfInt[4] < paramInt3))
   {
     arrayOfInt[4] = (1 + arrayOfInt[4]);
     k++;
   }
   if (arrayOfInt[4] >= paramInt3) {
     return (0.0F / 0.0F);
   }
   if (5 * Math.abs(arrayOfInt[0] + arrayOfInt[1] + arrayOfInt[2] + arrayOfInt[3] + arrayOfInt[4] - paramInt4) >= paramInt4) {
     return (0.0F / 0.0F);
   }
   if (foundPatternCross(arrayOfInt)) {
     return centerFromEnd(arrayOfInt, k);
   }
   return (0.0F / 0.0F);
 }
 byte[] readCodewords() {
   byte[] result = new byte[144];
   int height = bitMatrix.getHeight();
   int width = bitMatrix.getWidth();
   for (int y = 0; y < height; y++) {
     int[] bitnrRow = BITNR[y];
     for (int x = 0; x < width; x++) {
       int bit = bitnrRow[x];
       if (bit >= 0 && bitMatrix.get(x, y)) {
         result[bit / 6] |= (byte) (1 << (5 - (bit % 6)));
       }
     }
   }
   return result;
 }
示例#26
0
  public Bitmap createBitmap(BitMatrix matrix) {
    int width = matrix.getWidth();
    int height = matrix.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] = matrix.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;
  }
  public Bitmap generateQRCodeBitmap(String data) throws WriterException {
    BitMatrix bitMatrix = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, 600, 600);

    int width = bitMatrix.getWidth();
    int height = bitMatrix.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] = bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF;
    }

    Bitmap bitmap = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
    return bitmap;
  }
  private static int moduleSize(int[] leftTopBlack, BitMatrix image) throws NotFoundException {
    int width = image.getWidth();
    int x = leftTopBlack[0];
    int y = leftTopBlack[1];
    while (x < width && image.get(x, y)) {
      x++;
    }
    if (x == width) {
      throw NotFoundException.getNotFoundInstance();
    }

    int moduleSize = x - leftTopBlack[0];
    if (moduleSize == 0) {
      throw NotFoundException.getNotFoundInstance();
    }
    return moduleSize;
  }
示例#29
0
  private static BitMatrix updateBit(BitMatrix matrix, int margin) {
    int tempM = margin * 2;
    int[] rec = matrix.getEnclosingRectangle(); // 获取二维码图案的属性
    int resWidth = rec[2] + tempM;
    int resHeight = rec[3] + tempM;
    BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定义边框生成新的BitMatrix

    resMatrix.clear();
    for (int i = margin; i < resWidth - margin; i++) { // 循环,将二维码图案绘制到新的bitMatrix中
      for (int j = margin; j < resHeight - margin; j++) {
        if (matrix.get(i - margin + rec[0], j - margin + rec[1])) {
          resMatrix.set(i, j);
        }
      }
    }
    return resMatrix;
  }
 public static int[] generateQR(String text, int dimension) {
   QRCodeWriter qrCodeWriter = new QRCodeWriter();
   BitMatrix matrix = null;
   try {
     matrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, dimension, dimension);
   } catch (WriterException e) {
     e.printStackTrace();
   }
   int[] pixels = new int[dimension * dimension];
   for (int y = 0; y < dimension; y++) {
     int offset = y * dimension;
     for (int x = 0; x < dimension; x++) {
       if (matrix.get(x, y)) pixels[offset + x] = Color.BLACK;
       else pixels[offset + x] = Color.WHITE;
     }
   }
   return pixels;
 }