0
点赞
收藏
分享

微信扫一扫

Demo:第二章:Java实现随机图像生成(人像,汽车

大南瓜鸭 2022-08-13 阅读 72

}

//新闻图片内容转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依赖:

<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-all -->

<dependency>

<groupId>org.apache.xmlgraphics</groupId>

<artifactId>batik-all</artifactId>

<version>1.12</version>

<type>pom</type>

</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/fop -->

<dependency>

<groupId>org.apache.xmlgraphics</groupId>

<artifactId>fop</artifactId>

<version>2.4</version>

</dependency>

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

Demo:第二章:Java实现随机图像生成(人像,汽车

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

*/

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

Transcoder transcoder = new PDFTranscoder();

svgConverte(svgCode, out, transcoder);

}

/**

  • SVG转PDF

  • @param svgFile SVG文件

  • @param outFile 输出文件

  • @throws TranscoderException

  • @throws IOException

*/

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

Transcoder transcoder = new PDFTranscoder();

svgConverte(svgFile, outFile, transcoder);

}

private void svgConverte(String svgCode, String outpath, Transcoder transcoder) throws IOException, TranscoderException {

svgConverte(svgCode, getOutputStream(outpath), transcoder);

}

private static void svgConverte(File svg, File outFile, Transcoder transcoder) throws IOException, TranscoderException {

svgConverte(svg2String(getInputStream(svg)), getOutputStream(outFile), transcoder);

}

private static void svgConverte(String svgCode, OutputStream out, Transcoder transcoder) throws IOException, TranscoderException {

svgCode = svgCode.replaceAll(":rect", "rect");

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

TranscoderOutput output = new TranscoderOutput(out);

svgConverte(input, output, transcoder);

}

private static void svgConverte(TranscoderInput input, TranscoderOutput output, Transcoder transcoder) throws IOException, TranscoderException {

transcoder.transcode(input, output);

}

public static InputStream getInputStream(File file) throws IOException {

return new FileInputStream(file);

}

public InputStream getInputStream(String filepath) throws IOException {

File file = new File(filepath);

if (file.exists())

return getInputStream(file);

else

return null;

}

public static OutputStream getOutputStream(File outFile) throws IOException {

return new FileOutputStream(outFile);

}

public OutputStream getOutputStream(String outpath) throws IOException {

File file = new File(outpath);

if (!file.exists())

file.createNewFile();

return getOutputStream(file);

}

/**

  • 默认使用编码UTF-8 SVG文件输入流转String

  • @param svgFile

  • @return SVG代码

  • @throws IOException

*/

public static String svg2String(File svgFile) throws IOException {

InputStream in = getInputStream(svgFile);

return svg2String(in, "UTF-8");

}

/**

  • SVG文件输入流转String

  • @param svgFile

  • @return SVG代码

  • @throws IOException

*/

public String svg2String(File svgFile, String charset) throws IOException {

InputStream in = getInputStream(svgFile);

return svg2String(in, charset);

}

/**

  • 默认使用编码UTF-8SVG输入流转String

  • @param in

  • @return SVG代码

*/

public static String svg2String(InputStream in) {

return svg2String(in, "UTF-8");

}

/**

举报

相关推荐

0 条评论