java生产订单二维码
时间: 2023-11-24 12:50:22 浏览: 74
A级景区数据文件json
以下是Java生成订单二维码的示例代码:
```java
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;
public class QRCodeGenerator {
private static final String QR_CODE_IMAGE_PATH = "G:/测试/QRCode.png";
private static void generateQRCodeImage(String text, int width, int height, String filePath)
throws WriterException, IOException {
Map<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>();
hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hintMap);
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);
}
}
File qrFile = new File(filePath);
ImageIO.write(image, "png", qrFile);
}
public static void main(String[] args) {
try {
generateQRCodeImage("订单编号:1234567890", 350, 350, QR_CODE_IMAGE_PATH);
System.out.println("二维码生成成功!");
} catch (WriterException e) {
System.out.println("二维码生成失败,WriterException :: " + e.getMessage());
} catch (IOException e) {
System.out.println("二维码生成失败,IOException :: " + e.getMessage());
}
}
}
```
运行该代码将在指定路径下生成一个名为QRCode.png的二维码图片,其中包含了订单编号信息。
阅读全文