说明:此实例为生成二维码名片的实例
需要Qrcode_swetake.jar
接下来将jar包引入到项目中,就可以实现二维码生成了
String content="BEGIN:VCARD\n"
+ "VERSION:3.0\n"
+ "FN:姓名\n"
+ "TEL;CELL;VOICE:手机\n"
+ "TEL;WORK;VOICE:手机\n"
+ "EMAIL;PREF;INTERNET:邮箱\n"
+ "URL:网址\n"
+ "TITLE:职位\n"
+ "ROLE: 职称\n"
+ "ADR;WORK;POSTAL:地址;\n"
+ "ORG:保 密\n"
+ "NOTE:QQ:\n"
+ "END:VCARD";
----------------------------------------------
package com.code;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
public class QrCodeTest {
public static void main(String[] args) throws UnsupportedEncodingException{
Qrcode qrcode = new Qrcode();
//set 表示设置 get 表示获取
//纠错率 模式 版本
//设置模式为二进制
qrcode.setQrcodeEncodeMode(‘B‘);
//设置纠错率
qrcode.setQrcodeErrorCorrect(‘M‘);
//设置版本号 1-40
qrcode.setQrcodeVersion(12);
//专利 , Java作出一个产品 有自己特色。申请产权和专利
String content="BEGIN:VCARD\n"
+ "VERSION:3.0\n"
+ "FN:姓名\n"
+ "TEL;CELL;VOICE:手机\n"
+ "TEL;WORK;VOICE:手机\n"
+ "EMAIL;PREF;INTERNET:邮箱\n"
+ "URL:网址\n"
+ "TITLE:职位\n"
+ "ROLE: 职称\n"
+ "ADR;WORK;POSTAL:地址;\n"
+ "ORG:保 密\n"
+ "NOTE:QQ:\n"
+ "END:VCARD";
//尺寸(根据二维码信息量的大小灵活更改)
int width=330,height=330;
BufferedImage bufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//创建画图工具
Graphics2D grap = bufImage.createGraphics();
//grap.setBackground(Color.WHITE);
grap.setBackground(new Color(255, 255, 255));
grap.clearRect(0,0,width,height);
grap.setColor(new Color(0, 0, 255));
//boolean 只有两个 要么true 要么是false
boolean[][] qrcodeBytes = qrcode.calQrcode(content.getBytes("UTF-8"));
int qrcodeWidth=5;
for(int i = 0;i<qrcodeBytes.length;i++){
for(int j = 0;j<qrcodeBytes.length;j++){
if(qrcodeBytes[j][i]){
grap.fillRect(j*qrcodeWidth, i*qrcodeWidth, qrcodeWidth, qrcodeWidth);
}
}
}
try {
//grap.drawImage(ImageIO.read(new File("D:\\as.png")),50,50,50,50,null);
//grap.drawImage(ImageIO.read(new File("D:\\as.png")),50,190,40,40,null);
grap.drawImage(ImageIO.read(new File("D:\\code/dzq.jpg")),125,50,80,80,null);
grap.drawImage(ImageIO.read(new File("D:\\code/wn.png")),145,190,40,40,null);
grap.dispose();
bufImage.flush();
ImageIO.write(bufImage, "JPEG",new File("D:\\code/new-card-2015.png"));
System.out.println("创建成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
本文出自 “wennuanyiran” 博客,转载请与作者联系!
原文地址:http://dingzhaoqiang.blog.51cto.com/5601059/1697768