DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Should You Create Your Own E-Signature API?
  • Import a Function/Module in Dataweave
  • Using Barcodes in iText 7
  • Dynamically Evaluate Dataweave Scripts

Trending

  • 12 Principles for Better Software Engineering
  • Run Scalable Python Workloads With Modal
  • Vibe Coding: Conversational Software Development — Part 1 Introduction
  • The Invisible Risk in Your Middleware: A Next.js Flaw You Shouldn’t Ignore
  1. DZone
  2. Coding
  3. Java
  4. How to Create a QR Code SVG Using Zxing and JFreeSVG in Java? [Snippet]

How to Create a QR Code SVG Using Zxing and JFreeSVG in Java? [Snippet]

Create your own QR Code SVG image in Java using the Zxing code generation library.

By 
Mohamed Sanaulla user avatar
Mohamed Sanaulla
·
May. 08, 19 · Code Snippet
Likes (5)
Comment
Save
Tweet
Share
18.3K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will look at how to use the Zxing QR code generation library and JFreeSVG library to create a QR Code SVG image in Java.

QR Code Generation

The below code creates a java.awt.image.BufferedImage object representing QR Code using Zxing library:

public static BufferedImage getQRCode(String targetUrl, int width, 
    int height) {
    try {
        Hashtable<EncodeHintType, Object> hintMap = new Hashtable<>();

        hintMap.put(EncodeHintType.ERROR_CORRECTION, 
            ErrorCorrectionLevel.L);
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix byteMatrix = qrCodeWriter.encode(targetUrl, 
            BarcodeFormat.QR_CODE, width, height, hintMap);
        int CrunchifyWidth = byteMatrix.getWidth();

        BufferedImage image = new BufferedImage(CrunchifyWidth, 
            CrunchifyWidth, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();

        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
        graphics.setColor(Color.BLACK);

        for (int i = 0; i < CrunchifyWidth; i++) {
            for (int j = 0; j < CrunchifyWidth; j++) {
                if (byteMatrix.get(i, j)) {
                    graphics.fillRect(i, j, 1, 1);
                }
            }
        }
        return image;
    } catch (WriterException e) {
        e.printStackTrace();
        throw new RuntimeException("Error getting QR Code");
    }

}


Conversion to SVG

The below code snippet converts the java.awt.image.BufferedImage object into SVG using JFreeSVG:

public static String getQRCodeSvg(String targetUrl, int width, 
    int height, boolean withViewBox){
    SVGGraphics2D g2 = new SVGGraphics2D(width, height);
    BufferedImage qrCodeImage = getQRCode(targetUrl, width, height);
    g2.drawImage(qrCodeImage, 0,0, width, height, null);

    ViewBox viewBox = null;
    if ( withViewBox ){
        viewBox = new ViewBox(0,0,width,height);
    }
    return g2.getSVGElement(null, true, viewBox, null, null);
}


The complete code can be found here.

SVG QR code Java (programming language) Snippet (programming)

Published at DZone with permission of Mohamed Sanaulla, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Should You Create Your Own E-Signature API?
  • Import a Function/Module in Dataweave
  • Using Barcodes in iText 7
  • Dynamically Evaluate Dataweave Scripts

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: