Java实现二维码功能的实例代码

网友投稿 187 2023-06-15


Java实现二维码功能的实例代码

最近突然想写一些文章,所以就陆陆续续的编写一些自我感觉有用的并且大家也可能用到的一些技术代码。方便互相学习交流。

今天这篇文章,主要是利用java实现二维码:

在写代码之前先讲一下整体思路,以方便更好更快捷的实现功能。

(1).首先要想实现二维码功能需要导入com.google.zxing的核心jar包,我这里导入的是core-3.2.1.jar;

(2).所谓二维码莫过于把需要的内容放入一张图片中,所以这里首先是创建一张带有参数内容的图片,方法如下:

private static BufferedImage createImage(String content, String imgPath, boolean needCompress) thIBHQsrows Exception {

Hashtable hints = new Hashtable();

hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

hints.put(EncodeHintType.CHARACTER_SET, CHARSET);

hints.put(EncodeHintType.MARGIN, 1);

BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,

hints);

int width = bitMatrix.getWidth();

int height = bitMatrix.getHeight();

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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);

}

}

if (imgPath == null || "".equals(imgPath)) {

return image;

}

// 插入图片

QRCodeUtil.insertImage(image, imgPath, needCompress);

return image;

}

//插入logo 如不需要logo可以执行此方法

private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {

File file = new File(imgPath);

if (!file.exists()) {

System.err.println("" + imgPath + " 该文件不存在!");

return;

}

Image src = ImageIO.read(new File(imgPath));

int width = src.getWidth(null);

int height = src.getHeight(null);

if (needCompress) { // 压缩LOGO

if (width > WIDTH) {

width = WIDTH;

}

if (height > HEIGHT) {

height = HEIGHT;

}

Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);

BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics g = tag.getGraphics();

g.drawImage(image, 0, 0, null); // 绘制缩小后的图

g.dispose();

src = image;

}

// 插入LOGO

Graphics2D graph = source.createGraphics();

int x = (QRCODE_SIZE - width) / 2;

int y = (QRCODE_SIZE - height) / 2;

graph.drawImage(src, x, y, width, height, null);

Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);

graph.setStroke(new BasicStroke(3f));

graph.draw(shape);

graph.dispose();

}

(3).至此一张二维码图片生成方法就写完了,是不是很简单,接下来就是调用此方法。

/**

* 生成二维码(内嵌LOGO)

*

* @param content

* 内容

* @param imgPath

* LOGO地址

* @param destPath

* 存放目录

* @param needCompress

* 是否压缩LOGO

* @throws Exception

*/

public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {

BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);

mkdirs(destPath);

String file = new Random().nextInt(99999999) + ".jpg";

ImageIO.write(image, FORMAT_NAME, new File(destPath + "/" + file));

}

/**

* 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)

*

* @author LongJin

* @date 2013-12-11 上午10:16:36

* @param destPath

* 存放目录

*/

public static void mkdirs(String destPath) {

File file = new File(destPath);

// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)

if (!file.exists() && !file.isDirectory()) {

file.mkdirs();

}

}

(4).如果不要logo只要在调用时将logo的路径传为null就好。

(5).至此一个二维码生成工具就完成了,当然有人可能需要解析二维码,所以这里就把二维码解析方法也写出,同时也方便我日后查看。

/**

* 解析二维码

*

* @param file

* 二维码图片

* @return

* @throws Exception

*/

public static String decode(File file) throws Exception {

BufferedImage image;

image = ImageIO.read(file);

if (image == null) {

return null;

}

BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);

BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

Result result;

Hashtable hints = new Hashtable();

hints.put(DecodeHintType.CHARACTER_SET, CHARSET);

result = new MultiFormatReader().decode(bitmap, hints);

String resultStr = result.getText();

return resultStr;

}

以上所述是给大家介绍的Java实现二维码功能的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java 回调机制(CallBack) 详解及实例代码
下一篇:Bootstrap面板学习使用
相关文章

 发表评论

暂时没有评论,来抢沙发吧~