|
@@ -9,8 +9,13 @@ import javax.annotation.Resource;
|
|
|
import javax.imageio.ImageIO;
|
|
|
import java.awt.*;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
-import java.io.*;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.util.Base64;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
|
* 生成图片服务
|
|
@@ -26,29 +31,27 @@ public class ImageServiceImpl implements IImageService {
|
|
|
String uuid = UUID.randomUUID().toString();
|
|
|
String rasterFilePath = uuid + "raster.png";
|
|
|
String wktFilePath = uuid + "wkt.png";
|
|
|
- String base64RasterImage = imageMapper.getRasterImage(wkt);
|
|
|
- String base64WktImage = imageMapper.getWktImage(wkt);
|
|
|
- if (base64RasterImage != null && base64WktImage != null) {
|
|
|
|
|
|
- base64RasterImage = base64RasterImage.replaceAll("\n", "");
|
|
|
- base64WktImage = base64WktImage.replaceAll("\n", "");
|
|
|
+ String base64RasterImage = imageMapper.getRasterImage(wkt);
|
|
|
+ base64ToImage(base64RasterImage, rasterFilePath);
|
|
|
|
|
|
|
|
|
- byte[] imageRasterBytes = Base64.getDecoder().decode(base64RasterImage);
|
|
|
- OutputStream osRater = new FileOutputStream(rasterFilePath);
|
|
|
- osRater.write(imageRasterBytes);
|
|
|
+ File imageFile = new File(rasterFilePath);
|
|
|
+ BufferedImage image = ImageIO.read(imageFile);
|
|
|
|
|
|
- byte[] imageWktBytes = Base64.getDecoder().decode(base64WktImage);
|
|
|
- OutputStream osWkt = new FileOutputStream(wktFilePath);
|
|
|
- osWkt.write(imageWktBytes);
|
|
|
+ int width = image.getWidth();
|
|
|
+ int height = image.getHeight();
|
|
|
|
|
|
-
|
|
|
- mergeImage(wktFilePath, rasterFilePath);
|
|
|
+ Map<String, Object> mapParam = new HashMap<>();
|
|
|
+ mapParam.put("wkt", wkt);
|
|
|
+ mapParam.put("width", width);
|
|
|
+ mapParam.put("height", height);
|
|
|
+ String base64WktImage = imageMapper.getWktImage(mapParam);
|
|
|
+ base64ToImage(base64WktImage, wktFilePath);
|
|
|
|
|
|
- System.out.println("Image saved as " + rasterFilePath + " " + wktFilePath);
|
|
|
- } else {
|
|
|
- System.out.println("No raster data found for the given polygon.");
|
|
|
- }
|
|
|
+
|
|
|
+ mergeImage(wktFilePath, rasterFilePath);
|
|
|
+ System.out.println("Image saved as " + rasterFilePath + " " + wktFilePath);
|
|
|
return "";
|
|
|
}
|
|
|
|
|
@@ -61,98 +64,42 @@ public class ImageServiceImpl implements IImageService {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
private void mergeImage(String wktFilePath, String rasterFilePath) throws Exception {
|
|
|
-
|
|
|
- BufferedImage image1 = ImageIO.read(new File(wktFilePath));
|
|
|
- BufferedImage image2 = ImageIO.read(new File(rasterFilePath));
|
|
|
+ BufferedImage image1 = ImageIO.read(new File(rasterFilePath));
|
|
|
+ BufferedImage image2 = ImageIO.read(new File(wktFilePath));
|
|
|
|
|
|
-
|
|
|
- int width = Math.max(image1.getWidth(), image2.getWidth());
|
|
|
- int height = image1.getHeight() + image2.getHeight();
|
|
|
+
|
|
|
+ int overlayX = (image1.getWidth() - image2.getWidth()) / 2;
|
|
|
+ int overlayY = (image1.getHeight() - image2.getHeight()) / 2;
|
|
|
|
|
|
-
|
|
|
- BufferedImage mergedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
|
|
+
|
|
|
+ BufferedImage mergedImage = new BufferedImage(image1.getWidth(), image1.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
|
|
|
|
|
-
|
|
|
+
|
|
|
Graphics2D g2d = mergedImage.createGraphics();
|
|
|
-
|
|
|
-
|
|
|
+ g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_OVER, 0.1f));
|
|
|
+
|
|
|
g2d.drawImage(image1, 0, 0, null);
|
|
|
|
|
|
-
|
|
|
- g2d.drawImage(image2, 0, image1.getHeight(), null);
|
|
|
+
|
|
|
+ g2d.drawImage(image2, overlayX, overlayY, null);
|
|
|
|
|
|
-
|
|
|
+
|
|
|
g2d.dispose();
|
|
|
|
|
|
-
|
|
|
- ImageIO.write(mergedImage, "png", new File("image.png"));
|
|
|
+
|
|
|
+ File output = new File("overlayedImage.png");
|
|
|
+ ImageIO.write(mergedImage, "png", output);
|
|
|
|
|
|
- System.out.println("图片合并成功!");
|
|
|
+ System.out.println("叠加图片成功保存至:" + output.getAbsolutePath());
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ void base64ToImage(String base64Str, String filePath) throws IOException {
|
|
|
+ base64Str = base64Str.replaceAll("\n", "");
|
|
|
+ base64Str = base64Str.replaceAll("\r", "");
|
|
|
+ byte[] imageRasterBytes = Base64.getDecoder().decode(base64Str);
|
|
|
+ OutputStream osRater = new FileOutputStream(filePath);
|
|
|
+ osRater.write(imageRasterBytes);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|