java合成海报的工具类

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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);}}

 

转载于:https://my.oschina.net/u/2426590/blog/3027726

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/275508.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

a说b说谎b说c说谎说d说_说谎的眼睛及其同伙

a说b说谎b说c说谎说d说The eye is a complex and temperamental organ. By the end of this article, designers will have a better understanding of how the eye works with the brain, how it deconstructs images that the brain stitches back up again, and how the two…

一名运营,自学一年前端,成功入职杭州某独角兽企业,他的面试经验和学习方法等分享...

大家好,我是若川。这是我的微信群里小伙伴年年 的投稿。他是19年毕业,之前做的是运营相关的工作,在我的交流群里非常活跃,自学一年前端,目前成功转行入职杭州一家独角兽企业。相信他的文章能带给大家一些启发和激励。0…

百度指数可视化_可视化指数

百度指数可视化Abstract:– Analysis of the visual representations of exponentials.– Proposals to solve current visualization issues.– Call to discussion to come up with a better visual representation convention.抽象: –分析指数的视觉表示形式。…

阿里云谦大佬:时间精力有限的情况下如何高效学习前端?

大家好,我是若川。最近组织了源码共读活动1个月,200人,一起读了4周源码,欢迎加我微信 ruochuan12 进群参与。今天分享一篇阿里云谦大佬的文章。昨天在群里也有小伙伴说到:大佬们是需要什么学什么,新手一般是…

sketch钢笔工具_Sketch和Figma,不同的工具等于不同的结果

sketch钢笔工具We like to compare the difference between various design programs and debate about which one is the most powerful. But we often forget to reflect on how using one of these tools is impacting our product. A powerful artist would say that he ca…

程序下载

Zaxis终端前置机 版 本下 载特 性1.20.1104.102ZaxisSetup.rar 分类: 程序下载转载于:https://www.cnblogs.com/baijinlong/archive/2011/05/13/2045263.html

提升效率的Vue组件开发和实战技巧

大家好我是若川。现在的大前端时代,是一个动荡纷争的时代,江湖中已经分成了很多门派,主要以Vue,React还有Angular为首,形成前端框架三足鼎立的局势。Vue在前端框架中的地位就像曾经的 jQuery,由于其简单易懂…

linux下telnet失败怎么处理,CentOS下telnet退出失败的解决办法

最近有CentOS用户反映在调试网络程序时出现了问题,服务虽然启动了,但客户端却无法连接上,用telnet连接后发现是Windows防火墙的问题,可是用telnet命令连接成功后发现退不出去了,这该怎么办?下面小编就给大家…

figma下载_Figma中的动态内容和颜色

figma下载First off, why use dynamic data?首先,为什么要使用动态数据? It’s easy to create and manage long lists of content 创建和管理一长串内容很容易 You get a better idea of what your product will look like with actual data 通过实际…

你可能不知道的package.json

大家好,我是若川。最近组织了源码共度活动:1个月,200人,一起读了4周源码,参与的小伙伴都表示收获很大。如果感兴趣可以点击链接扫码加我微信 ruochuan12。今天推荐一篇相对简单的文章。前言在上一篇npm init vitejs/ap…

基于上下文的rpn_构建事物-产品评论视频中基于上下文的情感分析

基于上下文的rpnThe word “Social” has taken a whole new meaning in today’s digital era. Simply going out to enjoy is no longer the only “social” criteria. Social now is — giving a peek in your personal and professional life to your connections. Facebo…

可爱的 Python: 使用 mechanize 和 Beautiful Soup 轻松收集 Web 数据

可爱的 Python: 使用 mechanize 和 Beautiful Soup 轻松收集 Web 数据 使用 Python 工具简化 Web 站点数据的提取和组织 David Mertz, Ph.D., 开发人员, Gnosis Software, Inc.从 2000 年开始,David Mertz 就一直在为 developerWorks 专栏 Charming Python 和 XML M…

JavaScript 断点调试技巧

大家好,我是若川。最近组织了源码共度活动:1个月,200人,一起读了4周源码,参与的小伙伴都表示收获很大。如果感兴趣可以点击链接扫码加我微信 ruochuan12。之前推荐过很多次调试文章,说明调试的重要性&#…

大学生电子设计大赛案例分析_为大学生设计问答平台—案例研究

大学生电子设计大赛案例分析Dealing with academic-related questions like picking a course, fulfilling a major requirement can be tedious and ineffective when you have to simultaneously balance school work, social activities, and focus on personal growth and …

最新最详细最简洁Eclipse调试PHP配置详解(Xdebug,Zend Debugger)

搬家注:该日志写于2011 年 04 月 07 日,Eclipse,PHP等版本号很多,更新也比较快,请注意文章中的版本。本文不一定帮您解决问题,但能给您一些解决问题的思路及一些概念。 最近开始做SRTP项目WebOS&#xff0c…

入门前端学习路线图【送书】

大家好,我是若川。记得点上方音频听小姐姐配音,超级好听。华章图书又赞助了书籍送福利给大家。本次送4本书的抽奖方式是:截止到9月6日(周一)20:00,在留言区留言任意内容。我会在留言区抽取「1位」关注我公众…

单选按钮设置为被选中状态_为什么要设置错误的按钮状态

单选按钮设置为被选中状态当正确的方法出错时 (When the right way goes wrong) Let’s say you want to create a click effect on an HTML button. The first idea that many people get is to do something that reproduces the feeling of the sound emitted by a real but…

「娃娃分享」-常见自校检分析实例.

自校检是许多软件的保护手段之一,对软件加个简单的壳再增加自校检在一定程序上可以抵挡住一大部分新手,不过,对许多人来说,这个保护已经很弱了。。下面讲几种常见的解决自校检方法,写的粗略,希望大家补充。…

用VC和MinGW导出dll的def和lib(a)文件

为什么80%的码农都做不了架构师?>>> 原文地址:http://zhangyafeikimi.iteye.com/blog/404580 有了dll文件需要导出def文件: pexports zlib1.dll > zlib1.def 有了dll和def文件,需要导出MinGW的.a文件:…

产品设计美学案例分析_美学在产品设计中的重要性

产品设计美学案例分析重点 (Top highlight)In one of my previous jobs, I had really interesting debates with the CEO regarding whether we should spend more time improving the way our app looks and feels. ‘How could he not care that the design is outdated?! …