JAVA生成二维码图片和解析二维码图片内容代码实例

}

/**

  • 解析指定路径下的二维码图片

  • @param filePath

  • @return

*/

private static String parseQRCode(String filePath) {

String content = “”;

try {

File file = new File(filePath);

BufferedImage image = ImageIO.read(file);

LuminanceSource source = new BufferedImageLuminanceSource(image);

Binarizer binarizer = new HybridBinarizer(source);

BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);

Map<DecodeHintType, Object> hints = new HashMap<>();

hints.put(DecodeHintType.CHARACTER_SET, “UTF-8”);

MultiFormatReader formatReader = new MultiFormatReader();

Result result = formatReader.decode(binaryBitmap, hints);

//设置返回值

content = result.getText();

} catch (Exception e) {

e.printStackTrace();

}

return content;

}

/**

  • 根据内容,生成指定宽高、指定格式的二维码图片

  • @param text 内容

  • @param width 宽

  • @param height 高

  • @param format 图片格式

  • @return 生成的二维码图片路径

  • @throws Exception

*/

public static String generateQRCode(String text, int width, int height, String format, String pathName) throws Exception {

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

hints.put(EncodeHintType.CHARACTER_SET, “utf-8”);

BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);

File outputFile = new File(pathName);

if (!outputFile.exists()) {

outputFile.mkdirs();

}

writeToFile(bitMatrix, format, outputFile);

return pathName;

}

//输出为文件

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(“文件写入异常”);

}

}

//输出为流

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(“文件写入异常”);

}

}

//缓冲图片

public static BufferedImage toBufferedImage(BitMatrix matrix) {

int width = matrix.getWidth();

int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);

}

}

return image;

}

}

控制台输出



生成二维码图片效果

最后

由于细节内容实在太多了,为了不影响文章的观赏性,只截出了一部分知识点大致的介绍一下,每个小节点里面都有更细化的内容!

小编准备了一份Java进阶学习路线图(Xmind)以及来年金三银四必备的一份《Java面试必备指南》

,为了不影响文章的观赏性,只截出了一部分知识点大致的介绍一下,每个小节点里面都有更细化的内容!

[外链图片转存中…(img-yfKGpqZF-1714135394015)]

小编准备了一份Java进阶学习路线图(Xmind)以及来年金三银四必备的一份《Java面试必备指南》

[外链图片转存中…(img-PIFyicIV-1714135394015)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 生成二维码图片代码需要依赖一个第三方的库,比较常用的是`ZXing`。 使用`ZXing`生成二维码代码示例如下: ``` import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class QRCodeGenerator { private static final int width = 300; private static final int height = 300; private static final String format = "png"; private static final Map<EncodeHintType, Object> hints = new HashMap<>(); static { hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.MARGIN, 1); } public static void main(String[] args) throws WriterException, IOException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); String text = "http://www.baidu.com"; // 二维码内容 BitMatrix bitMatrix = qrCodeWriter.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, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB()); } } // 保存图片 ImageIO.write(image, format, new File("qrcode.png")); } } ``` 在上面的代码中,我们先定义了二维码的宽度、高度和格式,然后创建了一个`QRCodeWriter`对象,并使用它的`encode`方 ### 回答2: 生成二维码图片代码可以使用Java开发的Zxing库来实现。下面是使用Zxing库生成二维码图片的示例代码: ```java import java.io.File; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class QRCodeGenerator { public static void main(String[] args) { String text = "https://www.example.com"; // 二维码内容 int width = 250; // 二维码图片宽度 int height = 250; // 二维码图片高度 String format = "png"; // 二维码图片格式 // 设置二维码的一些参数 com.google.zxing.Writer writer = new QRCodeWriter(); BitMatrix matrix = null; try { // 设置二维码的一些参数 java.util.Hashtable<EncodeHintType, Object> hints = new java.util.Hashtable<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.MARGIN, 2); // 生成二维码图片矩阵 matrix = writer.encode(text, BarcodeFormat.QR_CODE, width, height, hints); } catch (WriterException e) { e.printStackTrace(); } // 保存二维码图片 Path path = FileSystems.getDefault().getPath("qrcode." + format); File outputFile = path.toFile(); try { MatrixToImageWriter.writeToFile(matrix, format, outputFile); System.out.println("二维码图片已生成。"); } catch (IOException e) { e.printStackTrace(); } } } ``` 通过以上代码,可以生成一个包含指定内容二维码图片,并保存到磁盘中。你可以自定义二维码内容、大小和保存格式等参数。 ### 回答3: 生成二维码图片代码主要使用了ZXing库。ZXing是一个开源的条形码和二维码解码/编码库,提供了生成二维码的功能。 首先,需要在Java项目中导入ZXing的jar包。可以通过Maven等工具添加依赖。 接下来,可以使用以下代码生成二维码图片: ```java import java.io.File; import java.io.IOException; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; public class QRCodeGenerator { public static void generateQRCode(String text, int width, int height, String filePath) throws WriterException, IOException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); File imageFile = new File(filePath); ImageIO.write(createImage(bitMatrix), "png", imageFile); } private static BufferedImage createImage(BitMatrix bitMatrix) { 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); } } return image; } public static void main(String[] args) { String text = "https://example.com"; int width = 200; int height = 200; String filePath = "qrcode.png"; try { generateQRCode(text, width, height, filePath); System.out.println("二维码图片生成成功!"); } catch (WriterException | IOException e) { System.out.println("二维码图片生成失败:" + e.getMessage()); } } } ``` 在上述代码中,我们定义了一个`generateQRCode`方法,该方法接受三个参数:文本内容二维码图片的宽度和高度,以及图片保存的路径。该方法内部首先使用`QRCodeWriter`生成二维码的比特矩阵(`BitMatrix`),然后使用`createImage`方法将比特矩阵转换为`BufferedImage`,最后将图片保存到指定的文件路径。 在`createImage`方法中,我们遍历了比特矩阵的每个像素点,并根据像素点的值设置对应位置的RGB颜色。 在`main`方法中,我们定义了一个示例来生成一个包含网址`https://example.com`的二维码图片图片的宽度和高度都是200,保存为`qrcode.png`。 运行以上代码,会在项目根目录下生成一个名为`qrcode.png`的二维码图片

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值