在如今的生活中,二维码随处可见,二维码的出现既减少了宣传纸张的浪费,又方便了人们的生活。这一篇我来说说 Java 利用第三方 Jar 包 zxing 生成二维码。
依赖
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.3</version> </dependency>
生成二维码
package code;
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Base64OutputStream;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class QRCodeKit {
public static final String QRCODE_DEFAULT_CHARSET = "UTF-8";
public static final int QRCODE_DEFAULT_HEIGHT = 300;
public static final int QRCODE_DEFAULT_WIDTH = 300;
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
public static void main(String[] args) throws IOException, NotFoundException {
String data = "https://www.jianshu.com/p/748aa03cc1e8?ddd=dsdsdsdsddsdsdsdsdsdsdsdsd";
File logoFile = new File("C:/1.png");
BufferedImage image = QRCodeKit.createQRCodeWithLogo(data, logoFile);
ImageIO.write(image, "png", new File("D:/result7.png"));
System.out.println("done");
}
/**
* Create qrcode with default settings
*
* @param data
* @return
* @author stefli
*/
public static BufferedImage createQRCode(String data) {
return createQRCode(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT);
}
/**
* Create qrcode with default charset
*
* @param data
* @param width
* @param height
* @return
* @author stefli
*/
public static BufferedImage createQRCode(String data, int width, int height) {
return createQRCode(data, QRCODE_DEFAULT_CHARSET, width, height);
}
/**
* Create qrcode with specified charset
*
* @param data
* @param charset
* @param width
* @param height
* @return
* @author stefli
*/
public static BufferedImage createQRCode(String data, String charset, int width, int height) {
Map hint = new HashMap();
hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hint.put(EncodeHintType.CHARACTER_SET, charset);
return createQRCode(data, charset, hint, width, height);
}
/**
* Create qrcode with specified hint
*
* @param data
* @param charset
* @param hint
* @param width
* @param height
* @return
* @author stefli
*/
public static BufferedImage createQRCode(String data, String charset, Map<EncodeHintType, ?> hint, int width,
int height) {
BitMatrix matrix;
try {
matrix = new MultiFormatWriter().encode(new String(data.getBytes(charset), charset), BarcodeFormat.QR_CODE,
width, height, hint);
return toBufferedImage(matrix);
} catch (WriterException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* toBufferedImage
*
* @param matrix
* @return
*/
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
/**
* Create qrcode with default settings and logo
*
* @param data
* @param logoFile
* @return
* @author stefli
*/
public static BufferedImage createQRCodeWithLogo(String data, File logoFile) {
return createQRCodeWithLogo(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT, logoFile);
}
/**
* Create qrcode with default charset and logo
*
* @param data
* @param width
* @param height
* @param logoFile
* @return
* @author stefli
*/
public static BufferedImage createQRCodeWithLogo(String data, int width, int height, File logoFile) {
return createQRCodeWithLogo(data, QRCODE_DEFAULT_CHARSET, width, height, logoFile);
}
/**
* Create qrcode with specified charset and logo
*
* @param data
* @param charset
* @param width
* @param height
* @param logoFile
* @return
* @author stefli
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static BufferedImage createQRCodeWithLogo(String data, String charset, int width, int height, File logoFile) {
Map hint = new HashMap();
hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hint.put(EncodeHintType.CHARACTER_SET, charset);
hint.put(EncodeHintType.MARGIN, 2);
return createQRCodeWithLogo(data, charset, hint, width, height, logoFile);
}
/**
* Create qrcode with specified hint and logo
*
* @param data
* @param charset
* @param hint
* @param width
* @param height
* @param logoFile
* @return
* @author stefli
*/
public static BufferedImage createQRCodeWithLogo(String data, String charset, Map<EncodeHintType, ?> hint,
int width, int height, File logoFile) {
try {
BufferedImage qrcode = createQRCode(data, charset, hint, width, height);
BufferedImage logo = ImageIO.read(logoFile);
BufferedImage combined = new BufferedImage(height, width, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) combined.getGraphics();
//设置二维码大小,太大,会覆盖二维码,此处20%
int logoWidth = logo.getWidth() > qrcode.getWidth() * 2 / 10 ? (qrcode.getWidth() * 2 / 10) : logo.getWidth();
int logoHeight = logo.getHeight() > qrcode.getHeight() * 2 / 10 ? (qrcode.getHeight() * 2 / 10) : logo.getHeight();
//设置logo图片放置位置--中心
int x = Math.round((qrcode.getWidth() - logoWidth) / 2);
int y = Math.round((qrcode.getHeight() - logoHeight) / 2);
g.drawImage(qrcode, 0, 0, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
//开始合并绘制图片
g.drawImage(logo, x, y, logoWidth, logoHeight, null);
g.drawRoundRect(x, y, logoWidth, logoHeight, 15, 15);
//logo边框大小
g.setStroke(new BasicStroke(3));
//logo边框颜色
g.setColor(Color.white);
g.drawRoundRect(x, y, logoWidth, logoHeight, 15, 15);
g.dispose();
logo.flush();
qrcode.flush();
return combined;
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* Return base64 for image
*
* @param image
* @return
* @author stefli
*/
public static String getImageBase64String(BufferedImage image) {
String result = null;
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
OutputStream b64 = new Base64OutputStream(os);
ImageIO.write(image, "png", b64);
result = os.toString("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
return result;
}
/**
* Decode the base64Image data to image
*
* @param base64ImageString
* @param file
* @author stefli
*/
public static void convertBase64StringToImage(String base64ImageString, File file) {
FileOutputStream os;
try {
Base64 d = new Base64();
byte[] bs = d.decode(base64ImageString);
os = new FileOutputStream(file.getAbsolutePath());
os.write(bs);
os.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}










