ImageUtil.java 2.48 KB
package com.topdraw.sohu.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ImageUtil {

	public static Map<String, Integer> mapImageType = new HashMap<>();
	static {
		mapImageType.put("normal", -1);
		mapImageType.put("thumbnail", 0);
		mapImageType.put("poster", 1);
		mapImageType.put("stills", 2);
		mapImageType.put("icon", 3);
		mapImageType.put("title", 4);
		mapImageType.put("ad", 5);
		mapImageType.put("sketch", 6);
		mapImageType.put("background", 7);
		mapImageType.put("channel", 9);
		mapImageType.put("channel_bw", 10);
		mapImageType.put("channel_logo", 11);
		mapImageType.put("channel_name", 12);
		mapImageType.put("other", 99);
	}
	
	public static String convertStrImages2SpecialFormatJSON(String strImages) {
		JSONArray jsonArr;
		if (strImages != null) {
			jsonArr = JSONArray.parseArray(strImages);
		} else {
			jsonArr = new JSONArray();
		}
		Map<String, Object> mapImages = new HashMap<String, Object>();
		mapImages.put("map", new HashMap<String, Object>());
		mapImages.put("list", new ArrayList<Map<String, Object>>());
		
		for (int i = 0; i < jsonArr.size(); i++) {
			JSONObject jsonObj = jsonArr.getJSONObject(i);
			String strKey = ResourceUtil.getImageKeyByType(jsonObj.containsKey("type") ? jsonObj.getIntValue("type") : -1);
			if (((Map<String, Object>) mapImages.get("map")).containsKey(strKey)) {
				((ArrayList<Integer>) ((Map<String, Object>) mapImages.get("map")).get(strKey)).add(i);
			} else {
				List<Integer> listIndex = new ArrayList<Integer>();
				listIndex.add(i);
				((Map<String, Object>) mapImages.get("map")).put(strKey, listIndex);
			}
			Map<String, Object> mapImageDetail = new HashMap<String, Object>();
			mapImageDetail.put("id", jsonObj.getLong("id"));
			mapImageDetail.put("fileUrl", jsonObj.getString("fileUrl"));
			mapImageDetail.put("height", jsonObj.getInteger("height"));
			mapImageDetail.put("width", jsonObj.getInteger("width"));
			mapImageDetail.put("size", jsonObj.getInteger("size"));
			mapImageDetail.put("extension", jsonObj.getString("extension"));
			mapImageDetail.put("enable", jsonObj.getBoolean("enable"));
			mapImageDetail.put("name", jsonObj.getString("name"));
			mapImageDetail.put("csp_code", jsonObj.getString("csp_code"));
			((ArrayList<Map<String, Object>>) mapImages.get("list")).add(mapImageDetail);
		}
		return JSON.toJSONString(mapImages);
	}


}