mvallim/emv-qrcode

Get Qr code!

landry007 opened this issue · 2 comments

Hello, please, I have a problem, how to get the Qr code with the string which is displayed with the toString method? thank you

Hi @landry007

Trys this sample code.

QRCodeImage.create(merchantPresentMode.toString())

Ex.

pom.xml

<dependency>
  <groupId>com.google.zxing</groupId>
  <artifactId>core</artifactId>
  <version>3.4.1</version>
</dependency>

<dependency>
  <groupId>com.google.zxing</groupId>
  <artifactId>javase</artifactId>
  <version>3.4.1</version>
</dependency>

QRCodeImage.java

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public final class QRCodeImage {

  private QRCodeImage() {
    super();
  }

  /**
   * Create QRCode image
   *
   * @param value Base64 string
   * @return array of bytes image
   * @throws IOException
   * @throws WriterException
   */
  public static byte[] create(final String value) throws IOException, WriterException {
    try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
      final QRCodeWriter qrcodeWriter = new QRCodeWriter();
      final BitMatrix bitMatrix = qrcodeWriter.encode(value, BarcodeFormat.QR_CODE, 100, 100);
      final BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
      ImageIO.write(bufferedImage, "png", outputStream);
      return outputStream.toByteArray();
    }
  }

}

Closing this issue for being without activity.