昨天晚上看了一个视频讲的是web端把名片搞成二维码的形式,觉得挺有意思的,不过我还是初学,所以就没在网页端实现,写了命令行程序.

虽然看着程序很短,不过写的过程中还是出了问题, 最致命的就是

Graphics2D.clearRect(0,0,235,235);
Graphics2D.setColor(Color.BLACK); 
这两句代码顺序搞反,导致生成的二维码异常,不能够被读取其中的信息. 当时检查了好久都没看出问题所在,其实现在来看先设置颜色再clean不就相当于没设置颜色吗,
所以以后一定要小心仔细,争取少犯这种低级错误
 
package com.fantasyado.qrcoder;
import com.swetake.util.Qrcode; import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner; public class Main
{ public static void main(String[] args)
{
// write your code here
String content="";
Scanner sc=new Scanner(System.in);
System.out.println("input your information,and ends with ok");
while(! content.endsWith("ok")){
content=content+"\n";
content=content.concat(sc.next());
} content=content.substring(1,content.length()-2);
// content=content.replaceAll("ok",""); System.out.println("input your filename ends with EnterKey");
String filename=sc.next();
String filepath="D:\\"+filename+".png";
new Main().drawQRCODE(content, filepath);
System.out.println("draw QR_code successfullly!");
System.out.println(content); }
public void drawQRCODE(String content,String filepath){
try {
Qrcode qrcode=new Qrcode(); qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(15);
int width= 235;
int height=235;
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2=image.createGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0,0,235,235);
g2.setColor(Color.BLACK); byte[] contentbytes=content.getBytes("utf-8");
boolean[][] codeout= qrcode.calQrcode(contentbytes);
for (int i = 0; i <codeout.length; i++) {
for (int j = 0; j < codeout.length; j++) { if (codeout[j][i]) g2.fillRect(j*3+2,i*3+2,3,3);
}
}
g2.dispose();
image.flush();
File imgFile = new File(filepath);
ImageIO.write(image, "png", imgFile);
}catch (Exception e){
e.printStackTrace();
}
}
}

最新文章

  1. [LeetCode] Repeated Substring Pattern 重复子字符串模式
  2. fiddler监听127.0.0.1或localhost
  3. [POJ1328]Radar Installation
  4. iOS开发多线程篇—线程间的通信
  5. Java-note-字符串连接
  6. Chrome 插件vimium快捷键大全
  7. The first day,I get a blogs!!
  8. [shiro学习笔记]第二节 shiro与web融合实现一个简单的授权认证
  9. 死磕 java集合之ConcurrentHashMap源码分析(三)
  10. 虚拟机中使用centos7搭建ftp服务器
  11. HBase学习——4.HBase过滤器
  12. Axure安装、破解、汉化全套
  13. Codeforces 982E Billiard 扩展欧几里德
  14. Vue实现用户自定义上传头像裁剪
  15. Oracle备份恢复简单过程以及中间的坑.
  16. ZOC7 for Mac连接CentOS7无法输入中文问题
  17. windows 清理利器
  18. javascript私有静态成员
  19. 【bzoj3451】Tyvj1953 Normal 期望+树的点分治+FFT
  20. jquery.sparkline.js简介

热门文章

  1. Security &#187; Authorization &#187; 基于视图的授权
  2. USR-BLE101配置
  3. poll()函数的使用
  4. Matrix
  5. CentOS7 安装MongoDB 3.0服务器
  6. OpenBSD内核之引导MBR
  7. php empty,isset,is_null比较(差异与异同)
  8. Jesen不等式
  9. vs启动调试很慢的解决办法
  10. c++内存对齐