【JAVA】URL转二维码以及图片合成

 

最近项目中有一个需求,要将一个URL链接转成二维码,并合成到一个固定的背景图片上的指定位置。其实将二维码合成到图片上还是将图片合成到二维码上,都是同一个道理。

需要采用google提供的 jar 包来将URL转化成二维码图片。

Maven项目直接引用以下依赖:

<dependency>
	<groupId>com.google.zxing</groupId>
	<artifactId>core</artifactId>
	<version>3.4.1</version>
</dependency>

以下工具方法复制即可使用。

将URL转化成二维码图片:

    /**
	 * 二维码图片的生成
	 * @param content			链接
	 * @param qrcode_width		二维码宽
	 * @param qrcode_height		二维码高
	 * @return
	 * @throws Exception
	 */
    public static BufferedImage createImage(String content, int qrcode_width, int qrcode_height) throws Exception {
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
                BarcodeFormat.QR_CODE, qrcode_width, qrcode_height, 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);
            }
        }
        return image;
    }

将链接二维码放入背景图指定位置:

    /**
	 * 合成图片,将链接转为二维码并放入背景图指定位置
	 * @param url		二维码链接
	 * @param path		背景图片地址
	 * @param startX	二维码在背景图片的X轴位置
	 * @param startY	二维码在背景图片的Y轴位置
	 * @param codeWidth	二维码宽度
	 * @param codeHeight 二维码高度
	 * @return			合成的图片
	 */
	public static BufferedImage compositeImage(String url, String path, int startX, int startY, int codeWidth, int codeHeight) {
		try {
			BufferedImage headImage = createImage(url, codeWidth, codeHeight);
			
			FileInputStream fileInputStream = new FileInputStream(path);
			String backBIS64 = ImageUtil.GetImageStr(fileInputStream);
			// 读取背景图片
			InputStream in = new ByteArrayInputStream(ImageUtil.GenerateImage(backBIS64));
			Image backImage = ImageIO.read(in);
			int alphaType = BufferedImage.TYPE_INT_RGB;
			if (hasAlpha(backImage)) {
				alphaType = BufferedImage.TYPE_INT_ARGB;
			}
			BufferedImage back = new BufferedImage(backImage.getWidth(null), backImage.getHeight(null), alphaType);

			// 画图
			Graphics2D g = back.createGraphics();
			g.drawImage(backImage, 0, 0, null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
			g.drawImage(headImage, startX, backImage.getHeight(null) - startY, headImage.getWidth(null), headImage.getHeight(null), null);

			g.dispose();

			return back;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	/**
	 * 是否开启alpha通道
	 * 
	 * @param image
	 * @return
	 */
	public static boolean hasAlpha(Image image) {
		if (image instanceof BufferedImage) {
			BufferedImage bimage = (BufferedImage) image;
			return bimage.getColorModel().hasAlpha();
		}
		PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
		try {
			pg.grabPixels();
		} catch (InterruptedException e) {
		}
		ColorModel cm = pg.getColorModel();
		return cm.hasAlpha();
	}

将Logo图片合成到链接二维码正中:

    /**
	 * 	合成图片,将LOGO放入链接二维码正中间
	 * @param url			连接地址
	 * @param path			LOGO存储路径
	 * @param codeWidth		二维码宽度
	 * @param codeHeight	二维码高度
	 * @param logoPer	LOGO占二维码图片的百分比
	 * @return
	 */
	public static BufferedImage compositeLogoImage(String url, String logoPath, int codeWidth, int codeHeight, int logoWidth, int logoHeight) {
		try {
			//	创建链接二维码
			BufferedImage urlImage = createImage(url, codeWidth, codeHeight);
			
			//	读取LOGO
			FileInputStream fileInputStream = new FileInputStream(logoPath);
			Image logoImage = ImageIO.read(fileInputStream);
			
			//	加载背景图片
			int alphaType = BufferedImage.TYPE_INT_RGB;
			if (ImageUtil.hasAlpha(urlImage)) {
				alphaType = BufferedImage.TYPE_INT_ARGB;
			}
			BufferedImage back = new BufferedImage(urlImage.getWidth(), urlImage.getHeight(), alphaType);
 
			//	画图
			Graphics2D g = back.createGraphics();
			g.drawImage(urlImage, 0, 0, null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
			g.drawImage(logoImage, getLogoPosition(urlImage.getWidth(), logoWidth), getLogoPosition(urlImage.getHeight(), logoHeight), 
					logoWidth, logoHeight, null);
			g.dispose();
 
			return back;
 
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private static int getLogoPosition(int backWidth, double logoWidth) {
		Double d = (backWidth - logoWidth)/2;
		return d.intValue();
	}

 

附:BufferedImage 的使用

//通过ImageIO提供的方法可以操作BufferedImage类的写入写出
ImageIO.write(BufferedImage bufferImage, String formatName, OutputStream output);
BufferedImage bufferImage = ImageIO.read(InputStream input);


//输出到前端
ImageIO.write(bufferImage, "png", response.getOutputStream());

 

 

 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
可以使用第三方库Zxing来生成二维码图片,具体实现可以参考以下Java代码: ``` import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class QrCodeGenerator { public static void main(String[] args) throws WriterException, IOException { String url = "https://www.example.com"; int width = 300; int height = 300; String format = "png"; File qrFile = new File("qrcode.png"); Map<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); createQRCode(url, width, height, format, hintMap, qrFile); } private static void createQRCode(String url, int width, int height, String format, Map hintMap, File qrFile) throws WriterException, IOException { BitMatrix matrix = new MultiFormatWriter().encode(new String(url.getBytes("UTF-8"), "ISO-8859-1"), BarcodeFormat.QR_CODE, width, height, hintMap); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.createGraphics(); java.awt.Graphics2D graphics = (java.awt.Graphics2D) image.getGraphics(); graphics.setColor(java.awt.Color.WHITE); graphics.fillRect(0, 0, width, height); graphics.setColor(java.awt.Color.BLACK); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (matrix.get(i, j)) { graphics.fillRect(i, j, 1, 1); } } } ImageIO.write(image, format, qrFile); } } ``` 该代码可以生成一个指定URL二维码图片,保存在本地文件qrcode.png中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值