java生成一维码和二维码,mybatis原理和实现机制

本文介绍了如何使用Java生成一维码和二维码,包括MatrixUtil类的详细方法如toQRCodeMatrix和toBarCodeMatrix。此外,还展示了MatrixToImageWriter和QRCodeFactory类用于自定义二维码,如添加logo和文字。同时提到了MyBatis的相关内容,但未展开详细讨论。
摘要由CSDN通过智能技术生成

}

}

return image;

}

/**

  • 将字符串编成一维条码的矩阵

  • @param str

  • @param width

  • @param height

  • @return

*/

public static BitMatrix toBarCodeMatrix(String str, Integer width,

Integer height) {

if (width == null || width < 200) {

width = 200;

}

if (height == null || height < 50) {

height = 50;

}

try {

// 文字编码

Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();

hints.put(EncodeHintType.CHARACTER_SET, CHARSET);

BitMatrix bitMatrix = new MultiFormatWriter().encode(str,

BarcodeFormat.CODE_128, width, height, hints);

return bitMatrix;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

  • 根据矩阵、图片格式,生成文件。

*/

public static void writeToFile(BitMatrix matrix, String format, File file)

throws IOException {

BufferedImage image = toBufferedImage(matrix);

if (!ImageIO.write(image, format, file)) {

throw new IOException("Could not write an image of format "

  • format + " to " + file);

}

}

/**

  • 将矩阵写入到输出流中。

*/

public static void writeToStream(BitMatrix matrix, String format,

OutputStream stream) throws IOException {

BufferedImage image = toBufferedImage(matrix);

if (!ImageIO.write(image, format, stream)) {

throw new IOException("Could not write an image of format "

  • format);

}

}

/**

  • 解码,需要javase包。

  • @param file

  • @return

*/

/*public static String decode(File file) {

BufferedImage image;

try {

if (file == null || file.exists() == false) {

throw new Exception(" File not found:" + file.getPath());

}

image = ImageIO.read(file);

LuminanceSource source = new BufferedImageLuminanceSource(image);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值