Exemple #1
0
  // YUVオブジェクトのバイナリデータを読み込み、マーカーを検知
  public void readYUV(
      final byte[] data,
      int dataWidth,
      int dataHeight,
      int left,
      int top,
      int width,
      int height,
      boolean reserversion) {
    final LuminanceSource source =
        new PlanarYUVLuminanceSource(
            data, dataWidth, dataHeight, left, top, width, height, reserversion);

    final BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
    // final Reader reader = new MultiFormatReader();
    final Reader reader = new QRCodeReader();
    try {
      // 結果をゲット
      mQRResult.setResultData(reader.decode(binaryBitmap));
      mFoundFlag = true;
    }
    // 見つからなかった!
    catch (NotFoundException ex) {
      ex.printStackTrace();
      mFoundFlag = false;
    } catch (ChecksumException ex) {
      ex.printStackTrace();
      mFoundFlag = false;
    } catch (FormatException ex) {
      ex.printStackTrace();
      mFoundFlag = false;
    }
  }
  private boolean decode(
      MonochromeBitmapSource source, float rotation, String expectedText, boolean tryHarder) {
    Result result;
    String suffix = " (" + (tryHarder ? "try harder, " : "") + "rotation: " + rotation + ')';

    try {
      result = barcodeReader.decode(source, tryHarder ? TRY_HARDER_HINT : null);
    } catch (ReaderException re) {
      System.out.println(re + suffix);
      return false;
    }

    if (!expectedFormat.equals(result.getBarcodeFormat())) {
      System.out.println(
          "Format mismatch: expected '"
              + expectedFormat
              + "' but got '"
              + result.getBarcodeFormat()
              + "'"
              + suffix);
      return false;
    }

    String resultText = result.getText();
    if (!expectedText.equals(resultText)) {
      System.out.println(
          "Mismatch: expected '" + expectedText + "' but got '" + resultText + "'" + suffix);
      return false;
    }
    return true;
  }
Exemple #3
0
  /**
   * Verifies that the specified symbol was encoded and rendered in a way that matches expectations.
   *
   * @param symbol the symbol to check
   * @throws IOException if there is any I/O error
   * @throws ReaderException if ZXing has an issue decoding the barcode image
   */
  private void verifySuccess(Symbol symbol) throws IOException, ReaderException {

    assertEquals("error message", "", symbol.error_msg);

    List<String> expectedList = Files.readAllLines(codewordsFile.toPath(), UTF_8);

    try {
      // try to verify codewords
      int[] actualCodewords = symbol.getCodewords();
      assertEquals(expectedList.size(), actualCodewords.length);
      for (int i = 0; i < actualCodewords.length; i++) {
        int expected = getInt(expectedList.get(i));
        int actual = actualCodewords[i];
        assertEquals("at codeword index " + i, expected, actual);
      }
    } catch (UnsupportedOperationException e) {
      // codewords aren't supported, try to verify patterns
      String[] actualPatterns = symbol.pattern;
      assertEquals(expectedList.size(), actualPatterns.length);
      for (int i = 0; i < actualPatterns.length; i++) {
        String expected = expectedList.get(i);
        String actual = actualPatterns[i];
        assertEquals("at pattern index " + i, expected, actual);
      }
    }

    // make sure the barcode images match
    BufferedImage expected = ImageIO.read(pngFile);
    BufferedImage actual = draw(symbol);
    assertEqual(expected, actual);

    // if possible, ensure an independent third party (ZXing) can read the generated barcode and
    // agrees on what it represents
    Reader zxingReader = findReader(symbol);
    if (zxingReader != null) {
      LuminanceSource source = new BufferedImageLuminanceSource(expected);
      BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
      Map<DecodeHintType, Boolean> hints =
          Collections.singletonMap(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
      Result result = zxingReader.decode(bitmap, hints);
      String zxingData = removeChecksum(result.getText(), symbol);
      String okapiData = removeStartStopChars(symbol.getContent(), symbol);
      assertEquals(okapiData, zxingData);
    }
  }
 public String attemptDecodePicture(Bitmap thumbnail) {
   if (thumbnail == null) {
     Log.d(TAG, "No picture selected");
   } else {
     Log.d(TAG, "Picture selected");
     Result rawResult = null;
     Reader reader = new QRCodeReader();
     int w = thumbnail.getWidth();
     int h = thumbnail.getHeight();
     int maxOneDimension = 500;
     if (w * h > maxOneDimension * maxOneDimension) { // too big, reduce
       float bitmapRatio = (float) w / (float) h;
       if (bitmapRatio > 0) {
         w = maxOneDimension;
         h = (int) (w / bitmapRatio);
       } else {
         h = maxOneDimension;
         w = (int) (h * bitmapRatio);
       }
       thumbnail = Bitmap.createScaledBitmap(thumbnail, w, h, true);
     }
     int[] pixels = new int[w * h];
     thumbnail.getPixels(pixels, 0, w, 0, 0, w, h);
     RGBLuminanceSource source = new RGBLuminanceSource(w, h, pixels);
     if (source.getMatrix() != null) {
       BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
       try {
         rawResult = reader.decode(bitmap);
       } catch (ReaderException re) {
         re.printStackTrace();
       } finally {
         reader.reset();
       }
     }
     if (rawResult != null) {
       Log.d(TAG, "QR code found " + rawResult.getText());
       return rawResult.getText();
     } else {
       Log.d(TAG, "Picture No QR code found");
     }
   }
   return null;
 }
  //  @Override
  public Result decode(BinaryBitmap image, Hashtable hints)
      throws NotFoundException, ChecksumException, FormatException {

    int width = image.getWidth();
    int height = image.getHeight();
    int halfWidth = width / 2;
    int halfHeight = height / 2;

    BinaryBitmap topLeft = image.crop(0, 0, halfWidth, halfHeight);
    try {
      return delegate.decode(topLeft, hints);
    } catch (NotFoundException re) {
      // continue
    }

    BinaryBitmap topRight = image.crop(halfWidth, 0, halfWidth, halfHeight);
    try {
      return delegate.decode(topRight, hints);
    } catch (NotFoundException re) {
      // continue
    }

    BinaryBitmap bottomLeft = image.crop(0, halfHeight, halfWidth, halfHeight);
    try {
      return delegate.decode(bottomLeft, hints);
    } catch (NotFoundException re) {
      // continue
    }

    BinaryBitmap bottomRight = image.crop(halfWidth, halfHeight, halfWidth, halfHeight);
    try {
      return delegate.decode(bottomRight, hints);
    } catch (NotFoundException re) {
      // continue
    }

    int quarterWidth = halfWidth / 2;
    int quarterHeight = halfHeight / 2;
    BinaryBitmap center = image.crop(quarterWidth, quarterHeight, halfWidth, halfHeight);
    return delegate.decode(center, hints);
  }
 public String attemptDecodeBytes(byte[] bytes, Camera camera) {
   Result rawResult = null;
   Reader reader = new QRCodeReader();
   int w = camera.getParameters().getPreviewSize().width;
   int h = camera.getParameters().getPreviewSize().height;
   PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(bytes, w, h, 0, 0, w, h, false);
   if (source.getMatrix() != null) {
     BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
     try {
       rawResult = reader.decode(bitmap);
     } catch (ReaderException re) {
       // nothing to do here
     } finally {
       reader.reset();
     }
   }
   if (rawResult != null) {
     Log.d(TAG, "QR code found " + rawResult.getText());
     return rawResult.getText();
   } else {
     return null;
   }
 }
Exemple #7
0
  public void draw() {
    if (cam.available() == true) {
      cam.read();
      pushMatrix();
      scale(-1, 1);
      translate(-cam.width, 0);
      image(cam, 0, 0);
      popMatrix();

      // image(cam, 0,0);
      try {
        // Now test to see if it has a QR code embedded in it
        LuminanceSource source = new BufferedImageLuminanceSource((BufferedImage) cam.getImage());
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result = reader.decode(bitmap);
        // Once we get the results, we can do some display
        if (result.getText() != null) {
          println(result.getText());
          ResultPoint[] points = result.getResultPoints();
          // Draw some ellipses on at the control points
          for (int i = 0; i < points.length; i++) {
            // fill(#ff8c00);
            ellipse(points[i].getX(), points[i].getY(), 20, 20);
            // fill(#ff0000);
            text(i, points[i].getX(), points[i].getY());
          }
          // Now fetch the book cover, if it is found
          if (!result.getText().equals(lastISBNAcquired)) {
            String url = "http://covers.oreilly.com/images/" + result.getText() + "/cat.gif";
            try {
              cover = loadImage(url, "gif");
              lastISBNAcquired = result.getText();
            } catch (Exception e) {
              cover = null;
            }
          }
          // Superimpose the cover on the image
          if (cover != null) {
            image(cover, points[1].getX(), points[1].getY());
          }
        }
      } catch (Exception e) {
        //         println(e.toString());
      }
    }
  }
  @SuppressWarnings("unchecked")
  private void doDecodeMultiple(
      BinaryBitmap image,
      @SuppressWarnings("rawtypes") Hashtable hints,
      @SuppressWarnings("rawtypes") Vector results,
      int xOffset,
      int yOffset) {
    Result result;
    try {
      result = delegate.decode(image, hints);
    } catch (ReaderException re) {
      return;
    }
    boolean alreadyFound = false;
    for (int i = 0; i < results.size(); i++) {
      Result existingResult = (Result) results.elementAt(i);
      if (existingResult.getText().equals(result.getText())) {
        alreadyFound = true;
        break;
      }
    }
    if (alreadyFound) {
      return;
    }
    results.addElement(translateResultPoints(result, xOffset, yOffset));
    ResultPoint[] resultPoints = result.getResultPoints();
    if (resultPoints == null || resultPoints.length == 0) {
      return;
    }
    int width = image.getWidth();
    int height = image.getHeight();
    float minX = width;
    float minY = height;
    float maxX = 0.0f;
    float maxY = 0.0f;
    for (int i = 0; i < resultPoints.length; i++) {
      ResultPoint point = resultPoints[i];
      float x = point.getX();
      float y = point.getY();
      if (x < minX) {
        minX = x;
      }
      if (y < minY) {
        minY = y;
      }
      if (x > maxX) {
        maxX = x;
      }
      if (y > maxY) {
        maxY = y;
      }
    }

    // Decode left of barcode
    if (minX > MIN_DIMENSION_TO_RECUR) {
      doDecodeMultiple(image.crop(0, 0, (int) minX, height), hints, results, xOffset, yOffset);
    }
    // Decode above barcode
    if (minY > MIN_DIMENSION_TO_RECUR) {
      doDecodeMultiple(image.crop(0, 0, width, (int) minY), hints, results, xOffset, yOffset);
    }
    // Decode right of barcode
    if (maxX < width - MIN_DIMENSION_TO_RECUR) {
      doDecodeMultiple(
          image.crop((int) maxX, 0, width - (int) maxX, height),
          hints,
          results,
          xOffset + (int) maxX,
          yOffset);
    }
    // Decode below barcode
    if (maxY < height - MIN_DIMENSION_TO_RECUR) {
      doDecodeMultiple(
          image.crop(0, (int) maxY, width, height - (int) maxY),
          hints,
          results,
          xOffset,
          yOffset + (int) maxY);
    }
  }
 //  @Override
 public void reset() {
   delegate.reset();
 }