1、文件路径配置
sprite-path: /home/mapplate/sprite/
2、实现类
package com.shgis.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.shgis.config.FileProperties;
import com.shgis.entity.EbufferedImage;
import com.shgis.exception.MsgException;
import com.shgis.service.SpriteService;
import com.shgis.utils.StringUtils;
import com.shgis.utils.Util;
import lombok.Cleanup;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class SpriteServiceImpl implements SpriteService {
private String spritePath;
private FileProperties fileProperties;
public SpriteServiceImpl(FileProperties fileProperties) {
this.fileProperties = fileProperties;
this.spritePath = fileProperties.getPath().getSpritePath();
}
@Override
public void generateSmartSprite() {
List<File> files = new ArrayList<>();
try {
files = new Util().getFiles(this.spritePath + "images" + File.separator + "icons");
} catch (Exception e) {
e.printStackTrace();
}
List<EbufferedImage> ebuffImages = new ArrayList<>();
for (File file : files) {
try {
BufferedImage bimg = ImageIO.read(file);
EbufferedImage ebimg = new EbufferedImage();
ebimg.setBufferedImage(bimg);
ebimg.setFileName(file.getName());
ebuffImages.add(ebimg);
} catch (IOException e) {
e.printStackTrace();
}
}
int type = ebuffImages.get(0).getBufferedImage().getType();
ebuffImages = ebuffImages.stream().sorted(Comparator.comparing(EbufferedImage::getWidth)).collect(Collectors.toList());
int imgMaxWidth = ebuffImages.stream().max(Comparator.comparing(EbufferedImage::getWidth)).get().getWidth();
int widthnum = 512;
if (imgMaxWidth > widthnum) {
widthnum = imgMaxWidth;
}
List<List<EbufferedImage>> rowparams = new ArrayList<List<EbufferedImage>>();
List<EbufferedImage> paramnowlist = new ArrayList<EbufferedImage>();
int countnum = 0;
for (int i = 0; i < ebuffImages.size(); i++) {
countnum += ebuffImages.get(i).getWidth();
if (countnum > widthnum) {
i = i - 1;
countnum = 0;
rowparams.add(paramnowlist);
paramnowlist = new ArrayList<EbufferedImage>();
} else {
paramnowlist.add(ebuffImages.get(i));
}
if (i == ebuffImages.size() - 1) {
rowparams.add(paramnowlist);
break;
}
}
int allheight = 0;
for (List<EbufferedImage> rowparam : rowparams) {
allheight += rowparam.stream().max(Comparator.comparing(EbufferedImage::getHeight)).get().getHeight();
}
BufferedImage finalImg = new BufferedImage(widthnum, allheight, BufferedImage.TYPE_INT_ARGB);
int heighttemp = 0;
JSONObject json = new JSONObject();
for (int i = 0; i < rowparams.size(); i++) {
int tempwidthnum = 0;
List<EbufferedImage> rowparam = rowparams.get(i);
for (int j = 0; j < rowparam.size(); j++) {
EbufferedImage smallImg = rowparam.get(j);
finalImg.createGraphics().drawImage(smallImg.getBufferedImage(), tempwidthnum, heighttemp, null);
JSONObject obj = new JSONObject();
obj.put("height", smallImg.getHeight());
obj.put("width", smallImg.getWidth());
obj.put("pixelRatio", 1);
obj.put("x", tempwidthnum);
obj.put("y", heighttemp);
String fileName = smallImg.getFileName().split("\\.")[0].split("@")[0];
json.put(fileName, obj);
tempwidthnum += rowparam.get(j).getWidth();
}
heighttemp += rowparam.stream().max(Comparator.comparing(EbufferedImage::getHeight)).get().getHeight();
}
try {
ImageIO.write(finalImg, "png", new File(this.spritePath + "spriteSmart.png"));
@Cleanup InputStream is = (InputStream) ImageIO.createImageInputStream(finalImg);
finalImg.flush();
FileUtils.writeStringToFile(new File(this.spritePath + "spriteSmart.json"), json.toString());
FileUtils.copyFile(new File(this.spritePath + "spriteSmart.png"), new File(this.spritePath + "spriteSmart@2x.png"));
FileUtils.copyFile(new File(this.spritePath + "spriteSmart.json"), new File(this.spritePath + "spriteSmart@2x.json"));
} catch (IOException e) {
e.printStackTrace();
}
}
}