您的位置:首页 > 编程语言 > Java开发

java实现二维码制作

2015-05-13 22:46 288 查看
最近,看到好多人在做生成二维码的功能,感觉很高大上,就想着自己也整一套二维码制作的代码。之前几个月就写好了,只是没有时间发表。前几个月很懒,博文也是断断续续的写,感觉这样很不好,所以,决定接下来要好好写。

package com.zsl.code;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import com.swetake.util.Qrcode;

/**
* 〈二维码制作〉
* @author    ZSL
* @version   V1.00 2015-5-13[版本号, YYYY-MM-DD]
* @see       [相关类/方法]
* @since     TP V1.0R001 [产品/模块版本]
*/
public class Code
{

/**
*〈生成二维码〉
* @param args void
* @throws IOException
*/

public static void createCode()
throws IOException
{
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(7);
int width = 140, height = 140;
BufferedImage bufImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D grap = bufImage.createGraphics();
//String content="http://user.qzone.qq.com/1064396300/main";
String content = "龙哥:我的理想是什么?我的目标是什么?我的钱途又在哪里?";

byte[] contentByte = content.getBytes("utf-8");
grap.setBackground(Color.WHITE);
grap.clearRect(0, 0, width, height);
grap.setColor(Color.BLACK);

boolean[][] codeOut = qrcode.calQrcode(contentByte);
for (int i = 0; i < codeOut.length; i++ )
{
for (int j = 0; j < codeOut.length; j++ )
{
if (codeOut[j][i])
{
grap.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);
}
}
}

File img = new File("E:\\code.png");
if ( !img.exists())
{
img.mkdir();
}
else
{
img.delete();
}
grap.dispose();
ImageIO.write(bufImage, "png", img);
JOptionPane.showMessageDialog(null, "二维码生成成功!");
}

public static void main(String[] args)
throws IOException
{
createCode();
}
}


二维码已经生成成功,唯一的缺点就是中间不带logo图片。

后期一定会改进的。

下面附上源码一份:java实现二维码制作源码

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 二维码