Last active
March 5, 2022 00:20
-
-
Save Jerry0022/f786075b682a0d3dee90 to your computer and use it in GitHub Desktop.
How to generate a QR Code from a String and show it in JavaFX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Jerry on 24.09.2015. | |
*/ | |
import javafx.application.Application; | |
import javafx.scene.Scene; | |
import javafx.scene.image.Image; | |
import javafx.scene.image.ImageView; | |
import javafx.scene.layout.BorderPane; | |
import javafx.stage.Stage; | |
import net.glxn.qrgen.QRCode; | |
import net.glxn.qrgen.image.ImageType; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
// QRCode Gradle dependency: compile 'net.glxn:qrgen:1.4' | |
public class QRCodeGenerator extends Application { | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) { | |
// GENERATE QR CODE | |
ByteArrayOutputStream out = QRCode.from("LT Jerry0022").to(ImageType.PNG).withSize(200, 200).stream(); | |
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | |
// SHOW QR CODE | |
BorderPane root = new BorderPane(); | |
Image image = new Image(in); | |
ImageView view = new ImageView(image); | |
view.setStyle("-fx-stroke-width: 2; -fx-stroke: blue"); | |
root.setCenter(view); | |
Scene scene = new Scene(root, 200, 200); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to save generated image in my project