giovedì 7 marzo 2013

Creazione automatica QrCode in Java/Netbeans

Per generare QrCode in modo automatico in modo semplice si puo' utilizzare il metodo presentato a questo indirizzo

 Si deve scaricare la libreria XZing e si devono aggiungere al progetto le librerie core e javase contenute nelle sottodirectory del file zip


il codice e' stato modificato in modo da accettare in input sulla riga di comando (nella variabile args) il contenuto del QrCode. Questa motiva permette mediante una procedura batch di creare in automatico quanti QrCode si desiderino

-----------------------------------------
package qrcode;


import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Hashtable;

import com.google.zxing.client.j2se.MatrixToImageWriter;


/**
 *
 * @author l.innocenti
 */
public class QrCode {

    public static void main(String[] args) {
        Charset charset = Charset.forName("UTF-8");
        CharsetEncoder encoder = charset.newEncoder();
        byte[] b = null;
            try {
                // Convert a string to UTF-8 bytes in a ByteBuffer
                ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(args[0]));
                b = bbuf.array();
            } catch (CharacterCodingException e) {
                System.out.println(e.getMessage());
            }

            String data;
            try {
                data = new String(b, "UTF-8");
                // get a byte matrix for the data
                BitMatrix matrix = null;
                int h = 100;
                int w = 100;
                com.google.zxing.Writer writer = new MultiFormatWriter();
                try {
                    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2);
                    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                    matrix = writer.encode(data,
                    com.google.zxing.BarcodeFormat.QR_CODE, w, h, hints);
                } catch (com.google.zxing.WriterException e) {
                    System.out.println(e.getMessage());
                }

                // change this path to match yours (this is my mac home folder, you can use: c:\\qr_png.png if you are on windows)
                        String filePath = args[0]+".png";
                File file = new File(filePath);
                try {
                    MatrixToImageWriter.writeToFile(matrix, "PNG", file);
                    System.out.println("printing to " + file.getAbsolutePath());
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            } catch (UnsupportedEncodingException e) {
                System.out.println(e.getMessage());
            }
            }
}
-----------------------------------------
Il risultato e' una immagine Png con nome uguale al contenuto del QrCode 
Per creare in automatico 1000 numeri QrCode di numeri progressivi si puo' creare un file batch su Windows con la seguente linea
-------------------------------------------------------------

FOR /L %%A IN (1,1,1000) DO java -jar Qrcode.jar %%A
-------------------------------------------------------------

mentre in Linux
-------------------------------------------------------------

#!/bin/bash
for i in {1..1000}
do
   java -jar Qrcode.jar $i
done
--------------------------------------------------------------


Nessun commento:

Posta un commento

FTP Bash script

 Uno script rubato su stack overflow per fare l'upload automatico su FTP senza criptazione   #!/bin/sh HOST='ftp.xxxxx.altervista.or...