You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.9 KiB

5 years ago
package com.fr.plugin.external;
import com.fr.base.FRContext;
import com.fr.general.IOUtils;
import com.fr.general.xml.GeneralXMLTools;
import com.fr.stable.CoreGraphHelper;
import com.fr.stable.StableUtils;
import com.fr.stable.StringUtils;
import com.fr.stable.project.ProjectConstants;
import com.fr.workspace.WorkContext;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ImageManager {
private static ImageManager manager;
private static Image EMPTY = CoreGraphHelper.createBufferedImage(0, 0);
public static ImageManager getInstance() {
if (manager == null) {
manager = new ImageManager();
}
return manager;
}
private Map<String, ImageBean> cachedMap = new ConcurrentHashMap<String, ImageBean>();
private ExecutorService executorService = Executors.newSingleThreadExecutor();
private boolean compress = true;
public void release() {
cachedMap.clear();
executorService.shutdown();
}
public void updateImage(ExternalImageBackground background) {
updateImage(background.getId(), background.getImage());
}
public void updateImage(String id, Image image) {
updateImage(id, image, true);
}
public void updateImage(String id, Image image, boolean save) {
if (StringUtils.isNotEmpty(id) && image != null) {
cachedMap.put(id, new ImageBean(image));
if (save) {
saveImageToFile(image, id);
}
}
}
public void removeImage(String id) {
cachedMap.remove(id);
}
public String[] listAllIds() {
updateImageFromDisk();
return cachedMap.keySet().toArray(new String[cachedMap.size()]);
}
public Image findImageById(String id) {
ImageBean result = cachedMap.get(id);
if (result == null) {
updateImageFromDisk(id);
result = cachedMap.get(id);
if (result == null) {
return EMPTY;
}
}
return result.getImage();
}
public String findAttachId(String id) {
ImageBean result = cachedMap.get(id);
if (result == null) {
updateImageFromDisk(id);
result = cachedMap.get(id);
if (result == null) {
return StringUtils.EMPTY;
}
}
return result.getAttachment().getID();
}
public boolean isCompress() {
return compress;
}
public void setCompress(boolean compress) {
this.compress = compress;
}
public void updateImageFromDisk() {
String path = StableUtils.pathJoin(WorkContext.getCurrent().getPath(),
ProjectConstants.ASSETS_NAME, "background");
File imageFolder = new File(path);
if (!imageFolder.exists()) {
return;
}
File[] images = imageFolder.listFiles();
for (File imageFile : images) {
String name = imageFile.getName();
String id = name.substring(0, name.lastIndexOf("."));
if(cachedMap.containsKey(id)) {
continue;
}
try {
byte[] content = IOUtils.inputStream2Bytes(new FileInputStream(imageFile));
Image image = GeneralXMLTools.imageDecode(content);
updateImage(id, image, false);
} catch (Throwable e) {
FRContext.getLogger().debug("Image read failed: " + e.getMessage() + " id: " + id);
}
}
}
public void updateImageFromDisk(String targetId) {
String path = StableUtils.pathJoin(WorkContext.getCurrent().getPath(),
ProjectConstants.ASSETS_NAME, "background");
File imageFolder = new File(path);
if (!imageFolder.exists()) {
return;
}
File[] images = imageFolder.listFiles();
for (File imageFile : images) {
String name = imageFile.getName();
int dotIndex = name.lastIndexOf(".");
if (dotIndex <= 0) {
continue;
}
String id = name.substring(0, dotIndex);
if(cachedMap.containsKey(id) || !targetId.equals(id)) {
continue;
}
try {
byte[] content = IOUtils.inputStream2Bytes(new FileInputStream(imageFile));
Image image = GeneralXMLTools.imageDecode(content);
updateImage(id, image, false);
} catch (Throwable e) {
FRContext.getLogger().debug("Image read failed: " + e.getMessage() + " id: " + id);
}
}
}
private void cacheAllImage() {
executorService.execute(new Runnable() {
@Override
public void run() {
if(!(WorkContext.getCurrent().isLocal())) {
FRContext.getLogger().error("图片外置不支持远程设计!");
return;
}
String path = StableUtils.pathJoin(WorkContext.getCurrent().getPath(),
ProjectConstants.ASSETS_NAME, "background");
File imageFolder = new File(path);
if (!imageFolder.exists()) {
return;
}
File[] images = imageFolder.listFiles();
for (File imageFile : images) {
String name = imageFile.getName();
String id = name.substring(0, name.lastIndexOf("."));
try {
byte[] content = IOUtils.inputStream2Bytes(new FileInputStream(imageFile));
Image image = GeneralXMLTools.imageDecode(content);
updateImage(id, image, false);
} catch (Throwable e) {
FRContext.getLogger().debug("Image read failed: " + e.getMessage() + " id: " + id);
}
}
}
});
}
private void saveImageToFile(final Image image, final String id) {
executorService.execute(new Runnable() {
@Override
public void run() {
// 存到磁盘
String path = StableUtils.pathJoin(WorkContext.getCurrent().getPath(),
ProjectConstants.ASSETS_NAME, "background", id + ".png");
File imageFile = new File(path);
try {
StableUtils.makesureFileExist(imageFile);
FileOutputStream out = new FileOutputStream(imageFile);
IOUtils.writeImage(image, "png", out);
out.close();
} catch (IOException e) {
FRContext.getLogger().error(e.getMessage());
}
}
});
}
}