duminică, 15 decembrie 2019

QR Code generator in Java FX

Generatorul de cod QR,




















package javafx_qrcodewriter;

import com.google.zxing.BarcodeFormat; //trebue de creeat
import com.google.zxing.WriterException;  //trebue de creeat

import com.google.zxing.common.BitMatrix;  //trebue de creeat
import com.google.zxing.qrcode.QRCodeWriter;  //trebue de creeat
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
 *
 * viorel
 */
public class JavaFX_QRCodeWriter extends Application {
 
    private Stage primaryStage;
    private ImageView qrView;
    private AnchorPane canvas;
    private FileChooser fileChooser;
    @Override
    public void start(Stage primaryStage) {
        final FileChooser fileChooser = new FileChooser();
        //creare
        Label lText = new Label("Introdu textul pentru cod");
        TextField tfTextQrCod = new TextField();
        Button btn = new Button();
        Button btnSalveaza = new Button();
        ImageView qrView = new ImageView();
        //end creare

        //pozitionare
        lText.setLayoutX(20);
        lText.setLayoutY(20);

        tfTextQrCod.setLayoutX(20);
        tfTextQrCod.setLayoutY(40);

        btn.setLayoutX(200);
        btn.setLayoutY(40);
        btn.setText("Genereaza QR_Code");
     
        btnSalveaza.setLayoutX(120);
        btnSalveaza.setLayoutY(450);
        btnSalveaza.setText("Salveaza ca PNG");

        //end pozitie

        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                QRCodeWriter qrCodeWriter = new QRCodeWriter();
                String myWeb = tfTextQrCod.getText();
                int width = 350;
                int height = 350;
                String fileType = "png";

                BufferedImage bufferedImage = null;
                try {
                    BitMatrix byteMatrix = qrCodeWriter.encode(myWeb, BarcodeFormat.QR_CODE, width, height);
                    bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                    bufferedImage.createGraphics();

                    Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics();
                    graphics.setColor(Color.WHITE);
                    graphics.fillRect(0, 0, width, height);
                    graphics.setColor(Color.BLACK);

                    for (int i = 0; i < height; i++) {
                        for (int j = 0; j < width; j++) {
                            if (byteMatrix.get(i, j)) {
                                graphics.fillRect(i, j, 1, 1);
                            }
                        }
                    }

                    System.out.println("Success...");

                    qrView.setImage(SwingFXUtils.toFXImage(bufferedImage, null));

                } catch (WriterException ex) {
                    Logger.getLogger(JavaFX_QRCodeWriter.class.getName()).log(Level.SEVERE, null, ex);
                }
                System.out.println(tfTextQrCod.getText());
            }
        });
     
 
     
        Pane root = new Pane();
        root.getChildren().addAll(lText, tfTextQrCod, btn,  btnSalveaza);
   
        canvas = new AnchorPane();//suprafata de desenare a tutoirpor figurelor oinclusiv text si imagini
   
        root.getChildren().add(canvas);
        canvas.setLayoutX(0);
        canvas.setLayoutY(80);
        canvas.getChildren().add(qrView);
     
        Scene scene = new Scene(root, 340, 500);
     
        primaryStage.setTitle("QR-Codul tau");
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.show();
    }
        public static void main(String[] args) {
        launch(args);
    }
}


Niciun comentariu:

Trimiteți un comentariu