Class QRCodeGenerator

java.lang.Object
io.github.pratiyush.totp.QRCodeGenerator

public final class QRCodeGenerator extends Object
Generates QR codes for TOTP secrets compatible with authenticator apps.

This class creates QR codes containing otpauth:// URIs that can be scanned by Google Authenticator, Microsoft Authenticator, and other compatible apps.

Dependencies

Requires ZXing library (optional dependency). If ZXing is not available, methods will throw NoClassDefFoundError.

Usage Examples


 // Generate QR code as BufferedImage
 BufferedImage qr = QRCodeGenerator.generateImage(
         secret, "user@example.com", "MyApp", 250);
 
 // Save to file
 QRCodeGenerator.saveToFile(secret, "user@example.com", "MyApp",
         Path.of("qr.png"), 250);
 
 // Get as Base64 for embedding in HTML
 String base64 = QRCodeGenerator.generateBase64(
         secret, "user@example.com", "MyApp", 250);
 String html = "<img src='data:image/png;base64," + base64 + "'/>";
 

otpauth URI Format

Generated URIs follow the format:

 
 otpauth://totp/{issuer}:{account}?secret={secret}&issuer={issuer}&algorithm={algo}&digits={digits}&period={period}
 
 
See Also:
  • Field Details

    • DEFAULT_SIZE

      public static final int DEFAULT_SIZE
      Default QR code size in pixels
      See Also:
    • MIN_SIZE

      public static final int MIN_SIZE
      Minimum QR code size in pixels
      See Also:
    • MAX_SIZE

      public static final int MAX_SIZE
      Maximum QR code size in pixels
      See Also:
  • Method Details

    • buildOtpauthUri

      public static String buildOtpauthUri(String secret, String account, String issuer, TOTPConfig config) throws TOTPException
      Builds an otpauth URI for TOTP.
      Parameters:
      secret - the Base32 encoded secret
      account - the account name (typically email)
      issuer - the service name
      config - optional configuration (null for defaults)
      Returns:
      the otpauth URI
      Throws:
      TOTPException - if parameters are invalid
    • buildOtpauthUri

      public static String buildOtpauthUri(String secret, String account, String issuer) throws TOTPException
      Builds an otpauth URI with default configuration.
      Parameters:
      secret - the Base32 encoded secret
      account - the account name
      issuer - the service name
      Returns:
      the otpauth URI
      Throws:
      TOTPException - if parameters are invalid
    • generateImage

      public static BufferedImage generateImage(String secret, String account, String issuer, int size) throws TOTPException
      Generates a QR code image.
      Parameters:
      secret - the Base32 encoded secret
      account - the account name
      issuer - the service name
      size - the image size in pixels
      Returns:
      the QR code as BufferedImage
      Throws:
      TOTPException - if generation fails
    • generateImage

      public static BufferedImage generateImage(String secret, String account, String issuer, int size, TOTPConfig config) throws TOTPException
      Generates a QR code image with custom configuration.
      Parameters:
      secret - the Base32 encoded secret
      account - the account name
      issuer - the service name
      size - the image size in pixels
      config - optional TOTP configuration
      Returns:
      the QR code as BufferedImage
      Throws:
      TOTPException - if generation fails
    • generateBase64

      public static String generateBase64(String secret, String account, String issuer, int size) throws TOTPException
      Generates a QR code as Base64 encoded PNG.

      The result can be used directly in HTML img tags:

      
       <img src="data:image/png;base64,{result}" />
       
      Parameters:
      secret - the Base32 encoded secret
      account - the account name
      issuer - the service name
      size - the image size in pixels
      Returns:
      Base64 encoded PNG
      Throws:
      TOTPException - if generation fails
    • generateBase64

      public static String generateBase64(String secret, String account, String issuer, int size, TOTPConfig config) throws TOTPException
      Generates a QR code as Base64 encoded PNG with custom configuration.
      Parameters:
      secret - the Base32 encoded secret
      account - the account name
      issuer - the service name
      size - the image size in pixels
      config - optional TOTP configuration
      Returns:
      Base64 encoded PNG
      Throws:
      TOTPException - if generation fails
    • generateDataUri

      public static String generateDataUri(String secret, String account, String issuer, int size) throws TOTPException
      Generates a data URI for embedding in HTML.
      Parameters:
      secret - the Base32 encoded secret
      account - the account name
      issuer - the service name
      size - the image size in pixels
      Returns:
      data URI (data:image/png;base64,...)
      Throws:
      TOTPException - if generation fails
    • saveToFile

      public static void saveToFile(String secret, String account, String issuer, Path path, int size) throws TOTPException
      Saves a QR code to a file.
      Parameters:
      secret - the Base32 encoded secret
      account - the account name
      issuer - the service name
      path - the file path
      size - the image size in pixels
      Throws:
      TOTPException - if generation or writing fails
    • saveToFile

      public static void saveToFile(String secret, String account, String issuer, Path path, int size, TOTPConfig config) throws TOTPException
      Saves a QR code to a file with custom configuration.
      Parameters:
      secret - the Base32 encoded secret
      account - the account name
      issuer - the service name
      path - the file path
      size - the image size in pixels
      config - optional TOTP configuration
      Throws:
      TOTPException - if generation or writing fails
    • writeTo

      public static void writeTo(String secret, String account, String issuer, OutputStream out, String format, int size) throws TOTPException
      Writes a QR code to an output stream.
      Parameters:
      secret - the Base32 encoded secret
      account - the account name
      issuer - the service name
      out - the output stream
      format - the image format (PNG, JPEG, etc.)
      size - the image size in pixels
      Throws:
      TOTPException - if generation or writing fails