利用Qrcode.jar生成二维码
2015-10-23 20:51
453 查看
package com.java.code;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.servlet.jsp.PageContext;
import com.swetake.util.Qrcode;
public class QrcodeTest {
public static void getQrcode(String content, String imgPath) {
try {
int width = 140;
int height = 140;
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(7);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
// 绘制二维码
Graphics2D gs = bi.createGraphics();
gs.setBackground(Color.white);
gs.clearRect(0, 0, width, height);
gs.setColor(Color.green);
// 获取内容,通过数组
byte[] contentType = content.getBytes("gb2312");
int pixoff = 2;
if (contentType.length > 0 && contentType.length < 120) {
boolean[][] codeOut = qrcode.calQrcode(contentType);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
} else {
System.out.println("erro");
}
gs.dispose();
bi.flush();
File imgFile = new File(imgPath);
ImageIO.write(bi, "png", imgFile);
System.out.println("ok");
} catch (Exception e) {
e.getMessage();
}
}
public static void main(String[] args) {
String content = "http://p4.qhimg.com/dmt/235_165_/t012097845e1edd19aa.jpg";
String imgPath = "E:\\java_code\\QrcodeDemo\\WebContent\\images\\temp.png";
getQrcode(content, imgPath);
}
}
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.servlet.jsp.PageContext;
import com.swetake.util.Qrcode;
public class QrcodeTest {
public static void getQrcode(String content, String imgPath) {
try {
int width = 140;
int height = 140;
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(7);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
// 绘制二维码
Graphics2D gs = bi.createGraphics();
gs.setBackground(Color.white);
gs.clearRect(0, 0, width, height);
gs.setColor(Color.green);
// 获取内容,通过数组
byte[] contentType = content.getBytes("gb2312");
int pixoff = 2;
if (contentType.length > 0 && contentType.length < 120) {
boolean[][] codeOut = qrcode.calQrcode(contentType);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
} else {
System.out.println("erro");
}
gs.dispose();
bi.flush();
File imgFile = new File(imgPath);
ImageIO.write(bi, "png", imgFile);
System.out.println("ok");
} catch (Exception e) {
e.getMessage();
}
}
public static void main(String[] args) {
String content = "http://p4.qhimg.com/dmt/235_165_/t012097845e1edd19aa.jpg";
String imgPath = "E:\\java_code\\QrcodeDemo\\WebContent\\images\\temp.png";
getQrcode(content, imgPath);
}
}
相关文章推荐
- YTU2506——切面条(蓝桥杯赛题)
- 写手Remoting测试工具
- java作业4
- Oracle安装后出现的问题
- Spring动态代理,aop 注解实现aop
- 7
- 编程的本质
- js拖拽功能
- MongoDB批量操作及与MySQL效率对比
- C语言小游戏—迷宫游戏的实现
- jquery获取表格中动态单元格内单选框和多选框的值
- python例子-Django之模型(Model_数据库)
- hdoj Happy Matt Friends 5119 (DP) 好题
- Mac OS上设置Django开发环境
- Storm drpc学习
- 调用网易有道词典api
- springmvc 请求响应时报异常
- 《设计模式》之工厂方法
- Boost源码学习二[内存管理二]
- POJ 3230 【DP】