アットウィキロゴ

QR

package b;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import javax.imageio.ImageIO;
 
 
public class pro{
 
    public static void main(String[] args) throws WriterException, IOException {
 
        //QRコード生成したい文字列
        String source = "タロウ";
        //QRコード生成時のエンコーディング
        String encoding = "UTF-8";
        //サイズ(ピクセル)
        int size = 100;
        //画像ファイルの保存先
        String filePath = "qr_code.png";
 
        //生成処理
        ConcurrentHashMap hints = new ConcurrentHashMap();
        //エラー訂正レベル指定
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        //エンコーディング指定
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
        //マージン指定
        hints.put(EncodeHintType.MARGIN, 0);
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix bitMatrix = writer.encode(source, BarcodeFormat.QR_CODE, size, size, hints);
        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
 
        //ファイルへの保存処理
        ImageIO.write(image, "png", new File(filePath));
    }
}
最終更新:2016年11月29日 18:48