分享:Java 开发精美艺术二维码
Java 开发精美艺术二维码
看到网络上各种各样的二维码层出不穷,好像很炫酷的样子,一时兴起,我也要制作这种炫酷二维码效果
例如:
根据之前所做的小项目 java 开发二维码系统
以这个为基础,将实现精美艺术二维码
基本代码:
Qrcode qrcode = new Qrcode(); qrcode.setQrcodeErrorCorrect('L'); qrcode.setQrcodeEncodeMode('B'); 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 包
- java、jsp
- struts2 以及相关 jar 包
- Qrcode.jar
- 文件上传相关 jar 包
因为要实现精美艺术二维码,把黑白二维码的黑色部分,点状部分替换成有颜色的点,汇聚成一张精美的二维码
那么实现的关键点就是:替换
将制作好的小图片素材,按照编号命名,三个码眼使用大图片素材,其他使用不相同小图片素材,绘制二维码图片的时候,将画笔改为将插入图片素材 drawImage
根据不同类型的艺术二维码(不同的素材),使用不同的算法
话不多说,上代码
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; 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; 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(); qrcode.setQrcodeErrorCorrect('L'); qrcode.setQrcodeEncodeMode('B'); qrcode.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; for(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; if(type.equals("fang")){ x = (imageBG.getWidth() - 640) / 2; y = (imageBG.getHeight() - 640) / 2; } if(type.equals("ming")){ x = (imageBG.getWidth() - 100) / 2; y = (imageBG.getHeight() - 640) / 2; } if(type.equals("suo")){ x = (imageBG.getWidth() - 640) / 2; y = (imageBG.getHeight() - 1100) / 2; } bg.drawImage(image, x, y, 640, 640, null); bg.dispose(); gs.dispose(); String realPath = request.getRealPath("/"); 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("/"); 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; } public static void loadImage(String aspath,String type,String filename){ try { image_eye = ImageIO.read(new FileInputStream(aspath+"\\images\\ImageMaker\\"+type+"\\"+filename+"\\eye.png")); 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")); 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")); 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")); 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(); } } 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]){ 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]){ 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]) { 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 { int s1 = random.nextInt(max); gs.drawImage(img1[s1], i * pix + pixoff, j * pix + pixoff, pix, pix, null); } } } } } 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]){ 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]){ 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]) { 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 { int s1 = random.nextInt(max); gs.drawImage(img1[s1], i * pix + pixoff, j * pix + pixoff, pix, pix, null); } } } } } 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++){ if(code[i][j]){ if(i+2 < codeLength && code[i+1][j] && code[i+2][j]){ 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]){ int s2 = random.nextInt(max); gs.drawImage(img2[s2], j*25+pixoff, i*25+pixoff, 25, 50, null); code[i+1][j]=false; } else { int s1 = random.nextInt(max); gs.drawImage(img1[s1], j*25+pixoff, i*25+pixoff, 25, 25, null); } } } } } }
- 以下截图的项目,生成二维码的价格均是测试用例,并不会真的需要支付…
- 目前只实现单码的艺术二维码生成,其他暂不支持~~
- 除了单码,其他二维码样例图片取自第九工场
项目地址:http://ele.ainyi.com/krry_AiQrcode
GitHub:https://github.com/Krryxa/krry_AiQrcode
欢迎 start
krryblog:https://ainyi.com