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;
}
相关jar包 core-2-3-0.jar