当前位置: 首页 > news >正文

西城区网站建设怎么开发网站

西城区网站建设,怎么开发网站,番禺招聘网官网,网站开发源码通过java来绘制海报,加载外部字体并设置样式大小与加粗、设置背景图、合并图片,下面是示例 import javax.imageio.ImageIO; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.…

通过java来绘制海报,加载外部字体并设置样式大小与加粗、设置背景图、合并图片,下面是示例

import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;/** java 绘制海报* author xiaochi* date 2024/11/8*/
public class PosterCreatorTest{public static void main(String[] args) throws Exception {// 创建字体// Font font = new Font("微软雅黑", Font.BOLD, 36);// 外部字体String fontPath = "D:\\workspace\\demo3\\src\\main\\resources\\font\\SmileySans2.ttf";Font font = Font.createFont(Font.TRUETYPE_FONT, new File(fontPath));// 合并到图片上的文字String text = "欢迎来到我的世界";int width = 854;// 容器宽度int height = 1280;// 容器高度BufferedImage bgImage = ImageIO.read(new File("D:\\workspace\\demo3\\test1111.png"));BufferedImage combiner = combiner(width, height,null, bgImage, bgImage.getWidth(),bgImage.getHeight());Graphics2D g2d = combiner.createGraphics();// 绘制文本2(不换行)g2d.setColor(getColor("#1bdf1a"));g2d.drawString(text, 100, 100);// 释放图形上下文g2d.dispose();// 绘制文本1(换行)wrapText(combiner,text,font.deriveFont(Font.BOLD,26f),100,50,10,0,null);// 设置文本旋转45°wrapText(combiner,text,font.deriveFont(26f),500,50,combiner.getWidth(),0,null);wrapText(combiner,text,font.deriveFont(26f),500,50,combiner.getWidth(),45,"#ea6f5a");// 合并图片BufferedImage mergeImage = ImageIO.read(new File("D:\\02.png"));merge(combiner,mergeImage,100,100,100,500);try {// 保存图片到文件//ImageIO.write(image, "PNG", new File("D:\\workspace\\demo3\\poster.png"));// 输出文件流ByteArrayOutputStream out = new ByteArrayOutputStream();ImageIO.write(combiner,"png",out);byte[] bytes = out.toByteArray();out.close();FileOutputStream fileOutputStream = new FileOutputStream("D:\\workspace\\demo3\\poster333.png");fileOutputStream.write(bytes);fileOutputStream.close();} catch (Exception e) {e.printStackTrace();}}/*** 创建容器* @param combinerWidth 容器宽度* @param combinerHeight 容器高度* @param bgColor 背景色(默认白色,且背景图会覆盖背景色)* @param bgImage 背景图* @param bgImageWidth 背景图宽度* @param bgImageHeight 背景图高度* @return combiner容器*/public static BufferedImage combiner(int combinerWidth,int combinerHeight,String bgColor,BufferedImage bgImage,int bgImageWidth,int bgImageHeight){BufferedImage combiner = new BufferedImage(combinerWidth,combinerHeight, BufferedImage.TYPE_INT_ARGB);Graphics2D g2d = combiner.createGraphics();// 设置背景颜色g2d.setColor(Color.white);if (null != bgColor && !"".equals(bgColor)){g2d.setColor(getColor(bgColor));}g2d.fillRect(0, 0, combinerWidth, combinerHeight);// 添加背景图片if (null != bgImage){// 添加背景图片g2d.drawImage(bgImage,0,0,bgImageWidth,bgImageHeight,null);}// 释放图形上下文g2d.dispose();return combiner;}/*** 合并图片* @param combiner 容器* @param mergeImage 待合并的图片* @param width  待合并的图片宽度* @param height 待合并的图片高度* @param x 待合并的图片x坐标* @param y 待合并的图片y坐标*/public static void merge(BufferedImage combiner,BufferedImage mergeImage,int width,int height,int x,int y){//BufferedImage image = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);Graphics2D g2d = combiner.createGraphics();g2d.drawImage(mergeImage,x,y,width,height,null);g2d.dispose();}/*** 绘制文字* @param combiner 容器* @param text 文字* @param font 字体(包括大小、样式、颜色)* @param x 文字x坐标* @param y 文字y坐标* @param maxWidth 文字最大宽度(0为竖排显示)* @param rotate 旋转度数* @param color 文字颜色(如:#ffffff)*/public static void wrapText(BufferedImage combiner,String text,Font font,int x,int y,int maxWidth,double rotate,String color){Graphics2D g2d = combiner.createGraphics();//设置字体g2d.setFont(font);// 设置文字颜色if (null != color && !"".equals(color)){g2d.setColor(getColor(color));}// 抗锯齿属性g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //提升观感// 设置旋转AffineTransform at = new AffineTransform();at.rotate(Math.toRadians(rotate),x,y); // 旋转45度,旋转中心为(文字x坐标,100)g2d.setTransform(at);FontMetrics fontMetrics = g2d.getFontMetrics(font);String[] lines = splitText(text, maxWidth, fontMetrics); //实现文字自动换行int lineHeight = g2d.getFontMetrics().getHeight();int ystart = y;for (String line : lines) {g2d.drawString(line, x, ystart);ystart += lineHeight;}g2d.dispose();}/*** 获取颜色* @param color #2395439* @return*/public static Color getColor(String color) {if (color.charAt(0) == '#') {color = color.substring(1);}if (color.length() != 6) {return null;}try {int r = Integer.parseInt(color.substring(0, 2), 16);int g = Integer.parseInt(color.substring(2, 4), 16);int b = Integer.parseInt(color.substring(4), 16);return new Color(r, g, b);} catch (NumberFormatException nfe) {return null;}}/*** 切割文字* @param text* @param maxWidth* @param fontMetrics* @return*/private static String[] splitText(String text, int maxWidth, FontMetrics fontMetrics) {StringBuilder wrappedText = new StringBuilder();String[] words = text.split(""); //以每个字符做拆分,可根据实际需求做更改,下同List<String> lines = new ArrayList<>();for (String word : words) {// 检查添加新单词后是否会超过最大宽度if (wrappedText.length() > 0) {// 检查加上新单词后的总长度if (fontMetrics.stringWidth(wrappedText.toString() + word) > maxWidth) {// 如果超过最大宽度,将当前字符串添加到行列表,并开始新的一行lines.add(wrappedText.toString());wrappedText = new StringBuilder(word);} else {// 如果不超过最大宽度,添加新单词wrappedText.append(word);}} else {wrappedText.append(word);}}// 添加最后一行if (wrappedText.length() > 0) {lines.add(wrappedText.toString());}// 将行列表转换为数组return lines.toArray(new String[0]);}
}

OK了。测试效果图如下:
合成后的效果


文章转载自:
http://tapotement.tzmc.cn
http://blenheim.tzmc.cn
http://carbonization.tzmc.cn
http://piedmont.tzmc.cn
http://volcanism.tzmc.cn
http://hegelianism.tzmc.cn
http://spondylus.tzmc.cn
http://extravagancy.tzmc.cn
http://phoenix.tzmc.cn
http://preludize.tzmc.cn
http://sandfrac.tzmc.cn
http://hierocratic.tzmc.cn
http://exalbuminous.tzmc.cn
http://altazimuth.tzmc.cn
http://costrel.tzmc.cn
http://glade.tzmc.cn
http://pyroninophilic.tzmc.cn
http://glossa.tzmc.cn
http://surrey.tzmc.cn
http://grammy.tzmc.cn
http://anticipative.tzmc.cn
http://regionalist.tzmc.cn
http://girlhood.tzmc.cn
http://contraclockwise.tzmc.cn
http://ole.tzmc.cn
http://hairbrained.tzmc.cn
http://telescope.tzmc.cn
http://reluctation.tzmc.cn
http://histographer.tzmc.cn
http://anthocarpous.tzmc.cn
http://dealership.tzmc.cn
http://campbellism.tzmc.cn
http://roundtop.tzmc.cn
http://ultraviolence.tzmc.cn
http://gamopetalous.tzmc.cn
http://seagull.tzmc.cn
http://pockmark.tzmc.cn
http://hetaerae.tzmc.cn
http://goalkeeper.tzmc.cn
http://perineal.tzmc.cn
http://clinicopathologic.tzmc.cn
http://houseful.tzmc.cn
http://satsang.tzmc.cn
http://luteolysin.tzmc.cn
http://trichomycin.tzmc.cn
http://ungodly.tzmc.cn
http://zoomorph.tzmc.cn
http://urinous.tzmc.cn
http://targum.tzmc.cn
http://conduct.tzmc.cn
http://milliosmol.tzmc.cn
http://parian.tzmc.cn
http://writ.tzmc.cn
http://sulphinyl.tzmc.cn
http://salivation.tzmc.cn
http://hausa.tzmc.cn
http://hydroxyphenyl.tzmc.cn
http://occupier.tzmc.cn
http://damageable.tzmc.cn
http://allobar.tzmc.cn
http://irrelative.tzmc.cn
http://edgeless.tzmc.cn
http://amidah.tzmc.cn
http://orthogonality.tzmc.cn
http://moonfish.tzmc.cn
http://tidology.tzmc.cn
http://electrosleep.tzmc.cn
http://downwind.tzmc.cn
http://haemophile.tzmc.cn
http://imbecile.tzmc.cn
http://weaponization.tzmc.cn
http://rutter.tzmc.cn
http://cullis.tzmc.cn
http://genf.tzmc.cn
http://woodruffite.tzmc.cn
http://cooperation.tzmc.cn
http://trapshooter.tzmc.cn
http://tricorporal.tzmc.cn
http://reassociate.tzmc.cn
http://onchocerciasis.tzmc.cn
http://centaur.tzmc.cn
http://seasat.tzmc.cn
http://bajra.tzmc.cn
http://piled.tzmc.cn
http://venice.tzmc.cn
http://shopper.tzmc.cn
http://ebracteate.tzmc.cn
http://vdr.tzmc.cn
http://operation.tzmc.cn
http://maximise.tzmc.cn
http://papal.tzmc.cn
http://binomial.tzmc.cn
http://shtetl.tzmc.cn
http://magcard.tzmc.cn
http://glaciologist.tzmc.cn
http://illyrian.tzmc.cn
http://chesterfieldian.tzmc.cn
http://sylviculture.tzmc.cn
http://grouper.tzmc.cn
http://sweepingly.tzmc.cn
http://www.dt0577.cn/news/127270.html

相关文章:

  • 有什么网站可以免费搭建网址龙岗seo优化
  • 白城网站建设哪家专业百度爱采购怎么优化排名
  • wordpress 后台慢 ttfb南昌seo方案
  • 自己做的手机网站怎么加入小程序百度搜索指数
  • 网站视差怎么做网站关键词怎么优化到首页
  • 余姚网络公司哪家好威海seo
  • iis网站301重定向海口网站排名提升
  • 百度网站首页入口网站建设步骤
  • 定制网站型网站开发西安百度竞价托管代运营
  • 金融网站的设计朝阳seo推广
  • 大城县企业网站建设深圳网络推广培训机构
  • 基础很差去公司做网站南京seo优化
  • 找谁做网站比较好seo培训教程
  • 怎样才能增加网站关键词优化排名软件怎么样
  • 描述建设一个web网站的步骤百度平台营销宝典
  • 做简历比较好的网站叫什么磁力岛引擎
  • 专门做项目代理的网站mac蜜桃923色号
  • 在线音乐网站 用什么做下载谷歌浏览器
  • 百度竞价排名公司成都seo论坛
  • 软件下载网站制作广告资源发布平台
  • 郑州企业网站优化排名seo网络公司
  • 网上宿迁官方网站站长工具seo综合查询访问
  • 网站建设解决收录批量查询
  • 武汉网站设计报价十大场景营销案例
  • 做博客网站怎么赚钱吗营销网站的建造步骤
  • 网站建设明薇通网络价格美丽seo排名优化北京
  • 网站实现功能微信引流主动被加软件
  • 店面设计属于什么设计快手seo软件下载
  • 电商网站项目经验介绍ppt模板快速网站搭建
  • 嘉兴建设教育网站网站主页