@Test
  public void testQRCodeWriter() throws WriterException {
    // The QR should be multiplied up to fit, with extra padding if necessary
    int bigEnough = 256;
    Writer writer = new QRCodeWriter();
    BitMatrix matrix =
        writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, bigEnough, bigEnough, null);
    assertNotNull(matrix);
    assertEquals(bigEnough, matrix.getWidth());
    assertEquals(bigEnough, matrix.getHeight());

    // The QR will not fit in this size, so the matrix should come back bigger
    int tooSmall = 20;
    matrix =
        writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, tooSmall, tooSmall, null);
    assertNotNull(matrix);
    assertTrue(tooSmall < matrix.getWidth());
    assertTrue(tooSmall < matrix.getHeight());

    // We should also be able to handle non-square requests by padding them
    int strangeWidth = 500;
    int strangeHeight = 100;
    matrix =
        writer.encode(
            "http://www.google.com/", BarcodeFormat.QR_CODE, strangeWidth, strangeHeight, null);
    assertNotNull(matrix);
    assertEquals(strangeWidth, matrix.getWidth());
    assertEquals(strangeHeight, matrix.getHeight());
  }
Beispiel #2
0
  /** @param args */
  public static void main(String[] args) {

    Charset charset = Charset.forName("UTF-8");
    CharsetEncoder encoder = charset.newEncoder();
    byte[] b = null;
    try {
      // Convert a string to UTF-8 bytes in a ByteBuffer
      ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("http://www.google.co.in/"));
      b = bbuf.array();
    } catch (CharacterCodingException e) {
      System.out.println(e.getMessage());
    }

    String data;
    try {
      data = new String(b, "UTF-8");
      // get a byte matrix for the data
      BitMatrix matrix = null;
      ByteMatrix byteMatrix = null;
      int h = 100;
      int w = 100;
      com.google.zxing.Writer writer = new MultiFormatWriter();
      try {
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        matrix = writer.encode(data, com.google.zxing.BarcodeFormat.QR_CODE, w, h, hints);
      } catch (com.google.zxing.WriterException e) {
        System.out.println(e.getMessage());
      }

      // change this path to match yours (this is my mac home folder, you can use: c:\\qr_png.png if
      // you are on windows)
      String filePath = "D:\\CreateQR.JPG";
      File file = new File(filePath);
      try {
        MatrixToImageWriter.writeToFile(matrix, "JPG", file);
        System.out.println("printing to " + file.getAbsolutePath());
      } catch (IOException e) {
        System.out.println(e.getMessage());
      }
    } catch (UnsupportedEncodingException e) {
      System.out.println(e.getMessage());
    }
  }
  private static void compareToGoldenFile(
      String contents, ErrorCorrectionLevel ecLevel, int resolution, String fileName)
      throws WriterException, IOException {

    BufferedImage image = loadImage(fileName);
    assertNotNull(image);
    BitMatrix goldenResult = createMatrixFromImage(image);
    assertNotNull(goldenResult);

    Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
    hints.put(EncodeHintType.ERROR_CORRECTION, ecLevel);
    Writer writer = new QRCodeWriter();
    BitMatrix generatedResult =
        writer.encode(contents, BarcodeFormat.QR_CODE, resolution, resolution, hints);

    assertEquals(resolution, generatedResult.getWidth());
    assertEquals(resolution, generatedResult.getHeight());
    assertEquals(goldenResult, generatedResult);
  }
  public void generate(String datos) {

    this.datos = datos;

    FileOutputStream qrCode = null;
    try {
      BitMatrix bm;
      Writer writer = new QRCodeWriter();
      bm = writer.encode(datos, BarcodeFormat.QR_CODE, ancho, alto);
      BufferedImage image = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);
      for (int y = 0; y < ancho; y++) {
        for (int x = 0; x < alto; x++) {
          int grayValue = (bm.get(x, y) ? 1 : 0) & 0xff;
          image.setRGB(x, y, grayValue == 0 ? 0 : 0xFFFFFF);
        }
      }
      image = invertirColores(image);
      qrCode = new FileOutputStream(RUTA_IMAGEN);
      ImageIO.write(image, FORMATO_IMAGEN, qrCode);
    } catch (IOException ex) {
      Logger.getLogger(QRGenMain.class.getName()).log(Level.SEVERE, null, ex);

    } catch (WriterException ex) {
      Logger.getLogger(QRGenMain.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      try {
        qrCode.close();
      } catch (IOException ex) {
        Logger.getLogger(QRGenMain.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    Desktop d = Desktop.getDesktop();
    try {
      d.open(new File(RUTA_IMAGEN));
      System.out.println(RUTA_IMAGEN);
    } catch (IOException ex) {
      Logger.getLogger(QRGenMain.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
Beispiel #5
0
  /**
   * Generate a QR code with the payment information of a given size, optionally, with branding
   *
   * @param size A size of the QR code (without the branding) in pixels
   * @param paymentString A SPAYD string with payment information
   * @param hasBranging A flag that can be used to turn on/off branding
   * @return An image with the payment QR code
   * @throws IOException
   */
  public static BufferedImage getQRCode(Integer size, String paymentString, boolean hasBranging)
      throws IOException {
    if (size == null) {
      size = SpaydConstants.defQRSize;
    } else if (size < SpaydConstants.minQRSize) {
      size = SpaydConstants.minQRSize;
    } else if (size > SpaydConstants.maxQRSize) {
      size = SpaydConstants.maxQRSize;
    }

    BitMatrix matrix = null;
    int h = size;
    int w = size;
    int barsize = -1;
    Writer writer = new MultiFormatWriter();
    try {
      Map<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
      hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1");
      QRCode code = Encoder.encode(paymentString, ErrorCorrectionLevel.M, hints);
      hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
      barsize = size / (code.getMatrix().getWidth() + 8);
      matrix = writer.encode(paymentString, com.google.zxing.BarcodeFormat.QR_CODE, w, h, hints);
    } catch (com.google.zxing.WriterException e) {
      System.out.println(e.getMessage());
    }

    if (matrix == null || barsize < 0) {
      throw new InvalidFormatException();
    }

    BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix);

    if (hasBranging) {
      Graphics2D g = (Graphics2D) image.getGraphics();

      BasicStroke bs = new BasicStroke(2);
      g.setStroke(bs);
      g.setColor(Color.BLACK);
      g.drawLine(0, 0, w, 0);
      g.drawLine(0, 0, 0, h);
      g.drawLine(w, 0, w, h);
      g.drawLine(0, h, w, h);

      String str = "QR Platba";
      int fontSize = size / 12;

      g.setFont(new Font("Verdana", Font.BOLD, fontSize));

      FontMetrics fm = g.getFontMetrics();
      Rectangle2D rect = fm.getStringBounds(str, g);

      g.setColor(Color.WHITE);
      g.fillRect(
          2 * barsize,
          h - fm.getAscent(),
          (int) rect.getWidth() + 4 * barsize,
          (int) rect.getHeight());

      int padding = 4 * barsize;

      BufferedImage paddedImage =
          new BufferedImage(w + 2 * padding, h + padding + (int) rect.getHeight(), image.getType());
      Graphics2D g2 = paddedImage.createGraphics();
      g2.setRenderingHint(
          RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
      g2.setFont(new Font("Verdana", Font.BOLD, fontSize));
      g2.setPaint(Color.WHITE);
      g2.fillRect(0, 0, paddedImage.getWidth(), paddedImage.getHeight());
      g2.drawImage(image, padding, padding, Color.WHITE, null);

      g2.setColor(Color.BLACK);
      g2.drawString(str, padding + 4 * barsize, (int) (padding + h + rect.getHeight() - barsize));

      image = paddedImage;
    }

    return image;
  }