JavaFx 生成二维码工具类封装
Posted Stars-one
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaFx 生成二维码工具类封装相关的知识,希望对你有一定的参考价值。
原文地址: JavaFx 生成二维码工具类封装 - Stars-One的杂货小窝
之前星之音乐下载器有需要生成二维码功能,当时用的是一个开源库来实现的,但是没过多久,发现那个库依赖太多,有个http-client的依赖,把软件都搞大了一倍,而且有时候开发的时候下载依赖还报错,就想换个方案
于是在网上找了下解决方案,最终只需要依赖两个zxing的两个依赖即可实现功能
本文基于TornadoFx框架进行编写,封装工具代码是kotlin版本,工具类已经封装在common-controls库中
工具支持带logo图标,带底部文本的二维码生成
代码封装
1.引入依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.0</version>
</dependency>
2.使用
由于工具代码过多不便阅读,就先讲些使用,工具代码就放下面了
比较核心的就两个方法,如下面代码所示,其他的方法是带Swing关键字,就是生成Swing包中的Image对象
getQRcodeFxImg()
方法就是直接生成Fx的Image对象,可以JavaFx中直接使用
/**
* 初始化设置
*
* @param qrcodeSize 二维码尺寸,默认为320(即320*320)
* @param logoSize logo图标尺寸,默认为80(即80*80)
* @param bottomTextSize 底部文字大小,默认20px
* @param qrcodeType 二维码图片格式,默认为png
*/
fun initConfig(qrcodeSize: Int = 320, logoSize: Int = 80, bottomTextSize: Int = 20, qrcodeType: String = "PNG")
/**
* 生成二维码图片
*
* @param data 二维码文本内容
* @param logoPath 图标图片的路径
* @param bottomText 底部文字
* @return fx的img对象
*/
fun getQRcodeFxImg(data: String?, logoPath: String?=null, bottomText: String?=null): WritableImage
使用的话也比较简单:
//得到的swing的image对象
val buImg = QRCodeUtil.getQRcodeFxImg("这是测试文本")
val buImg1 = QRCodeUtil.getQRcodeFxImg("这是测试文本", null, "底部文字")
val buImg2 = QRCodeUtil.getQRcodeFxImg("这是测试文本", "/x5.jpg", "底部文字")
val list = listOf(buImg, buImg1, buImg2)
hbox(20.0)
list.forEach
imageview(it)
fitWidth = 200.0
fitHeight = 200.0
3.工具库代码
/**
* 二维码生成工具类
* Created by stars-one
*/
object QRCodeUtil
private var QRCODE_SIZE = 320 // 二维码尺寸,宽度和高度均是320
private var LOGO_SIZE = 80 // 二维码里logo的尺寸,宽高一致 80*80
private var BOTTOM_TEXT_SIZE = 20 // 底部文本的文字大小
private var FORMAT_TYPE = "PNG" // 二维码图片类型
/**
* 初始化设置
*
* @param qrcodeSize 二维码尺寸,默认为320(即320*320)
* @param logoSize logo图标尺寸,默认为80(即80*80)
* @param bottomTextSize 底部文字大小,默认20px
* @param qrcodeType 二维码图片格式,默认为png
*/
fun initConfig(qrcodeSize: Int = 320, logoSize: Int = 80, bottomTextSize: Int = 20, qrcodeType: String = "PNG")
QRCODE_SIZE = qrcodeSize
LOGO_SIZE = logoSize
BOTTOM_TEXT_SIZE = bottomTextSize
FORMAT_TYPE = qrcodeType
/**
* 生成二维码图片
*
* @param data 二维码文本内容
* @param logoPath 图标图片的路径
* @param bottomText 底部文字
* @return
*/
fun getQRcodeFxImg(data: String?, logoPath: String?=null, bottomText: String?=null): WritableImage
val resources = ResourceLookup(this)
val url = if (logoPath == null)
null
else
resources.url(logoPath)
val swingImg = getQRCodeSwingImg(data, url, bottomText)
return SwingFXUtils.toFXImage(swingImg,null)
/**
* 默认需要logo,无底部文字
* 返回 BufferedImage 可以使用ImageIO.write(BufferedImage, "png", outputStream);输出
*
* @param dataStr
* @return 返回 BufferedImage 可以使用ImageIO.write(BufferedImage, "png", outputStream);输出
*/
@Throws(Exception::class)
fun getQRCodeSwingImg(dataStr: String?): BufferedImage
return getQRCodeSwingImg(dataStr, null, null)
/**
* 默认需要logo,无底部文字
*
* @param dataStr
* @return 返回字节数组
*/
@Throws(Exception::class)
fun getQRCodeByte(dataStr: String?): ByteArray
val bufferedImage = getQRCodeSwingImg(dataStr, null, null)
val outputStream = ByteArrayOutputStream()
ImageIO.write(bufferedImage, FORMAT_TYPE, outputStream)
return outputStream.toByteArray()
/**
* 默认需要logo,包含底部文字 文字为空则不显示文字
* 返回 BufferedImage 可以使用ImageIO.write(BufferedImage, "png", outputStream);输出
*
* @param dataStr
* @return
*/
@Throws(Exception::class)
fun getQRCodeSwingImg(dataStr: String?, bottomText: String?): BufferedImage
return getQRCodeSwingImg(dataStr, null, bottomText)
/**
* 默认需要logo,包含底部文字 文字为空则不显示文字
*
* @param dataStr
* @return 返回字节数组
*/
@Throws(Exception::class)
fun getQRCodeByte(dataStr: String?, bottomText: String?): ByteArray
val bufferedImage = getQRCodeSwingImg(dataStr, null, bottomText)
val outputStream = ByteArrayOutputStream()
ImageIO.write(bufferedImage, FORMAT_TYPE, outputStream)
return outputStream.toByteArray()
/**
* 获取二维码图片
*
* @param dataStr 二维码内容
* @param needLogo 是否需要添加logo
* @param bottomText 底部文字 为空则不显示
* @return
*/
@Throws(Exception::class)
fun getQRCodeSwingImg(dataStr: String?, url: URL?, bottomText: String?): BufferedImage
if (dataStr == null)
throw RuntimeException("未包含任何信息")
val hints = HashMap<EncodeHintType, Any?>()
hints[EncodeHintType.CHARACTER_SET] = "utf-8" //定义内容字符集的编码
hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.L //定义纠错等级
hints[EncodeHintType.MARGIN] = 1
val qrCodeWriter = QRCodeWriter()
val bitMatrix = qrCodeWriter.encode(dataStr, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints)
val width = bitMatrix.width
val height = bitMatrix.height
var tempHeight = height
if (StringUtils.isNotBlank(bottomText))
tempHeight = tempHeight + 12
val image = BufferedImage(width, tempHeight, BufferedImage.TYPE_INT_RGB)
for (x in 0 until width)
for (y in 0 until height)
image.setRGB(x, y, if (bitMatrix[x, y]) -0x1000000 else -0x1)
// 判断是否添加logo
if (url != null)
insertLogoImage(image, url)
// 判断是否添加底部文字
if (StringUtils.isNotBlank(bottomText))
addFontImage(image, bottomText)
return image
/**
* 插入logo图片
*
* @param source 二维码图片
* @throws Exception
*/
@Throws(Exception::class)
private fun insertLogoImage(source: BufferedImage, url: URL)
var src: Image = ImageIO.read(url)
val width = LOGO_SIZE
val height = LOGO_SIZE
val image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH)
val tag = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
val g = tag.graphics
g.drawImage(image, 0, 0, null) // 绘制缩小后的图
g.dispose()
src = image
// 插入LOGO
val graph = source.createGraphics()
val x = (QRCODE_SIZE - width) / 2
val y = (QRCODE_SIZE - height) / 2
graph.drawImage(src, x, y, width, height, null)
val shape: Shape = RoundRectangle2D.Float(x.toFloat(), y.toFloat(), width.toFloat(), width.toFloat(), 6f, 6f)
graph.stroke = BasicStroke(3f)
graph.draw(shape)
graph.dispose()
private fun addFontImage(source: BufferedImage, declareText: String?)
//生成image
val defineWidth = QRCODE_SIZE
val defineHeight = 20
val textImage = BufferedImage(defineWidth, defineHeight, BufferedImage.TYPE_INT_RGB)
val g2 = textImage.graphics as Graphics2D
//开启文字抗锯齿
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
g2.background = Color.WHITE
g2.clearRect(0, 0, defineWidth, defineHeight)
g2.paint = Color.BLACK
val context = g2.fontRenderContext
//部署linux需要注意 linux无此字体会显示方块
val font = Font("宋体", Font.BOLD, BOTTOM_TEXT_SIZE)
g2.font = font
val lineMetrics = font.getLineMetrics(declareText, context)
val fontMetrics: FontMetrics = FontDesignMetrics.getMetrics(font)
val offset = ((defineWidth - fontMetrics.stringWidth(declareText)) / 2).toFloat()
val y = (defineHeight + lineMetrics.ascent - lineMetrics.descent - lineMetrics.leading) / 2
g2.drawString(declareText, offset.toInt(), y.toInt())
val graph = source.createGraphics()
//开启文字抗锯齿
graph.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
//添加image
val width = textImage.getWidth(null)
val height = textImage.getHeight(null)
val src: Image = textImage
graph.drawImage(src, 0, QRCODE_SIZE - 8, width, height, Color.WHITE, null)
graph.dispose()
二维码生成工具类
1 package com.ideal.common.util; 2 3 import java.awt.image.BufferedImage; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.FileOutputStream; 8 import java.util.UUID; 9 10 import net.glxn.qrgen.core.image.ImageType; 11 import net.glxn.qrgen.javase.QRCode; 12 13 /** 14 * 15 * 二维码生成工具类 16 *@author:daipeng 17 *@date:2017年6月15日 18 */ 19 public class MyQRCodeUtils { 20 /** 21 * 22 * 23 * @Title: getCode 24 * @Description: 生成二维码 25 * @author:daipeng 26 * @param: @param mCode 二维码的url地址 27 * @param: @param pathName 二维码全路径名称唯一,否则会覆盖 28 * @param: @return 29 * @param: @throws Exception 30 * @return: String 31 * @throws 32 */ 33 public static void getCode(String mCode,String pathName) throws Exception{ 34 File qrCode = QRCode.from(mCode).withCharset("UTF-8").to(ImageType.PNG).withSize(250, 250).file(); 35 FileInputStream inputStream = new FileInputStream(qrCode); 36 FileOutputStream outputStream = new FileOutputStream(new File(pathName+".png")); 37 byte[] buffer = new byte[1024]; 38 int i = -1; 39 while ((i = inputStream.read(buffer)) != -1) { 40 outputStream.write(buffer, 0, i); 41 } 42 outputStream.flush(); 43 outputStream.close(); 44 inputStream.close(); 45 } 46 47 public static File getCode2(String mCode,String pathName) throws Exception{ 48 File qrCode = QRCode.from(mCode).withCharset("UTF-8").to(ImageType.PNG).withSize(250, 250).file(); 49 return qrCode; 50 } 51 52 53 public static void main(String[] args) throws Exception { 54 String mCode = "advd"; 55 String pathName = "E:\\1"; 56 getCode(mCode,pathName); 57 } 58 }
以上是关于JavaFx 生成二维码工具类封装的主要内容,如果未能解决你的问题,请参考以下文章