|
@@ -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 {
|
|
|
- // 读取两个PNG图片
|
|
|
- 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
|
|
|
- BufferedImage mergedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
|
|
+ // 创建一个合并后的图片对象,支持透明度
|
|
|
+ BufferedImage mergedImage = new BufferedImage(image1.getWidth(), image1.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
|
|
|
|
|
- // 创建Graphics2D对象
|
|
|
+ // 获取画布对象
|
|
|
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);
|
|
|
|
|
|
- // 释放Graphics2D对象
|
|
|
+ // 释放画布
|
|
|
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());
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- // // 矢量数据创建图片
|
|
|
- // private void wktToImage(String wkt) throws Exception {
|
|
|
- // // 创建一个 WKTReader 对象
|
|
|
- // WKTReader reader = new WKTReader(JTSFactoryFinder.getGeometryFactory());
|
|
|
- // Geometry geometry = reader.read(wkt);
|
|
|
- //
|
|
|
- // // 定义SimpleFeatureType
|
|
|
- // SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
|
|
|
- // builder.setName("Location");
|
|
|
- // builder.add("the_geom", Geometry.class);// 添加空间信息
|
|
|
- // builder.add("name", String.class);// 添加到属性字段里
|
|
|
- // final SimpleFeatureType TYPE = builder.buildFeatureType();
|
|
|
- //
|
|
|
- // // 创建SimpleFeature
|
|
|
- // SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
|
|
|
- // featureBuilder.add(geometry);
|
|
|
- // featureBuilder.add("Polygon1");
|
|
|
- // SimpleFeature feature = featureBuilder.buildFeature(null);
|
|
|
- //
|
|
|
- // // 创建 FeatureCollection 并添加 Feature
|
|
|
- // DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal", TYPE);
|
|
|
- // featureCollection.add(feature);
|
|
|
- //
|
|
|
- // // 创建 MapContent
|
|
|
- // MapContent map = new MapContent();
|
|
|
- // map.setTitle("WKT to Image");
|
|
|
- //
|
|
|
- // // 创建一个样式
|
|
|
- // Color outlineColor = Color.BLUE;
|
|
|
- // Color fillColor = Color.RED;
|
|
|
- // Style style = SLD.createPolygonStyle(outlineColor, fillColor, 0.5F);
|
|
|
- //
|
|
|
- // // 创建一个图层并添加到 MapContent
|
|
|
- // org.geotools.map.FeatureLayer layer = new org.geotools.map.FeatureLayer(featureCollection, style);
|
|
|
- // map.addLayer(layer);
|
|
|
- //
|
|
|
- // // 计算几何图形的包络并添加填充
|
|
|
- // Envelope envelope = geometry.getEnvelopeInternal();
|
|
|
- // double padding = envelope.getWidth() * 0.1; // 添加10%的填充
|
|
|
- // envelope.expandBy(padding);
|
|
|
- //
|
|
|
- // // 设置 MapViewport
|
|
|
- // MapViewport viewport = new MapViewport();
|
|
|
- // CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
|
|
|
- // viewport.setBounds(new org.geotools.geometry.jts.ReferencedEnvelope(envelope, crs));
|
|
|
- // map.setViewport(viewport);
|
|
|
- //
|
|
|
- // // 创建一个 StreamingRenderer
|
|
|
- // StreamingRenderer renderer = new StreamingRenderer();
|
|
|
- // renderer.setMapContent(map);
|
|
|
- //
|
|
|
- // // 创建 BufferedImage
|
|
|
- // BufferedImage image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_ARGB);
|
|
|
- // Graphics2D graphics = image.createGraphics();
|
|
|
- // renderer.paint(graphics, new Rectangle(800, 600), viewport.getBounds());
|
|
|
- //
|
|
|
- // // 保存图像
|
|
|
- // File file = new File("output.png");
|
|
|
- // ImageIO.write(image, "png", file);
|
|
|
- //
|
|
|
- // System.out.println("Image saved to " + file.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);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|