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;
  }
Example #2
0
  /**
   * 字符串转二维码
   *
   * @param str 渠道号
   */
  private void StringToBarcode(String str) {
    BitMatrix byteMatrix = null;
    try {
      // 如果目录不存在-创建目录
      if (!mConfig.getOutPath().exists()) {
        mConfig.getOutPath().mkdir();
      }
      String change = new String(str.getBytes(mConfig.getCharCode()), mConfig.getCharCode());
      // 二维码内容
      String content = mConfig.getContentRegex().replace("*", change);
      // 二维码宽度
      int width = mConfig.getWidth();
      QRCoder bc = new QRCoder();
      // 输出文件
      String outPutFile =
          mConfig.getOutPath().getPath() + File.separator + str + "." + mConfig.getFormate();

      bc.createQrCode(
          content,
          width,
          width,
          outPutFile,
          mConfig.getLogo(),
          mConfig.getFormate(),
          0xFF00000F,
          0xFFFFFFFF,
          mConfig.getLogoCent());
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (byteMatrix != null) {
        byteMatrix.clear();
      }
    }
  }
 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;
   }
 }
Example #4
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();
   }
 }
  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;
  }
Example #7
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;
    }
  }
  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;
  }
Example #9
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;
    }
  }
Example #10
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);
  }
Example #11
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;
 }
Example #12
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;
  }
  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;
  }
Example #14
0
  /**
   * Locate the vertices and the codewords area of a black blob using the Start and Stop patterns as
   * locators. This assumes that the image is rotated 180 degrees and if it locates the start and
   * stop patterns at it will re-map the vertices for a 0 degree rotation. TODO: Change assumption
   * about barcode location. TODO: Scanning every row is very expensive. We should only do this for
   * TRY_HARDER.
   *
   * @param matrix the scanned barcode image.
   * @return an array containing the vertices: vertices[0] x, y top left barcode vertices[1] x, y
   *     bottom left barcode vertices[2] x, y top right barcode vertices[3] x, y bottom right
   *     barcode vertices[4] x, y top left codeword area vertices[5] x, y bottom left codeword area
   *     vertices[6] x, y top right codeword area vertices[7] x, y bottom right codeword area
   */
  private static ResultPoint[] findVertices180(BitMatrix matrix) {
    int height = matrix.getHeight();
    int width = matrix.getWidth();
    int halfWidth = width >> 1;

    ResultPoint[] result = new ResultPoint[8];
    boolean found = false;

    // Top Left
    for (int i = height - 1; i > 0; i--) {
      int[] loc = findGuardPattern(matrix, halfWidth, i, halfWidth, true, START_PATTERN_REVERSE);
      if (loc != null) {
        result[0] = new ResultPoint(loc[1], i);
        result[4] = new ResultPoint(loc[0], i);
        found = true;
        break;
      }
    }
    // Bottom Left
    if (found) { // Found the Top Left vertex
      found = false;
      for (int i = 0; i < height; i++) {
        int[] loc = findGuardPattern(matrix, halfWidth, i, halfWidth, true, START_PATTERN_REVERSE);
        if (loc != null) {
          result[1] = new ResultPoint(loc[1], i);
          result[5] = new ResultPoint(loc[0], i);
          found = true;
          break;
        }
      }
    }
    // Top Right
    if (found) { // Found the Bottom Left vertex
      found = false;
      for (int i = height - 1; i > 0; i--) {
        int[] loc = findGuardPattern(matrix, 0, i, halfWidth, false, STOP_PATTERN_REVERSE);
        if (loc != null) {
          result[2] = new ResultPoint(loc[0], i);
          result[6] = new ResultPoint(loc[1], i);
          found = true;
          break;
        }
      }
    }
    // Bottom Right
    if (found) { // Found the Top Right vertex
      found = false;
      for (int i = 0; i < height; i++) {
        int[] loc = findGuardPattern(matrix, 0, i, halfWidth, false, STOP_PATTERN_REVERSE);
        if (loc != null) {
          result[3] = new ResultPoint(loc[0], i);
          result[7] = new ResultPoint(loc[1], i);
          found = true;
          break;
        }
      }
    }
    return found ? result : 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));
     }
   }
 }
  public static BitMatrix parse(String stringRepresentation, String setString, String unsetString) {
    if (stringRepresentation == null) {
      throw new IllegalArgumentException();
    }

    boolean[] bits = new boolean[stringRepresentation.length()];
    int bitsPos = 0;
    int rowStartPos = 0;
    int rowLength = -1;
    int nRows = 0;
    int pos = 0;
    while (pos < stringRepresentation.length()) {
      if (stringRepresentation.charAt(pos) == '\n' || stringRepresentation.charAt(pos) == '\r') {
        if (bitsPos > rowStartPos) {
          if (rowLength == -1) {
            rowLength = bitsPos - rowStartPos;
          } else if (bitsPos - rowStartPos != rowLength) {
            throw new IllegalArgumentException("row lengths do not match");
          }
          rowStartPos = bitsPos;
          nRows++;
        }
        pos++;
      } else if (stringRepresentation.substring(pos, pos + setString.length()).equals(setString)) {
        pos += setString.length();
        bits[bitsPos] = true;
        bitsPos++;
      } else if (stringRepresentation
          .substring(pos, pos + unsetString.length())
          .equals(unsetString)) {
        pos += unsetString.length();
        bits[bitsPos] = false;
        bitsPos++;
      } else {
        throw new IllegalArgumentException(
            "illegal character encountered: " + stringRepresentation.substring(pos));
      }
    }

    // no EOL at end?
    if (bitsPos > rowStartPos) {
      if (rowLength == -1) {
        rowLength = bitsPos - rowStartPos;
      } else if (bitsPos - rowStartPos != rowLength) {
        throw new IllegalArgumentException("row lengths do not match");
      }
      nRows++;
    }

    BitMatrix matrix = new BitMatrix(rowLength, nRows);
    for (int i = 0; i < bitsPos; i++) {
      if (bits[i]) {
        matrix.set(i % rowLength, i / rowLength);
      }
    }
    return matrix;
  }
 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;
 }
Example #18
0
 /**
  * Convenience method that can decode a Data Matrix Code represented as a 2D array of booleans.
  * "true" is taken to mean a black module.
  *
  * @param image booleans representing white/black Data Matrix Code modules
  * @return text and bytes encoded within the Data Matrix Code
  * @throws FormatException if the Data Matrix Code cannot be decoded
  * @throws ChecksumException if error correction fails
  */
 public DecoderResult decode(boolean[][] image) throws FormatException, ChecksumException {
   int dimension = image.length;
   BitMatrix bits = new BitMatrix(dimension);
   for (int i = 0; i < dimension; i++) {
     for (int j = 0; j < dimension; j++) {
       if (image[i][j]) {
         bits.set(j, i);
       }
     }
   }
   return decode(bits);
 }
Example #19
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;
 }
  /**
   * 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;
  }
 /**
  * Exclusive-or (XOR): Flip the bit in this {@code BitMatrix} if the corresponding mask bit is
  * set.
  *
  * @param mask XOR mask
  */
 public void xor(BitMatrix mask) {
   if (width != mask.getWidth() || height != mask.getHeight() || rowSize != mask.getRowSize()) {
     throw new IllegalArgumentException("input matrix dimensions do not match");
   }
   BitArray rowArray = new BitArray(width / 32 + 1);
   for (int y = 0; y < height; y++) {
     int offset = y * rowSize;
     int[] row = mask.getRow(y, rowArray).getBitArray();
     for (int x = 0; x < rowSize; x++) {
       bits[offset + x] ^= row[x];
     }
   }
 }
Example #22
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;
 }
Example #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;
  }
 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;
 }
 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;
 }
Example #27
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;
  }
Example #29
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);
 }
  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;
  }