package io.renren.common.utils; import cn.hutool.core.lang.Console; import io.renren.modules.oss.cloud.OSSFactory; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import java.awt.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import java.io.*; import java.net.URL; public class ImageUtil { static final String IMAGE_SUFFIX = ".png"; static final int HEAD_URL_WIDTH = 100; static final int HEAD_URL_HEIGHT = 100; /** * @param posterImgUrl 海報 * @param tempQrCodeImgUrl 臨時二維碼 * @param headImgUrl 頭像 * @return 合成圖片地址 */ public static String drawImage(String posterImgUrl, String tempQrCodeImgUrl, String headImgUrl) throws IOException { BufferedImage posterBufImage = ImageIO.read(new URL(posterImgUrl)); Graphics2D posterBufImageGraphics = posterBufImage.createGraphics(); BufferedImage qrCodeImage = ImageIO.read(new URL(tempQrCodeImgUrl)); BufferedImage headImage = ImageIO.read(new URL(headImgUrl)); //設置圓形頭像 BufferedImage roundHeadImg = new BufferedImage(headImage.getWidth(), headImage.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D roundHeadGraphics = roundHeadImg.createGraphics(); Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, roundHeadImg.getWidth(), roundHeadImg.getHeight()); roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); roundHeadImg = roundHeadGraphics.getDeviceConfiguration().createCompatibleImage(headImage.getWidth(), headImage.getHeight(), Transparency.TRANSLUCENT); roundHeadGraphics = roundHeadImg.createGraphics(); // 使用 setRenderingHint 設置抗鋸齒 roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); roundHeadGraphics.setClip(shape); roundHeadGraphics.drawImage(headImage, 0, 0, null); roundHeadGraphics.dispose(); posterBufImageGraphics.drawImage(qrCodeImage, (posterBufImage.getWidth() - qrCodeImage.getWidth()), 10, qrCodeImage.getWidth(), qrCodeImage.getHeight(), null); posterBufImageGraphics.drawImage(roundHeadImg, 50, 100, HEAD_URL_WIDTH, HEAD_URL_HEIGHT, null); posterBufImageGraphics.dispose(); ByteArrayOutputStream bs = new ByteArrayOutputStream(); ImageOutputStream imgOut = ImageIO.createImageOutputStream(bs); ImageIO.write(posterBufImage, "png", imgOut); InputStream inSteam = new ByteArrayInputStream(bs.toByteArray()); String url = OSSFactory.build().uploadSuffix(inSteam, IMAGE_SUFFIX); return url;//返回合成的圖片地址url } public static void drawImage() throws IOException { //海報圖 String backgroundImageUrl = "http://img1.juimg.com/171010/330841-1G01000050879.jpg"; //二維碼 String qrCodeImageUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553504957130&di=02fae20a5c0f885d52299b2b1d682c86&imgtype=0&src=http%3A%2F%2Fimg.atobo.com%2FProductImg%2FEWM%2FUWeb%2F3%2F2%2F1%2F3%2F061%2F3213061%2F1.gif"; //頭像 String headUrl = "http://pic.51yuansu.com/pic3/cover/00/63/25/589bdedf5475d_610.jpg"; BufferedImage bgBufImage = ImageIO.read(new URL(backgroundImageUrl)); Graphics2D bgBufImageGraphics = bgBufImage.createGraphics(); BufferedImage qrCodeImage = ImageIO.read(new URL(qrCodeImageUrl)); BufferedImage headImage = ImageIO.read(new URL(headUrl)); //設置圓形圖片 BufferedImage roundHeadImg = new BufferedImage(headImage.getWidth(), headImage.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D roundHeadGraphics = roundHeadImg.createGraphics(); Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, roundHeadImg.getWidth(), roundHeadImg.getHeight()); roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); roundHeadImg = roundHeadGraphics.getDeviceConfiguration().createCompatibleImage(headImage.getWidth(), headImage.getHeight(), Transparency.TRANSLUCENT); roundHeadGraphics = roundHeadImg.createGraphics(); // 使用 setRenderingHint 設置抗鋸齒 roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); roundHeadGraphics.setClip(shape); roundHeadGraphics.drawImage(headImage, 0, 0, null); roundHeadGraphics.dispose(); bgBufImageGraphics.drawImage(qrCodeImage, (bgBufImage.getWidth() - qrCodeImage.getWidth()), 10, qrCodeImage.getWidth(), qrCodeImage.getHeight(), null); bgBufImageGraphics.drawImage(roundHeadImg, 50, 100, HEAD_URL_WIDTH, HEAD_URL_HEIGHT, null); bgBufImageGraphics.dispose(); ImageIO.write(bgBufImage, "png", new File("E:\\demo1.png")); Console.log("生成圖片完成"); } public static void main(String[] args) throws IOException { //海報圖 String backgroundImageUrl = "http://img1.juimg.com/171010/330841-1G01000050879.jpg"; //二維碼 String qrCodeImageUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553504957130&di=02fae20a5c0f885d52299b2b1d682c86&imgtype=0&src=http%3A%2F%2Fimg.atobo.com%2FProductImg%2FEWM%2FUWeb%2F3%2F2%2F1%2F3%2F061%2F3213061%2F1.gif"; //頭像 String headUrl = "http://pic.51yuansu.com/pic3/cover/00/63/25/589bdedf5475d_610.jpg"; String url = ImageUtil.drawImage(backgroundImageUrl, qrCodeImageUrl, headUrl); Console.log("url={}",url); } }