qr code
QR Code Generator using JavaFX & Fxml

QR Code Generator using JavaFX & Fxml

Posted on

This is a Java program for generating QR codes. It uses the QRGen library to create QR codes from text entered in a TextArea field.

QR Code

The program’s UI is designed using JavaFX, and it contains three elements: a TextArea to enter the text, a Button to generate the QR code, and an ImageView to display the generated QR code.

The program’s logic is implemented in the generateBtn() method, which is called when the user clicks on the generate button. The method first retrieves the user-entered text from the TextArea using the getText() method. It then creates a new QRCode object using the from() method of the QRGen library, passing in the user-entered text as a parameter. The to() method is then called on this object to convert the QR code to a PNG image format. The resulting image is stored in a ByteArrayOutputStream object, which is then converted to an InputStream object using the toByteArray() method. Finally, the InputStream object is used to create a new Image object, which is then displayed in the ImageView element using the setImage() method. The TextArea and Button elements are hidden from view after the QR code is generated using the setVisible() method.

This program could be used to generate QR codes for a variety of applications, such as creating QR codes for links, contact information, or product information.

package qrcodegenerater;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import net.glxn.qrgen.QRCode;
import net.glxn.qrgen.image.ImageType;

public class MyqrController implements Initializable {

    TextArea textArea;
    Button gBtn;
    ImageView showQRImg;

    public void initialize(URL url, ResourceBundle rb) {


    public void generateBtn(ActionEvent event) throws FileNotFoundException, IOException, InterruptedException {
        String getUserData = textArea.getText().toString().trim();
        ByteArrayOutputStream out = QRCode.from(getUserData).to(ImageType.PNG).stream();
        InputStream in = new ByteArrayInputStream(out.toByteArray());
        Image image = new Image(in);


This is an XML file that defines the user interface (UI) for a JavaFX program for generating QR codes.

The UI is made up of an AnchorPane that contains three child elements: a TextArea for entering text, a Button for generating the QR code, and an ImageView for displaying the generated QR code.

The attributes of the UI elements are specified using XML tags. The TextArea element has an fx:id attribute that is used to identify the element in the Java code. It also has a promptText attribute that provides a hint to the user about what to enter in the field. The Button element also has an fx:id attribute and an onAction attribute that specifies the method to be called when the button is clicked. The ImageView element has an fx:id attribute, and its attributes specify how the generated image should be displayed, such as the fitHeight and fitWidth attributes that specify the dimensions of the image.

The XML file also includes a stylesheet tag that references a separate CSS file that defines the styles for the UI elements.

Overall, this XML file defines the UI for the QR code generator program, which can be customized by modifying the attributes of the UI elements. The Java code that implements the program’s logic interacts with these UI elements to generate the QR code from user-entered text and display it in the ImageView element.

<?xml version="1.0" encoding="UTF-8"?>

<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="375.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="qrcodegenerater.MyqrController">
        <URL value="@myqr.css" />
      <TextArea fx:id="textArea" layoutX="24.0" layoutY="43.0" prefHeight="275.0" prefWidth="324.0" promptText="Enter your text or link">
            <Font size="18.0" />
      <Button fx:id="gBtn" layoutX="24.0" layoutY="333.0" mnemonicParsing="false" onAction="#generateBtn" prefHeight="39.0" prefWidth="324.0" text="Generate">
            <Font name="System Bold" size="18.0" />
      <ImageView fx:id="showQRImg" fitHeight="218.0" fitWidth="296.0" layoutX="38.0" layoutY="65.0" pickOnBounds="true" preserveRatio="true" />