java 生成二维码

用google的zxing

<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>



import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class BarCodeUtil {

private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private static int IMG_WIDTH = 300;
private static int IMG_HEIGHT = 300;

public static void main(String[] args) {
System.out.println(genBarCodeToBase64("二维码文字内容", "E:/barcode-logo.png"));
genBarCodeToFile("二维码文字内容", "E:/barcode-logo.png");
}

/**
* 生成二维码图片,不带logo,返回图片的base64编码
* @param text
* @return
*/
public static String genBarCodeToBase64(String text) {
return genBarCodeToBase64(text, null);
}

/**
* 生成二维码图片,带logo,返回图片的base64编码
* @param text
* @return
*/
public static String genBarCodeToBase64(String text, String logopath) {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//容错率设置为高,手机像素差点的也能扫出来
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, IMG_WIDTH, IMG_HEIGHT,
hints);
byte[] arr = toByteArray(bitMatrix, logopath);
return Base64.encodeBase64String(arr);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 生成为本地文件,带logo
*/
public static void genBarCodeToFile(String text, String logoPath) {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix bitMatrix;
try {
bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, IMG_WIDTH, IMG_HEIGHT, hints);
writeToFile(bitMatrix, "png", new File("E:/new.png"), logoPath);
} catch (Exception e) {
e.printStackTrace();
}
}

private static byte[] toByteArray(BitMatrix matrix, String logopath) throws IOException {
BufferedImage image = toBufferedImage(matrix);

//增加logo
if (StringUtils.isNotBlank(logopath)) {
Graphics2D gs = image.createGraphics();
BufferedImage img = ImageIO.read(new File(logopath));
gs.drawImage(img, IMG_WIDTH / 2 - img.getWidth() / 2, IMG_WIDTH / 2 - img.getHeight() / 2, null);
gs.dispose();
img.flush();
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(image, "png", out);
return out.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

private static void writeToFile(BitMatrix matrix, String format, File file, String logoPath) throws IOException {
BufferedImage image = toBufferedImage(matrix);
//增加logo
if (StringUtils.isNotBlank(logoPath)) {
Graphics2D gs = image.createGraphics();
BufferedImage img = ImageIO.read(new File(logoPath));
gs.drawImage(img, IMG_WIDTH / 2 - img.getWidth() / 2, IMG_WIDTH / 2 - img.getHeight() / 2, null);
gs.dispose();
img.flush();
}
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}

private 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;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值