说起mapbox的雪碧图,估计大都数人搜索到的是sprite-cli。
现在网上找一堆关于sprite的资料,结果发现这个东西的安装实在是麻烦的不得了(安装过程自知),需要一堆的环境支持,到了最后终于安装成功了却发现生成的雪碧图居然没有颜色,funk,浪费我N久的时间。没办法谁让俺是程序员,就喜欢折腾。
最后找了一个用程序的写的生成雪碧图方法
1 public static InputStream mergeImage() throws IOException { 2 List<File> files = new ArrayList<>(); 3 files = getFilesByPath(files, "xxx"); 4 //创建BufferedImage 5 List<BufferedImage> buffImages = new ArrayList<>(); 6 for (File file : files) { 7 buffImages.add(ImageIO.read(file)); 8 } 9 int type = buffImages.get(0).getType(); 10 int chunkWidth = buffImages.get(0).getWidth(); 11 int chunkHeight = buffImages.get(0).getHeight(); 12 13 //设置拼接后图的大小和类型 14 int cols = 6; 15 int rows = files.size()/cols + 1; 16 BufferedImage finalImg = new BufferedImage(chunkWidth * cols, chunkHeight * rows, type); 17 18 JSONObject json = new JSONObject(); 19 //写入图像内容 20 for (int i = 0; i < rows; i++) { 21 for (int j = 0; j < cols; j++) { 22 int num = i*6+j; 23 if(num+1 <= files.size()) { 24 finalImg.createGraphics().drawImage(buffImages.get(num), chunkWidth * j, chunkHeight * i, null); 25 JSONObject obj = new JSONObject(); 26 obj.put("height", chunkHeight); 27 obj.put("width", chunkWidth); 28 obj.put("pixelRatio", type); 29 obj.put("x", chunkWidth * j); 30 obj.put("y", chunkHeight * i); 31 String fileName = files.get(num).getName().split("\\.")[0].split("@")[0]; 32 json.put(fileName, obj); 33 } 34 } 35 } 36 //输出拼接后的图像 37 ImageIO.write(finalImg, "png", new File("D:\\sprite.png")); 38 @Cleanup InputStream is=(InputStream) ImageIO.createImageInputStream(finalImg); 39 finalImg.flush(); 40 FileUtils.writeStringToFile(new File("D:\\sprite.json"), json.toString()); 41 System.out.println("完成拼接!"); 42 return is; 43 }
简单的不要要的。
总结一句话:折腾是因为你没有找对方法,做人处事都一样,得从经验中快速找到问题的解决方法
原文:https://www.cnblogs.com/llcj/p/12342363.html