码迷,mamicode.com
首页 > 编程语言 > 详细

java生成二维码

时间:2017-06-30 13:51:02      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:coder   bsp   buffer   googl   str   get   bar   ble   white   

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生成二维码

标签:coder   bsp   buffer   googl   str   get   bar   ble   white   

原文地址:http://www.cnblogs.com/wanghongwei123/p/7098335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!