Demo:第二章:Java实现随机图像生成(人像,汽车,房屋等等)(1)

在这里插入图片描述

https://avatars.dicebear.com/api/jdenticon/jeden.svg

在这里插入图片描述当然还可以控制头像的一些属性,比如发型,肤色,鼻子,嘴巴等

具体可以参考:https://avatars.dicebear.com/styles/avataaars

具体运用到实战:

首先是需要调这个第三方的api接口,它会返回给你一串字符串,我们拿到字符串,写入文件,注意后缀要以.svg结尾,否则这个图片无法查看。(一般区块链的图片,上链的大多都是svg文件)

然后我们需要将这个图片给前端展示,这里就需要将svg转成jpg(苹果前端和苹果前端也是可以处理的,不过比较麻烦,这里就需要我们处理一下)。

接着将图片转成jpg之后,还需要将它上传到图片存储服务器上,这里我以AWS的S3为例。

最后jpg图片上传成功以后,需要清理之前在服务器上生成的svg和jpg文件。

/**

  • @Description newsContentPicture为需要生成图片的内容

  • @Author zhiwei Liao

  • @Date 2021/9/14 16:28

**/

@Override

public String uploadNewsContentPictureS3(String newsContentPicture) throws Exception{

String osName = System.getProperties().getProperty(“os.name”);

String svgPath = null;

String jpgPath = null;

URL url = null;

log.info(“========操作系统:” + osName);

String fileName = String.valueOf(System.currentTimeMillis());

if(osName.contains(“Linux”)){

svgPath = linuxSvgPath + fileName + “.svg”;

jpgPath = linuxJpgPath + fileName + “.jpg”;

}else if(osName.contains(“Windows”)){

svgPath = System.getProperty(“user.dir”) + “\svg\” + fileName + “.svg”;

jpgPath = System.getProperty(“user.dir”) + “\jpg\” + fileName + “.jpg”;

}

//新闻图片内容转svg

File svgFile = readIoStringToFile(newsContentPicture, svgPath);

if(svgFile != null){

//svg文件转jpg

File jpgFile = SVGConverterUtils.svgFileChangeJpg(svgFile, jpgPath);

if(jpgFile != null){

//上传jpg图片到s3

AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(clientRegion)

.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build();

PutObjectRequest request = new PutObjectRequest(bucketName, fileName, jpgFile);

ObjectMetadata metadata = new ObjectMetadata();

metadata.addUserMetadata(“x-amz-meta-title”, “someTitle”);

request.setMetadata(metadata);

request.setKey(fileName);

s3Client.putObject(request);

url = s3Client.getUrl(bucketName, fileName);

if (svgFile.exists()) {

svgFile.delete();

}

if (jpgFile.exists()) {

jpgFile.delete();

}

}

}

return url.toString();

}

/**

  • 把IO字符串输出到文件

  • @param ioString

  • @param filePath

*/

public static File readIoStringToFile(String ioString, String filePath) {

log.info(“========readIoStringToFile.filePath:” + filePath);

File file = null;

FileOutputStream fos = null;

try {

file = new File(filePath);

log.info(“========readIoStringToFile.file:” + file);

if (file.exists()) {

file.delete();

}else {

file.getParentFile().mkdir();

file.createNewFile();

}

fos = new FileOutputStream(file);

fos.write(ioString.getBytes(“UTF-8”));

fos.flush();

} catch (Exception e) {

log.error(“readIoStringToFile.Exception异常了:” + e.getMessage());

e.printStackTrace();

} finally {

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

log.error(“readIoStringToFile.IOException异常了:” + e.getMessage());

e.printStackTrace();

}

}

}

return file;

}

svg转jpg依赖:

org.apache.xmlgraphics

batik-all

1.12

pom

org.apache.xmlgraphics

fop

2.4

svg转jpg的工具类:

package com.common.entity.utils;

import lombok.extern.slf4j.Slf4j;

import org.apache.batik.transcoder.*;

import org.apache.batik.transcoder.image.JPEGTranscoder;

import org.apache.batik.transcoder.image.PNGTranscoder;

import org.apache.fop.render.ps.EPSTranscoder;

import org.apache.fop.render.ps.PSTranscoder;

import org.apache.fop.svg.PDFTranscoder;

import java.io.*;

/**

  • @author zhiwei Liao

  • @version 1.0

  • @Description 利用Apache Batik实现 SVG转PDF/PNG/JPG

  • @Date 2021/9/13 16:26

*/

@Slf4j

public class SVGConverterUtils {

public static void main(String[] args) throws Exception {

String svgpath = “E:\svgfile\c.svg”;

File svgFile = new File(svgpath);

String name = svgFile.getName();

name = name.substring(0, name.lastIndexOf(“.”));

// converter.svg2PDF(new File(svgpath), new File(“E:/” + name + “_SVG文件转PDF.pdf”));

// converter.svg2PNG(new File(svgpath), new File(“E:/” + name + “_SVG文件转PNG.png”));

svg2JPEG(new File(svgpath), new File(“E:/” + name + “_SVG文件转JPG.jpg”));

String svgCode = svg2String(new File(svgpath));

// converter.svg2PDF(svgCode, “D:/” + name + “_SVG代码转PDF.pdf”);

// converter.svg2PNG(svgCode, “D:/” + name + “_SVG代码转PNG.png”);

// converter.svg2JPEG(svgCode, “D:/” + name + “_SVG代码转JPG.jpg”);

// converter.svg2PDF(svgCode, new FileOutputStream(new File(“E:/svgfile/” + name + “_SVG代码转输出流.pdf”)));

// converter.svg2PNG(svgCode, new FileOutputStream(new File(“E:/svgfile/” + name + “_SVG代码转输出流.png”)));

// converter.svg2JPEG(svgCode, new FileOutputStream(new File(“E:/svgfile/” + name + “_SVG代码转输出流.jpg”)));

// converter.svg2EPS(svgCode, new FileOutputStream(new File(“E:/svgfile/” + name + “_SVG代码转输出流.eps”)));

// converter.svg2PS(svgCode, new FileOutputStream(new File(“E:/svgfile/” + name + “_SVG代码转输出流.ps”)));

svgtoeps(svgCode, new FileOutputStream(new File(“E:/svgfile/” + name + “_SVG代码转输出流.eps”)));

}

public static File svgFileChangeJpg(File svgFile, String jpgPath){

log.info(“========svgFileChangeJpg.jpgPath:” + jpgPath);

try {

File file = new File(jpgPath);

if (file.exists()) {

file.delete();

}else {

file.getParentFile().mkdir();

file.createNewFile();

}

log.info(“========svgFileChangeJpg.jpgFile:” + file);

log.info(“========svgFileChangeJpg.svgFile:” + svgFile);

svg2JPEG(svgFile, file);

return file;

} catch (Exception e) {

log.error(e.getMessage());

}

return null;

}

public static void svgtoeps(String svgCode, OutputStream os){

EPSTranscoder epsTranscoder = new EPSTranscoder();

try {

svgCode = svgCode.replaceAll(“:rect”, “rect”);

TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(svgCode.getBytes()));

TranscoderOutput output = new TranscoderOutput(os);

epsTranscoder.transcode(input, output);

os.flush();

os.close();

} catch (Exception e) {

}

}

/**

  • SVG转PNG

  • @param svgCode SVG代码

  • @param outpath 输出路径

  • @throws TranscoderException

  • @throws IOException

*/

public void svg2PNG(String svgCode, String outpath) throws TranscoderException, IOException {

Transcoder transcoder = new PNGTranscoder();

svgConverte(svgCode, outpath, transcoder);

}

/**

  • SVG转PNG

  • @param svgCode SVG代码

  • @param out 输出流

  • @throws TranscoderException

  • @throws IOException

*/

public void svg2PNG(String svgCode, OutputStream out) throws TranscoderException, IOException {

Transcoder transcoder = new PNGTranscoder();

svgConverte(svgCode, out, transcoder);

}

/**

  • SVG转PNG

  • @param svgFile SVG文件

  • @param outFile 输出文件

  • @throws TranscoderException

  • @throws IOException

*/

public void svg2PNG(File svgFile, File outFile) throws TranscoderException, IOException {

Transcoder transcoder = new PNGTranscoder();

svgConverte(svgFile, outFile, transcoder);

}

/**

  • SVG转JPG

  • @param svgCode SVG代码

  • @param outpath 输出路径

  • @throws TranscoderException

  • @throws IOException

*/

public void svg2JPEG(String svgCode, String outpath) throws TranscoderException, IOException {

Transcoder transcoder = new JPEGTranscoder();

//为防止ERROR: The JPEG quality has not been specified. Use the default one: no compression 错误,需如下配置

transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, 0.99f);

svgConverte(svgCode, outpath, transcoder);

}

/**

  • SVG转JPG

  • @param svgCode SVG代码

  • @param out 输出流

  • @throws TranscoderException

  • @throws IOException

*/

public void svg2JPEG(String svgCode, OutputStream out) throws TranscoderException, IOException {

Transcoder transcoder = new JPEGTranscoder();

//为防止ERROR: The JPEG quality has not been specified. Use the default one: no compression 错误,需如下配置

transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, 0.99f);

svgConverte(svgCode, out, transcoder);

}

/**

  • SVG转JPG

  • @param svgFile SVG文件

  • @param outFile 输出文件

  • @throws TranscoderException

  • @throws IOException

*/

public static void svg2JPEG(File svgFile, File outFile) throws TranscoderException, IOException {

Transcoder transcoder = new JPEGTranscoder();

//为防止ERROR: The JPEG quality has not been specified. Use the default one: no compression 错误,需如下配置

transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, 0.99f);

svgConverte(svgFile, outFile, transcoder);

}

/**

  • SVG转PDF

  • @param svgCode SVG代码

  • @param outpath 输出路径

  • @throws TranscoderException

  • @throws IOException

*/

public void svg2PDF(String svgCode, String outpath) throws TranscoderException, IOException {

Transcoder transcoder = new PDFTranscoder();

svgConverte(svgCode, outpath, transcoder);

}

/**

  • SVG转PS

  • @param svgCode

  • @param outpath

  • @throws TranscoderException

  • @throws IOException

*/

public void svg2PS(String svgCode, String outpath) throws TranscoderException, IOException {

Transcoder transcoder = new PSTranscoder();

svgConverte(svgCode, outpath, transcoder);

}

/**

  • SVG转PS

  • @param svgCode SVG代码

  • @param out 输出流

  • @throws TranscoderException

  • @throws IOException

*/

public void svg2PS(String svgCode, OutputStream out) throws TranscoderException, IOException {

Transcoder transcoder = new PSTranscoder();

svgConverte(svgCode, out, transcoder);

}

/**

  • SVG转EPS

  • @param svgCode SVG代码

  • @param out 输出流

  • @throws TranscoderException

  • @throws IOException

*/

public void svg2EPS(String svgCode, OutputStream out) throws TranscoderException, IOException {

Transcoder transcoder = new EPSTranscoder();

svgConverte(svgCode, out, transcoder);

}

/**

  • SVG转EPS

  • @param svgCode

  • @param outpath

  • @throws TranscoderException

  • @throws IOException

*/

public void svg2EPS(String svgCode, String outpath) throws TranscoderException, IOException {

Transcoder transcoder = new EPSTranscoder();

svgConverte(svgCode, outpath, transcoder);

}

/**

  • SVG转PDF

  • @param svgCode SVG代码

  • @param out 输出流

  • @throws TranscoderException

  • @throws IOException

*/
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Java)

总结

总体来说,如果你想转行从事程序员的工作,Java开发一定可以作为你的第一选择。但是不管你选择什么编程语言,提升自己的硬件实力才是拿高薪的唯一手段。

如果你以这份学习路线来学习,你会有一个比较系统化的知识网络,也不至于把知识学习得很零散。我个人是完全不建议刚开始就看《Java编程思想》、《Java核心技术》这些书籍,看完你肯定会放弃学习。建议可以看一些视频来学习,当自己能上手再买这些书看又是非常有收获的事了。


《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
.(img-TJblcUaB-1713822492631)]

[外链图片转存中…(img-oMGAMBXN-1713822492632)]

[外链图片转存中…(img-HbpndGPu-1713822492632)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Java)

[外链图片转存中…(img-5BAL62zu-1713822492632)]

总结

总体来说,如果你想转行从事程序员的工作,Java开发一定可以作为你的第一选择。但是不管你选择什么编程语言,提升自己的硬件实力才是拿高薪的唯一手段。

如果你以这份学习路线来学习,你会有一个比较系统化的知识网络,也不至于把知识学习得很零散。我个人是完全不建议刚开始就看《Java编程思想》、《Java核心技术》这些书籍,看完你肯定会放弃学习。建议可以看一些视频来学习,当自己能上手再买这些书看又是非常有收获的事了。

[外链图片转存中…(img-d6WC0DI5-1713822492632)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值