简体   繁体   中英

How do I generate QR Code from a array list in java and store to database?

I want to make a QR Code from my row database, I just have a make method save for insert function and there is a qr code generator. The problem is Zxing method Bitmatrix to encode parameter was String. How to generate an array or get one or two field row data for QR Code?

private static ArrayList<PersonalInfo> pi = new ArrayList();
    private static String image_name = "";
    private static String filepath = "G:\\Netbeans Project\\Template_End_Project\\src\\endproject" + image_name + ".png";
    private static String charset = "UTF-8";
//Here some variable

public static void Save(PersonalInfo pi1) throws SQLException, WriterException, IOException {
        try {
            unique(pi1);
            PreparedStatement ps = ConnectionClass.getPreStatement("insert into Karyawan_info(id_pegawai, nik_ktp, nama_lengkap, jenis_kelamin,tanggal_lahir, "
                    + "tempat_lahir, alamat, nomor_handphone, email, jabatan, tanggal_bergabung, gaji, kontak_darurat, nomor_kontak_darurat, pas_foto)"
                    + "values ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )");
            ps.setInt(1, pi1.getId_pegawai());
            ps.setInt(2, pi1.getNik_ktp());
            ps.setString(3, pi1.getNama_lengkap());
            ps.setString(4, pi1.getJenis_kelamin());
            ps.setString(5, pi1.getTanggal_lahir());
            ps.setString(6, pi1.getTempat_lahir());
            ps.setString(7, pi1.getAlamat());
            ps.setInt(8, pi1.getNomor_handphone());
            ps.setString(9, pi1.getEmail());
            ps.setString(10, pi1.getJabatan());
            ps.setString(11, pi1.getTanggal_bergabung());
            ps.setDouble(12, pi1.getGaji());
            ps.setString(13, pi1.getKontak_darurat());
            ps.setInt(14, pi1.getNomor_kontak_darurat());
            ps.setBytes(15, pi1.getPas_foto());

            ps.execute();

        } catch (IllegalArgumentException e) {

            throw new IllegalArgumentException(e.getMessage() + "Record not saved");
        } ;
        Map< EncodeHintType, ErrorCorrectionLevel> hintMap = new HashMap< EncodeHintType, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        BitMatrix matrix = new MultiFormatWriter().encode(
                new ArrayList(pi, charset),
                BarcodeFormat.QR_CODE, 200, 200, hintMap);// My Problem
        MatrixToImageWriter.writeToFile(matrix, filepath.substring(filepath
                .lastIndexOf('.') + 1), new File(filepath));
        System.out.println("QR Code image created successfully!");
    ```

You can convert your fields to string with a delimiter you can use this string to make QR code. You can parse the string to create your class's instance in the same way.

Create a method to convert fields.

    public String personalInfoToString(PersonalInfo pi1) {
    final String dm = ";";
    return pi1.getId_pegawai() + dm + pi1.getNik_ktp() + dm +  pi1.getNama_lengkap() ...;
}

Create a method to parse fields from the string.

    public String stringToPersonalInfo(String personalInfo) {
    final String dm = ";";

    PersonalInfo pi = new PersonalInfo();
    String[] data = personalInfo.split(dm);

    pi.setId_pegawai(data[0]);
    pi.setNik_ktp(data[1])
    return pi;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM