コード例 #1
0
  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;
  }
コード例 #2
0
  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;
  }
コード例 #3
0
  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;
  }
コード例 #4
0
ファイル: Decoder.java プロジェクト: belle96/zxing
 /**
  * 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);
 }
コード例 #5
0
  /**
   * 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;
  }
コード例 #6
0
ファイル: QRCodeUtil.java プロジェクト: phantom2012/java
  /*
   * 去白边
   */
  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;
  }
コード例 #7
0
  /**
   * Convert the ByteMatrix to BitMatrix.
   *
   * @param matrix The input matrix.
   * @return The output matrix.
   */
  private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix) {
    int matrixWidgth = matrix.getWidth();
    int matrixHeight = matrix.getHeight();

    BitMatrix output = new BitMatrix(matrixWidgth, matrixHeight);
    output.clear();
    for (int i = 0; i < matrixWidgth; i++) {
      for (int j = 0; j < matrixHeight; j++) {
        // Zero is white in the bytematrix
        if (matrix.get(i, j) == 1) {
          output.set(i, j);
        }
      }
    }

    return output;
  }
コード例 #8
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;
  }
コード例 #9
0
  // In case the golden images are not monochromatic, convert the RGB values to greyscale.
  private static BitMatrix createMatrixFromImage(BufferedImage image) {
    int width = image.getWidth();
    int height = image.getHeight();
    int[] pixels = new int[width * height];
    image.getRGB(0, 0, width, height, pixels, 0, width);

    BitMatrix matrix = new BitMatrix(width, height);
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
        int pixel = pixels[y * width + x];
        int luminance =
            (306 * ((pixel >> 16) & 0xFF) + 601 * ((pixel >> 8) & 0xFF) + 117 * (pixel & 0xFF))
                >> 10;
        if (luminance <= 0x7F) {
          matrix.set(x, y);
        }
      }
    }
    return matrix;
  }
コード例 #10
0
ファイル: QRCode.java プロジェクト: tangying2015/printbarcode
  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();
    }
  }
コード例 #11
0
  /**
   * 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.qrcode.QRCodeReader#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];

    int matrixWidth = (right - left + 1) / moduleSize;
    int matrixHeight = (bottom - top + 1) / moduleSize;
    if (matrixWidth == 0 || matrixHeight == 0) {
      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;
  }
コード例 #12
0
  /**
   * Encode.
   *
   * @param text the text to encode
   * @return the image
   */
  public Image encode(String text) {
    // Encode the text
    QRCode qrcode = new QRCode();
    try {
      Encoder.encode(text, ErrorCorrectionLevel.H, qrcode);
    } catch (WriterException e) {
      e.printStackTrace();
    }
    byte[][] matrix = qrcode.getMatrix().getArray();
    int size = qrcode.getMatrixWidth() * scale;

    // Make the BufferedImage that are to hold the QRCode
    BufferedImage im = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    im.createGraphics();
    Graphics2D g = (Graphics2D) im.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, size, size);

    // BitMatrix for validation
    BitMatrix bm = new BitMatrix(qrcode.getMatrixWidth());

    // paint the image using the ByteMatrik
    for (int h = 0; h < qrcode.getMatrixWidth(); h++) {
      for (int w = 0; w < qrcode.getMatrixWidth(); w++) {
        // Find the colour of the dot
        if (matrix[h][w] == 0) g.setColor(Color.WHITE);
        else {
          g.setColor(Color.BLACK);
          bm.set(w, h); // build the BitMatrix
        }

        // Paint the dot
        g.fillRect(w * scale, h * scale, scale, scale);
      }
    }
    return im;
  }
コード例 #13
0
  // Does not sharpen the data, as this call is intended to only be used by 2D Readers.
  public BitMatrix getBlackMatrix() throws NotFoundException {
    LuminanceSource source = getLuminanceSource();
    int width = source.getWidth();
    int height = source.getHeight();
    BitMatrix matrix = new BitMatrix(width, height);

    // Quickly calculates the histogram by sampling four rows from the image. This proved to be
    // more robust on the blackbox tests than sampling a diagonal as we used to do.
    initArrays(width);
    int[] localBuckets = buckets;
    for (int y = 1; y < 5; y++) {
      int row = height * y / 5;
      byte[] localLuminances = source.getRow(row, luminances);
      int right = (width << 2) / 5;
      for (int x = width / 5; x < right; x++) {
        int pixel = localLuminances[x] & 0xff;
        localBuckets[pixel >> LUMINANCE_SHIFT]++;
      }
    }
    int blackPoint = estimateBlackPoint(localBuckets);

    // We delay reading the entire image luminance until the black point estimation succeeds.
    // Although we end up reading four rows twice, it is consistent with our motto of
    // "fail quickly" which is necessary for continuous scanning.
    byte[] localLuminances = source.getMatrix();
    for (int y = 0; y < height; y++) {
      int offset = y * width;
      for (int x = 0; x < width; x++) {
        int pixel = localLuminances[offset + x] & 0xff;
        if (pixel < blackPoint) {
          matrix.set(x, y);
        }
      }
    }

    return matrix;
  }
コード例 #14
0
  /**
   * 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.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();
    }

    float moduleSize = moduleSize(leftTopBlack, image);

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

    // Sanity check!
    if (left >= right || top >= bottom) {
      throw NotFoundException.getNotFoundInstance();
    }

    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 = Math.round((right - left + 1) / moduleSize);
    int matrixHeight = Math.round((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 = (int) (moduleSize / 2.0f);
    top += nudge;
    left += nudge;

    // But careful that this does not sample off the edge
    int nudgedTooFarRight = left + (int) ((matrixWidth - 1) * moduleSize) - (right - 1);
    if (nudgedTooFarRight > 0) {
      if (nudgedTooFarRight > nudge) {
        // Neither way fits; abort
        throw NotFoundException.getNotFoundInstance();
      }
      left -= nudgedTooFarRight;
    }
    int nudgedTooFarDown = top + (int) ((matrixHeight - 1) * moduleSize) - (bottom - 1);
    if (nudgedTooFarDown > 0) {
      if (nudgedTooFarDown > nudge) {
        // Neither way fits; abort
        throw NotFoundException.getNotFoundInstance();
      }
      top -= nudgedTooFarDown;
    }

    // Now just read off the bits
    BitMatrix bits = new BitMatrix(matrixWidth, matrixHeight);
    for (int y = 0; y < matrixHeight; y++) {
      int iOffset = top + (int) (y * moduleSize);
      for (int x = 0; x < matrixWidth; x++) {
        if (image.get(left + (int) (x * moduleSize), iOffset)) {
          bits.set(x, y);
        }
      }
    }
    return bits;
  }