java zxing生成二维码
package zxing.test; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.codec.binary.Base64; /** * @作者 yan * @创建日期 * @版本 V1.0 * @描述 */ public class QrCodeUtil { /** * 生成二维码,返回二维码Base64编码 * @param content * @param size * @param imgFormt * @return */ public static String createQrCodeBase64(String content, int size, String imgFormt){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, size, size, hints); MatrixToImageWriter.writeToStream(bitMatrix, imgFormt, baos); } catch (WriterException ex) { Logger.getLogger(QrCodeUtil.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(QrCodeUtil.class.getName()).log(Level.SEVERE, null, ex); } byte result [] = baos.toByteArray(); return Base64.encodeBase64String(result); } /** * 生成二维码,保存到output * @param output * @param content * @param size * @param imgFormt * @return */ public static boolean createQrCode(OutputStream output, String content, int size, String imgFormt){ try { Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, size, size, hints); MatrixToImageWriter.writeToStream(bitMatrix, imgFormt, output); return true; } catch (WriterException ex) { Logger.getLogger(QrCodeUtil.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(QrCodeUtil.class.getName()).log(Level.SEVERE, null, ex); } return false; } }
jar:
zxing-core-3.2.0.jar
zxing-javase-3.2.0.jar