下面列出了怎么用com.google.zxing.MultiFormatWriter的API类实例代码及写法,或者点击链接到github查看源代码。
public static Bitmap transform(byte[] inputData) {
String encoded;
encoded = Base64.encodeToString(inputData, Base64.DEFAULT);
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Bitmap bitmap = null;
try {
BitMatrix bitMatrix = multiFormatWriter.encode(encoded, BarcodeFormat.QR_CODE, 1000, 1000);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
bitmap = barcodeEncoder.createBitmap(bitMatrix);
} catch (WriterException e) {
Timber.e(e);
}
return bitmap;
}
public Bitmap encodeAsBitmap() throws WriterException {
if (!encoded) return null;
Map<EncodeHintType, Object> hints = null;
String encoding = guessAppropriateEncoding(contents);
if (encoding != null) {
hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
// All are 0, or black, by default
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
public static Bitmap getQrCodeImage(String data, int width, int height) {
if (data == null || data.length() == 0) {
return null;
}
Map<EncodeHintType, Object> hintsMap = new HashMap<>(3);
hintsMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
hintsMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hintsMap.put(EncodeHintType.MARGIN, 0);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(data, BarcodeFormat.QR_CODE, width, height, hintsMap);
Bitmap bitmap = bitMatrix2Bitmap(bitMatrix);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/** 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定
*/
public static File getQRCodeImge(String contents, int width, int height, String imgPath) {
try {
Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
File imageFile = new File(imgPath);
writeToFile(bitMatrix, "png", imageFile);
return imageFile;
} catch (Exception e) {
log.error("create QR code error!", e);
return null;
}
}
/**
* 生成二维码并响应到浏览器
*
* @param content
* @param response
*/
public static void createQRCode(String content, HttpServletResponse response) {
int width = 300, height = 300;
String format = "png";
Map<EncodeHintType, Object> hashMap = new HashMap<>();
hashMap.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8);
hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hashMap.put(EncodeHintType.MARGIN, 1);
try {
response.setHeader("Cache-control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setHeader("content-type", "image/png");
response.setCharacterEncoding(StandardCharsets.UTF_8.displayName());
response.setDateHeader("Expires", 0);
BitMatrix bitMatrix = new MultiFormatWriter()
.encode(content, BarcodeFormat.QR_CODE, width, height, hashMap);
BufferedImage img = MatrixToImageWriter.toBufferedImage(bitMatrix);
ImageIO.write(img, format, response.getOutputStream());
} catch (Exception e) {
log.warn("create QRCode error message:{}", e.getMessage());
}
}
private Bitmap createQRImage(String address) {
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
int imageSize = (int) (size.x * QR_IMAGE_WIDTH_RATIO);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(
address,
BarcodeFormat.QR_CODE,
imageSize,
imageSize,
null);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
return barcodeEncoder.createBitmap(bitMatrix);
} catch (Exception e) {
Toast.makeText(this, getString(R.string.error_fail_generate_qr), Toast.LENGTH_SHORT)
.show();
}
return null;
}
/**
* 同步创建指定前景色、指定背景色二维码图片
* <pre>
* 该方法是耗时操作, 请在子线程中调用
* </pre>
* @param content 生成内容
* @param size 图片宽高大小 ( 正方形 px )
* @param foregroundColor 二维码图片的前景色
* @param backgroundColor 二维码图片的背景色
* @return 二维码图片
*/
public static Bitmap syncEncodeQRCode(final String content, final int size, final int foregroundColor, final int backgroundColor) {
try {
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, ENCODE_HINTS);
int[] pixels = new int[size * size];
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (matrix.get(x, y)) {
pixels[y * size + x] = foregroundColor;
} else {
pixels[y * size + x] = backgroundColor;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "syncEncodeQRCode");
return null;
}
}
public static Bitmap textToBarCode(String data) {
MultiFormatWriter writer = new MultiFormatWriter();
String finaldata = Uri.encode(data, "utf-8");
BitMatrix bm = null;
try {
bm = writer.encode(finaldata, BarcodeFormat.CODE_128, 150, 150);
} catch (WriterException e) {
e.printStackTrace();
}
Bitmap bitmap = Bitmap.createBitmap(180, 40, Bitmap.Config.ARGB_8888);
for (int i = 0; i < 180; i++) {//width
for (int j = 0; j < 40; j++) {//height
bitmap.setPixel(i, j, bm.get(i, j) ? BLACK : WHITE);
}
}
return bitmap;
}
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
public static BufferedImage DataMatrix(String code,
int dmWidth, int dmHeigh) {
try {
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(code,
BarcodeFormat.DATA_MATRIX, dmWidth, dmHeigh, hints);
return MatrixToImageWriter.toBufferedImage(bitMatrix);
} catch (Exception e) {
logger.error(e.toString());
return null;
}
}
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* 根据url生成二位图片对象
*
* @param codeUrl
* @return
* @throws WriterException
*/
public static BufferedImage getQRCodeImge(String codeUrl) throws WriterException {
Map<EncodeHintType, Object> hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
int width = 256;
BitMatrix bitMatrix = (new MultiFormatWriter()).encode(codeUrl, BarcodeFormat.QR_CODE, width, width, hints);
BufferedImage image = new BufferedImage(width, width, 1);
for(int x = 0; x < width; ++x) {
for(int y = 0; y < width; ++y) {
image.setRGB(x, y, bitMatrix.get(x, y) ? -16777216 : -1);
}
}
return image;
}
private Bitmap createQRImage(String address) {
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
int imageSize = (int) (size.x * QR_IMAGE_WIDTH_RATIO);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(
address,
BarcodeFormat.QR_CODE,
imageSize,
imageSize,
null);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
return barcodeEncoder.createBitmap(bitMatrix);
} catch (Exception e) {
Toast.makeText(this, getString(R.string.error_fail_generate_qr), Toast.LENGTH_SHORT)
.show();
}
return null;
}
public static Bitmap createQRImage(Context context, String address, int imageSize) {
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(
address,
BarcodeFormat.QR_CODE,
imageSize,
imageSize,
null);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
return barcodeEncoder.createBitmap(bitMatrix);
} catch (Exception e) {
Toast.makeText(context, context.getString(R.string.error_fail_generate_qr), Toast.LENGTH_SHORT)
.show();
}
return null;
}
private void generateQR(Setup setup) throws Exception {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
int data_length = 0;
try {
JSONObject obj = Settings.toJsonObject(setup);
String data = encodeSetup(obj);
data_length = data.length();
// data has to be a string
BitMatrix bitMatrix = multiFormatWriter.encode(data, BarcodeFormat.QR_CODE, 1080, 1080);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
((ImageView) findViewById(R.id.QRView)).setImageBitmap(bitmap);
} catch (WriterException e) {
Toast.makeText(this, e.getMessage() + " (" + data_length + " Bytes)", Toast.LENGTH_LONG).show();
finish();
}
}
private BufferedImage createImage(String content, InputStream logo, boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, imageSize, imageSize,
hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
if (logo == null) {
return image;
}
// 插入logo
this.insertLogo(image, logo, needCompress);
return image;
}
private Bitmap createQRImage(String address) {
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
int imageSize = (int) (size.x * QR_IMAGE_WIDTH_RATIO);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(
address,
BarcodeFormat.QR_CODE,
imageSize,
imageSize,
null);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
return barcodeEncoder.createBitmap(bitMatrix);
} catch (Exception e) {
Toast.makeText(this, getString(R.string.error_fail_generate_qr), Toast.LENGTH_SHORT)
.show();
}
return null;
}
/**
* @param content
* @param format
* @param height
* @return
*/
public static BitMatrix createBarCode(String content, BarcodeFormat format, int height) {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN, 0);
// 条形码宽度为自适应
int width = format == BarcodeFormat.QR_CODE ? height : 0;
try {
return new MultiFormatWriter().encode(content, format, width, height, hints);
} catch (WriterException ex) {
throw new RebuildException("Encode BarCode failed : " + content, ex);
}
}
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
*二维码实现
* @param msg /二维码包含的信息
* @param path /二维码存放路径
*/
public static void getBarCode(String msg,String path){
try {
File file=new File(path);
OutputStream ous=new FileOutputStream(file);
if(StringUtils.isEmpty(msg) || ous==null)
return;
String format = "png";
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map<EncodeHintType,String> map =new HashMap<EncodeHintType, String>();
//设置编码 EncodeHintType类中可以设置MAX_SIZE, ERROR_CORRECTION,CHARACTER_SET,DATA_MATRIX_SHAPE,AZTEC_LAYERS等参数
map.put(EncodeHintType.CHARACTER_SET,"UTF-8");
map.put(EncodeHintType.MARGIN,"1");
//生成二维码
BitMatrix bitMatrix = new MultiFormatWriter().encode(msg, BarcodeFormat.QR_CODE,300,300,map);
MatrixToImageWriter.writeToStream(bitMatrix,format,ous);
}catch (Exception e) {
e.printStackTrace();
}
}
/**
*二维码实现
* @param msg /二维码包含的信息
*/
public static byte[] getBarCodeWo(String msg){
byte[] bs = null;
try {
String format = "png";
Map<EncodeHintType,String> map =new HashMap<EncodeHintType, String>();
//设置编码 EncodeHintType类中可以设置MAX_SIZE, ERROR_CORRECTION,CHARACTER_SET,DATA_MATRIX_SHAPE,AZTEC_LAYERS等参数
map.put(EncodeHintType.CHARACTER_SET,"UTF-8");
map.put(EncodeHintType.MARGIN,"1");
//生成二维码
BitMatrix bitMatrix = new MultiFormatWriter().encode(msg, BarcodeFormat.QR_CODE,300,300,map);
BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "gif", out);
bs = out.toByteArray();
}catch (Exception e) {
e.printStackTrace();
}
return bs;
}
Bitmap encodeAsBitmap(String str, int size) throws WriterException {
BitMatrix result;
try {
result = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, size, size);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? 0xFF000000 : 0xFFFFFFFF;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* This method creates a picture of the scanned qr code
*/
private void showQrImage() {
multiFormatWriter = new MultiFormatWriter();
Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hintMap.put(EncodeHintType.MARGIN, 1); /* default = 4 */
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
try{
BarcodeFormat format = generalHandler.StringToBarcodeFormat(selectedFormat);
BitMatrix bitMatrix = multiFormatWriter.encode(selectedCode, format, 250,250, hintMap);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
bitmap = barcodeEncoder.createBitmap(bitMatrix);
codeImage.setImageBitmap(bitmap);
} catch (Exception e){
codeImage.setVisibility(View.GONE);
}
}
/**
* This method creates a picture of the scanned qr code
*/
private void showQrImage() {
multiFormatWriter = new MultiFormatWriter();
Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hintMap.put(EncodeHintType.MARGIN, 1); /* default = 4 */
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
try{
BarcodeFormat format = generalHandler.StringToBarcodeFormat(qrcodeFormat);
BitMatrix bitMatrix = multiFormatWriter.encode(qrcode, format, 250,250, hintMap);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
bitmap = barcodeEncoder.createBitmap(bitMatrix);
codeImage.setImageBitmap(bitmap);
} catch (Exception e){
codeImage.setVisibility(View.GONE);
}
}
public Bitmap encodeAsBitmap() throws WriterException {
if (!encoded) return null;
Map<EncodeHintType, Object> hints = null;
String encoding = guessAppropriateEncoding(contents);
if (encoding != null) {
hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
// All are 0, or black, by default
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定
*/
public static File getQRCodeImge(String contents, int width, int height, String imgPath) {
try {
Map<EncodeHintType, Object> hints = Maps.newHashMap();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
File imageFile = new File(imgPath);
writeToFile(bitMatrix, "png", imageFile);
return imageFile;
} catch (Exception e) {
log.error("create QR code error!", e);
return null;
}
}
/**
* 将内容contents生成长为width,宽为width的图片,返回刘文静
*/
public static ServletOutputStream getQRCodeImge(String contents, int width, int height) throws IOException {
ServletOutputStream stream = null;
try {
Map<EncodeHintType, Object> hints = Maps.newHashMap();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToStream(bitMatrix, "png", stream);
return stream;
} catch (Exception e) {
log.error("create QR code error!", e);
return null;
} finally {
if (stream != null) {
stream.close();
}
}
}