博客地址:https://ainyi.com/58

Java 开发精美艺术二维码

看到网络上各种各样的二维码层出不穷,好像很炫酷的样子,一时兴起,我也要制作这种炫酷二维码效果

例如:

根据以往例子

根据之前所做的小项目 java 开发二维码系统

以这个为基础,将实现精美艺术二维码

基本代码:

// 创建二维码对象
Qrcode qrcode = new Qrcode();
// 设置二维码的纠错级别
// L(7%) M(15%) Q(25%) H(30%)
qrcode.setQrcodeErrorCorrect('L'); // 一般纠错级别小一点
// 设置二维码的编码模式 Binary(按照字节编码模式)
qrcode.setQrcodeEncodeMode('B');
// 设置二维码的版本号 1-40  1:20*21    2:25*25   40:177*177
qrcode.setQrcodeVersion(7);
// 生成二维码中要存储的信息
String qrData = "https://ainyi.com";
// 设置一下二维码的像素
int width = 300;
int height = 300;
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 绘图
Graphics2D gs = bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height); // 清除下画板内容// 设置下偏移量,如果不加偏移量,有时会导致出错
int pixoff = 2;byte[] d = qrData.getBytes("utf-8");
if(d.length > 0 && d.length <120){boolean[][] s = qrcode.calQrcode(d);for(int i=0;i<s.length;i++){for(int j=0;j<s.length;j++){if(s[j][i]){gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3);}}}
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("E:/code/qrcode.png"));

准备工作

java 可以实现生成二维码,需要用到 Qrcode 的 jar 包

  1. java、jsp
  2. struts2 以及相关 jar 包
  3. Qrcode.jar
  4. 文件上传相关 jar 包
  • 自己编写摸索出来的艺术二维码算法
  • 响应式

实现流程

因为要实现精美艺术二维码,把黑白二维码的黑色部分,点状部分替换成有颜色的点,汇聚成一张精美的二维码

那么实现的关键点就是:==替换==

将制作好的小图片素材,按照编号命名,三个码眼使用大图片素材,其他使用不相同小图片素材,绘制二维码图片的时候,将画笔改为将插入图片素材 drawImage

根据不同类型的艺术二维码(不同的素材),使用不同的算法

话不多说,上代码

/*** QrcodeText 二维码* @author krry* @version 1.0**/
public class QrcodeText{private static int width = 975;private static int height = 975;// 设置偏移量,不设置可能导致解析出错private static int pixoff = 25;// 像素大小private static int pix = 25;// 二维码数组的长度private static int codeLength;// 随机数,生成[0,2]之间的随机整数,取长度为3的数组下标private static int max = 3;//素材图片容器private static BufferedImage image_eye;private static BufferedImage image11;private static BufferedImage image12;private static BufferedImage image13;private static BufferedImage image21;private static BufferedImage image22;private static BufferedImage image23;private static BufferedImage image31;private static BufferedImage image32;private static BufferedImage image33;private static BufferedImage image41;private static BufferedImage image42;private static BufferedImage image43;/*** 生成二维码* @param message  二维码内容* @param type   二维码类型 如:锁屏* @param filename     二维码类型下的标号  如1* @param arti     使用算法类型  如0  热门* @param transparent 是否透明     1:透明  0背景是白色* @param request  请求* @return*/public static String qrcode(String message,String type,String filename,String arti,String transparent,HttpServletRequest request){String pathName = null;FileOutputStream outputStream = null;try{//创建二维码对象Qrcode qrcode = new Qrcode();//设置二维码的纠错级别//L(7%) M(15%) Q(25%) H(30%)qrcode.setQrcodeErrorCorrect('L'); //一般纠错级别小一点//设置二维码的编码模式 Binary(按照字节编码模式)qrcode.setQrcodeEncodeMode('B');//设置二维码的版本号 1-40  1:20*21    2:25*25   40:177*177qrcode.setQrcodeVersion(5);//获取图片缓存流对象BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//获取画笔Graphics2D gs = image.createGraphics();//判断是否使用二维码背景颜色是透明if(transparent.equals("yes")){//设置透明image = gs.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);  gs = image.createGraphics();} else {gs.setBackground(Color.WHITE);gs.clearRect(0, 0, width, height);}//设置内容String content = message;byte[] contentsBytes = content.getBytes("utf-8");//二维码boolean[][] code = qrcode.calQrcode(contentsBytes);//获取二维码数组的长度codeLength = code.length;//码眼部分全部设置为falsefor(int i=0;i<7;i++){for(int j=0;j<7;j++){code[i][j]=false;}for(int j=codeLength-7;j<codeLength;j++){code[i][j]=false;code[j][i]=false;}}//获取资源地址String aspath = request.getServletContext().getRealPath("/resource");//加载图片loadImage(aspath,type,filename);//绘制二维码,选择算法if(arti.equals("0")) drawQrcodeHot(gs, code); //热门算法else if(arti.equals("1")) drawQrcodeOrdi(gs, code); //最初算法else if(arti.equals("2")) drawQrcodeRiTojiao(gs, code); //三角算法//如果类型不是单码,则装载背景图片,将二维码写进背景图片中,只有单码没有背景if(!type.equals("dan")){//装载背景图片BufferedImage imageBG = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\bg.jpg"));//获取背景图片的画笔Graphics2D bg = imageBG.createGraphics();//位置坐标int x = 0;int y = 0;//如果类型是方形,判断二维码在背景图片的位置,单码图片缩小到640像素if(type.equals("fang")){x = (imageBG.getWidth() - 640) / 2;y = (imageBG.getHeight() - 640) / 2;}//如果类型是名片,判断二维码在背景图片的位置,单码图片缩小到640像素if(type.equals("ming")){x = (imageBG.getWidth() - 100) / 2;y = (imageBG.getHeight() - 640) / 2;}//如果类型是锁屏,判断二维码在背景图片的位置,单码图片缩小到640像素if(type.equals("suo")){x = (imageBG.getWidth() - 640) / 2;y = (imageBG.getHeight() - 1100) / 2;}//将单码图片写进背景图片中,单码图片缩小到640像素bg.drawImage(image, x, y, 640, 640, null);//释放画笔bg.dispose();gs.dispose();//生成二维码图片String realPath = request.getRealPath("/");//String realPath = 服务器项目的地址;pathName = new Date().getTime()+".jpg";outputStream = new FileOutputStream(new File(realPath+"upload\\", pathName));ImageIO.write(imageBG, "jpg", outputStream);} else { //如果类型是单码,直接释放输出//释放画笔gs.dispose();//生成二维码图片String realPath = request.getRealPath("/");//String realPath = 服务器项目的地址;pathName = new Date().getTime()+".png";outputStream = new FileOutputStream(new File(realPath+"upload\\", pathName));ImageIO.write(image, "png", outputStream);}} catch (Exception e){e.printStackTrace();} finally {try {//关闭流outputStream.close();} catch (IOException e) {e.printStackTrace();}}return pathName;}/*** 加载图片素材* @param aspath* @param type* @param filename*/public static void loadImage(String aspath,String type,String filename){try {//加载码眼image_eye = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\eye.png"));//装载50*50的图片素材image11 = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\11.png"));image12 = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\12.png"));image13 = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\13.png"));//装载100*50的图片素材image21 = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\21.png"));image22 = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\22.png"));image23 = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\23.png"));//装载50*100的图片素材image31 = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\31.png"));image32 = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\32.png"));image33 = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\33.png"));//装载100*100的图片素材image41 = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\41.png"));image42 = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\42.png"));image43 = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\43.png"));} catch (Exception e) {e.printStackTrace();}}/*** 绘制 算法0  热门二维码   素材有50*50 50*100 100*50 100*100* @param gs 画笔* @param code 二维码数组*/public static void drawQrcodeHot(Graphics2D gs,boolean[][] code){//把图片素材放进数组BufferedImage[] img1 = {image11,image12,image13};BufferedImage[] img2 = {image21,image22,image23};BufferedImage[] img3 = {image31,image32,image33};BufferedImage[] img4 = {image41,image42,image43};//通用地绘制码眼gs.drawImage(image_eye, pix, pix, pix*7, pix*7, null);gs.drawImage(image_eye, (codeLength-7)*pix+pixoff, pix, pix*7, pix*7, null);gs.drawImage(image_eye, pix, (codeLength-7)*pix+pixoff, pix*7, pix*7, null);Random random = new Random();// 绘制for (int i = 0; i < codeLength; i++) {for (int j = 0; j < codeLength; j++) {if (code[i][j]) {if (i+1 < codeLength && j+1 < codeLength && code[i][j + 1] && code[i + 1][j + 1] && code[i + 1][j]){//随机取图片,画100*100的图int s4 = random.nextInt(max);gs.drawImage(img4[s4], i * pix + pixoff, j * pix + pixoff, pix * 2, pix * 2, null);code[i][j + 1] = code[i + 1][j] = code[i + 1][j + 1] = false;} else if(j+1 < codeLength && code[i][j+1]){//随机取图片,画50*100的图int s3 = random.nextInt(max);gs.drawImage(img3[s3], i * pix + pixoff, j * pix + pixoff, pix, pix*2, null);code[i][j+1] = false;} else if (i+1 < codeLength && code[i+1][j]) {//随机取图片,画100*50的图int s2 = random.nextInt(max);gs.drawImage(img2[s2], i * pix + pixoff, j * pix + pixoff, pix * 2, pix, null);code[i+1][j] = false;} else {//随机取图片,画50*50的图int s1 = random.nextInt(max);gs.drawImage(img1[s1], i * pix + pixoff, j * pix + pixoff, pix, pix, null);}}}}}/*** 绘制  算法2 二维码   素材有50*50 50*100 100*50 100*100  其中100*100是三个角的算法,右上角没有填充* @param gs 画笔* @param code 二维码数组*/public static void drawQrcodeRiTojiao(Graphics2D gs,boolean[][] code){System.out.println("三角啊");//把图片素材放进数组BufferedImage[] img1 = {image11,image12,image13};BufferedImage[] img2 = {image21,image22,image23};BufferedImage[] img3 = {image31,image32,image33};BufferedImage[] img4 = {image41,image42,image43};//通用地绘制码眼gs.drawImage(image_eye, pix, pix, pix*7, pix*7, null);gs.drawImage(image_eye, (codeLength-7)*pix+pixoff, pix, pix*7, pix*7, null);gs.drawImage(image_eye, pix, (codeLength-7)*pix+pixoff, pix*7, pix*7, null);Random random = new Random();// 绘制for (int i = 0; i < codeLength; i++) {for (int j = 0; j < codeLength; j++) {if (code[i][j]) {if (i+1 < codeLength && j+1 < codeLength && code[i][j + 1] && code[i + 1][j + 1]){//随机取图片,画100*100的图  右上角没有填充int s4 = random.nextInt(max);gs.drawImage(img4[s4], i * pix + pixoff, j * pix + pixoff, pix * 2, pix * 2, null);code[i][j + 1] = code[i + 1][j + 1] = false;} else if(j+1 < codeLength && code[i][j+1]){//随机取图片,画50*100的图int s3 = random.nextInt(max);gs.drawImage(img3[s3], i * pix + pixoff, j * pix + pixoff, pix, pix*2, null);code[i][j+1] = false;} else if (i+1 < codeLength && code[i+1][j]) {//随机取图片,画100*50的图int s2 = random.nextInt(max);gs.drawImage(img2[s2], i * pix + pixoff, j * pix + pixoff, pix * 2, pix, null);code[i+1][j] = false;} else {//随机取图片,画50*50的图int s1 = random.nextInt(max);gs.drawImage(img1[s1], i * pix + pixoff, j * pix + pixoff, pix, pix, null);}}}}}/*** 绘制  算法1 普通二维码   素材有50*50 50*100 50*150* @param gs 画笔* @param code 二维码数组*/public static void drawQrcodeOrdi(Graphics2D gs,boolean[][] code){//把图片素材放进数组BufferedImage[] img1 = {image11,image12,image13};BufferedImage[] img2 = {image21,image22,image23};BufferedImage[] img3 = {image31,image32,image33};//通用地绘制码眼gs.drawImage(image_eye, pix, pix, pix*7, pix*7, null);gs.drawImage(image_eye, (codeLength-7)*pix+pixoff, pix, pix*7, pix*7, null);gs.drawImage(image_eye, pix, (codeLength-7)*pix+pixoff, pix*7, pix*7, null);Random random = new Random();for(int i = 0;i < codeLength;i++){for(int j = 0;j < codeLength;j++){//1*3if(code[i][j]){if(i+2 < codeLength && code[i+1][j] && code[i+2][j]){//随机取图片  下标随机[0,2],画50*150的图int s3 = random.nextInt(max);gs.drawImage(img3[s3], j*25+pixoff, i*25+pixoff, 25, 75, null);code[i+2][j]=false;code[i+1][j]=false;}else if(i+1 < codeLength && code[i+1][j]){//1*2//随机取图片,画50*100的图int s2 = random.nextInt(max);gs.drawImage(img2[s2], j*25+pixoff, i*25+pixoff, 25, 50, null);code[i+1][j]=false;} else {//随机取图片,画50*50的图int s1 = random.nextInt(max);gs.drawImage(img1[s1], j*25+pixoff, i*25+pixoff, 25, 25, null);}}}}}
}

项目截图

说明

  1. 以下截图的项目,生成二维码的价格均是测试用例,并不会真的需要支付.....
  2. 目前只实现==单码==的艺术二维码生成,其他暂不支持~~
  3. 除了单码,其他二维码样例图片取自第九工场




相关地址

项目地址:https://www.ainyi.com/krry_AiQrcode

GitHub:https://github.com/Krryxa/krry_AiQrcode

欢迎 start

krryblog:https://ainyi.com

博客地址:https://ainyi.com/58

转载于:https://www.cnblogs.com/ainyi/p/9855969.html

分享:Java 开发精美艺术二维码相关推荐

  1. 【Java开发】生成二维码

    import com.swetake.util.Qrcode;import javax.imageio.ImageIO; import java.awt.*; import java.awt.imag ...

  2. 通通WPF随笔(3)——艺术二维码素材生成器

    最近公司让我开发一个条形码的生成控件,花了半天时间搞定觉得不过瘾,什么年代了该用二维码了吧.于是wiki了一下二维码的资料. 比较常见的就是QR码(Quick Response)即快速识别码,为了验证 ...

  3. 微信小程序 - 二维码数据解析,如何扫码进入开发版测试二维码数据

    1.生成二维码 在小程序开发中,经常需要用到通过分享小程序二维码,进行扫码进入小程序. 官方文档 生成小程序二维码接口,这个生成接口可以放置服务器使用. page:是扫码之后需要打开的小程序页面 sc ...

  4. Java入门123:一个老鸟的Java学习心得(二维码版)

    Java入门123:一个老鸟的Java学习心得(二维码版)清华大学出版社 Java入门123--一个老鸟的Java学习心得(二维码版)充分考虑了初学Java的种种困难,讲解细致入微,抽丝剥茧,层层推进 ...

  5. 艺术二维码生成神器!

    艺术二维码生成神器! 在这里,您可以让您的二维码进行个性化定制,您可以自定义二维码的icon,码块的形状,模式....您还可以设置二维码的背景,让您的二维码和背景图融为一体!     此外,最重大的功 ...

  6. Java:Java编程实现导出二维码

    Java:Java编程实现导出二维码 目录 输出结果 代码设计 输出结果 更新-- 代码设计 public class QRCodeUtil {private static final String ...

  7. 【二维码】艺术二维码生成

    一.艺术二维码 艺术二维码是在原有的黑白二维码的基础上根据二维码解析方式再配合新兴的设计方法,进行二维码美化,制作出的形态各异二维码,具体分为静态二维码设计.动态二维码设计.3D二维码设计.创意二维码 ...

  8. Java连接打印机打印二维码

    首先导入依赖 implementation 'com.google.zxing:core:3.5.1' implementation 'com.google.zxing:javase:3.5.1' 1 ...

  9. java根据内容生成二维码

    java根据内容生成二维码 一.首先导入jar包:google.zxing.jar jar包可以从maven网站下载:meven仓库 这里可以找到你想要的绝大多数jar包,可以是依赖,也可以是jar包 ...

最新文章

  1. Apache+php+tomcat+mysql整合手册
  2. Python核心编程(第二版)第六章部分习题代码
  3. mybatis开发dao的方法——(三)
  4. 打开git命令窗口_用动图的形式打开 10 大 Git 命令?
  5. RTT设备与驱动之PIN设备
  6. 【MySQL】计算 TPS,QPS 的方式
  7. GIMP永久保存选择的办法
  8. 组态王6.55连接MySql数据库(笔记)
  9. 210129阶段三调试、进程间通信-共享内存
  10. 639. 解码方法 II
  11. 前端学习(740):函数返回值注意事项
  12. 服务器需要指定dns吗,Windows Server2012 安装配置DNS服务器方法详解
  13. Maven中dependencyManagement标签和dependencies的区别
  14. Netty是如何解决粘包和拆包问题的
  15. 俯视角场景搭配潮流色彩,诠释海报唯美视角
  16. 变量、作用域和内存问题
  17. 【转】关于动态库和静态库
  18. 一文读懂质量保证和质量控制
  19. Vue3 script setup
  20. 欧拉计划(1~3)ps:以后看题一定要认真

热门文章

  1. 1121. Damn Single (25)
  2. Physical implementation —— LEF and DEF
  3. displayTag使用总结
  4. R语言patchwork包将多个ggplot2可视化结果组合起来、使用plot_annotation函数以及tag_level参数为组合图添加自定义编码序列(字符向量列表)
  5. Vue + ElementUI 实现后台管理系统模板 -- 前端篇(四):定义主页面
  6. 使用Google浏览器做真机页面调试
  7. Oracle数据库之coalesce函数的用法
  8. 网络准入控制产品发展
  9. 有哪些小巧舒适的蓝牙耳机?推荐几款小巧且舒适的蓝牙耳机
  10. 第一篇博客____望好风长吟