java生成二维码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java生成二维码相关的知识,希望对你有一定的参考价值。
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 byte[] getBarcode(String code, Integer width, Integer height)
throws IOException
{
ImageControl ic = new ImageControl();
BufferedImage buf = ic.getBarcode(code, width, height);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(buf, "jpg", os);
byte[] b = os.toByteArray();
return b;
}
public BufferedImage getBarcode(String str, Integer width, Integer height)
{
BufferedImage image = null;
try
{
Hashtable<EncodeHintType, String> hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
int black = -16777216;
int white = -1;
BitMatrix bitMatrix = new MultiFormatWriter().encode(str, BarcodeFormat.CODE_128, width.intValue(), height.intValue(), hints);
image = new BufferedImage(width.intValue(), height.intValue(), 12);
for (int x = 0; x < width.intValue(); x++) {
for (int y = 0; y < height.intValue(); y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? black : white);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return image;
}
以上是关于java生成二维码的主要内容,如果未能解决你的问题,请参考以下文章