首页 > 编程语言 >java 生成mapbox-gl 可以直接使用的雪碧图,包含对应json,图片大小无限制自动适配

java 生成mapbox-gl 可以直接使用的雪碧图,包含对应json,图片大小无限制自动适配

时间:2024-07-11 17:26:49浏览次数:19  
标签:java get int 适配 ebuffImages json import new

1、文件路径配置

sprite-path: /home/mapplate/sprite/

2、实现类

package com.shgis.service.impl;/**
 * Created by Administrator on 2021/10/9.
 */

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;

/**
 * @ClassName SpriteServiceImpl
 * @Description
 * @Author Administrator
 * @Date 2024/10/9 9:22
 * @Version V1.0
 */
@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();
        }
        //创建BufferedImage
        // List<BufferedImage> buffImages = new ArrayList<>();
        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);
                // buffImages.add(ImageIO.read(file));
            } 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", type);
                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 + "[email protected]"));
            FileUtils.copyFile(new File(this.spritePath + "spriteSmart.json"), new File(this.spritePath + "[email protected]"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

标签:java,get,int,适配,ebuffImages,json,import,new
From: https://blog.csdn.net/qq_28419035/article/details/140356913

相关文章