博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
生成二维码
阅读量:7076 次
发布时间:2019-06-28

本文共 3109 字,大约阅读时间需要 10 分钟。

package com.youngdeer.DbConnect;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Graphics2D;import java.awt.geom.RoundRectangle2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.Hashtable;import javax.imageio.ImageIO;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;public class QrcodeTest {		private static final int BLACK = 0xFF000000;//用于设置图案的颜色  		private static final int WHITE = 0xFFFFFFFF; //用于背景色  	public static void main(String args[]){		String text = "http://youngdeer.top:8080/apidoc/";		int width = 800;		int height = 800;		String format = "png";		Hashtable hints = new Hashtable();		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);		hints.put(EncodeHintType.MARGIN, 2);		try {			BitMatrix matrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);			BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);			for (int x = 0; x < width; x++) {				for (int y = 0; y < height; y++) {					image.setRGB(x, y, (matrix.get(x, y) ? BLACK : WHITE));				}			}			image = new QrcodeTest().LogoMatrix(image);			File file=new File("D:/new.png");//在D盘生成二维码图片			ImageIO.write(image, format, file);		} catch (Exception e) { 			e.printStackTrace();		}	}	    /**     * 设置 logo      * @param matrixImage 源二维码图片     * @return 返回带有logo的二维码图片     * @throws IOException     */      public BufferedImage LogoMatrix(BufferedImage matrixImage) throws IOException{           /**          * 读取二维码图片,并构建绘图对象          */          Graphics2D g2 = matrixImage.createGraphics();                      int matrixWidth = matrixImage.getWidth();           int matrixHeigh = matrixImage.getHeight();                       /**          * 读取Logo图片          */          BufferedImage logo = ImageIO.read(new File("D:/jcptapp.png"));              //开始绘制图片          g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//绘制               BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);            g2.setStroke(stroke);// 设置笔画对象          //指定弧度的圆角矩形          RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);          g2.setColor(Color.white);          g2.draw(round);// 绘制圆弧矩形          //设置logo 有一道灰色边框          BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);            g2.setStroke(stroke2);// 设置笔画对象          RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);          g2.setColor(new Color(128,128,128));           g2.draw(round2);// 绘制圆弧矩形          g2.dispose();          matrixImage.flush() ;          return matrixImage ;       } }

  

转载于:https://www.cnblogs.com/hutton/p/10416709.html

你可能感兴趣的文章
图书馆管理系统程序设计
查看>>
WebService Rest接收大量数据出现基础连接已经关闭的解决方案
查看>>
小R的烦恼 BZOJ3280
查看>>
左神算法基础班4_5折纸问题
查看>>
float运算精度丢失解决方法
查看>>
DNS 工作原理是什么,域名劫持、域名欺骗、域名污染又是什么
查看>>
settings配置数据库和日志
查看>>
【整理】SYSCOMMAND的wParam值的宏定义
查看>>
.net Application的目录
查看>>
洛谷 P1313 计算系数 Label:杨辉三角形 多项式计算
查看>>
YUV色彩空间(转自百度百科)
查看>>
创建服务
查看>>
Sencha Touch 2.1学习图表Chart概述
查看>>
NYOJ467 中缀式变后缀式
查看>>
qwq
查看>>
C#中timer类的用法
查看>>
shell脚本学习
查看>>
自动化交易机器人Beta猪
查看>>
最少的次数
查看>>
用vue开发单页应用的一些心得
查看>>