Java生成带logo的二维码

时间:2025-04-05 22:43:00
  • package ;
  • import ;
  • import ;
  • import .Graphics2D;
  • import ;
  • import ;
  • import .RoundRectangle2D;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import org.;
  • import org.;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import .;
  • import ;
  • import ;
  • import ;
  • /**
  • * 二维码工具类
  • * @date 2018年7月10日
  • * @version 1.0
  • */
  • public class QRCodeUtils {
  • private static Logger logger = ();
  • // 二维码大小
  • private static final int QRCODE_SIZE = 300;
  • // 二维码中间logo的尺寸
  • private static final int WIDTH = 60;
  • private static final int HEIGHT = 60;
  • public static void encode(String content, String logoPath, String imgPath) throws Exception {
  • encode(content, logoPath, imgPath, QRCODE_SIZE, 1);
  • }
  • public static void encode(String content, String logoPath, String imgPath, int size, int margin) throws Exception {
  • BufferedImage image = createImage(content, logoPath, size, margin);
  • (image, "png", new File(imgPath));
  • }
  • public static void encode(String content, String logoPath, OutputStream output) throws Exception {
  • encode(content, logoPath, output, QRCODE_SIZE, 1);
  • }
  • public static void encode(String content, String logoPath, OutputStream output, int size, int margin) throws Exception {
  • BufferedImage image = createImage(content, logoPath, size, margin);
  • (image, "png", output);
  • }
  • private static BufferedImage createImage(String content, String imgPath, int size, int margin) throws Exception {
  • Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
  • (EncodeHintType.ERROR_CORRECTION, );
  • (EncodeHintType.CHARACTER_SET, "UTF-8");
  • (, margin);
  • BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints);
  • int[] rec = ();
  • int resWidth = rec[2] + 1;
  • int resHeight = rec[3] + 1;
  • BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
  • ();
  • for (int i = 0; i < resWidth; i++) {
  • for (int j = 0; j < resHeight; j++) {
  • if ((i + rec[0], j + rec[1])) {
  • (i, j);
  • }
  • }
  • }
  • int width = ();
  • int height = ();
  • BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  • for (int x = 0; x < width; x++) {
  • for (int y = 0; y < height; y++) {
  • (x, y, (x, y) ? 0xFF000000 : 0xFFFFFFFF);
  • }
  • }
  • if (imgPath == null || "".equals(imgPath)) {
  • return image;
  • }
  • // 插入图片
  • insertImage(image, imgPath, size);
  • return image;
  • }
  • private static void insertImage(BufferedImage source, String imgPath, int size) throws Exception {
  • File file = new File(imgPath);
  • if (!()) {
  • (""+imgPath+" 该文件不存在!");
  • return;
  • }
  • Image src = (new File(imgPath));
  • int width = (null);
  • int height = (null);
  • // 压缩LOGO
  • if (width > WIDTH) {
  • width = WIDTH;
  • }
  • if (height > HEIGHT) {
  • height = HEIGHT;
  • }
  • Image image = (width, height, Image.SCALE_SMOOTH);
  • BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  • Graphics g = ();
  • (image, 0, 0, null); // 绘制缩小后的图
  • ();
  • src = image;
  • // 插入LOGO
  • Graphics2D graph = ();
  • int x = (size - width) / 2;
  • int y = (size - height) / 2;
  • (src, x, y, width, height, null);
  • Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
  • (new BasicStroke(3f));
  • (shape);
  • ();
  • }
  • public static String decode(File file) throws Exception {
  • BufferedImage image;
  • image = (file);
  • if (image == null) {
  • return null;
  • }
  • BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
  • BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
  • Result result;
  • Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
  • (DecodeHintType.CHARACTER_SET, "UTF-8");
  • result = new MultiFormatReader().decode(bitmap, hints);
  • return result != null ? () : null;
  • }
  • public static void main(String[] args) throws Exception {
  • // 生成带logo的二维码,不需要带logo不传参数即可
  • ("诺星战神-A001", "e:/", "e:/");
  • // 解析二维码存储的内容并打印
  • (decode(new File("e:/")));
  • }
  • }