张洋洋
2025-02-25 1af1a0d208cd223be9bc574eebf1c21b45b3b117
src/main/java/com/se/simu/utils/TiffToRGBUtil.java
@@ -1,8 +1,8 @@
package com.se.simu.utils;
import org.apache.commons.imaging.*;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.Band;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconstConstants;
@@ -14,7 +14,7 @@
public class TiffToRGBUtil {
    public static void main(String[] args) throws Exception{
        tifToPng("D:\\城市内涝\\sem\\DEM.tif","D:\\城市内涝\\sem\\DEM.png");
       tifToPng("D:\\城市内涝\\sem\\tongzhou_1m_tif\\tongzhou_raster_4548_1m_clip_river_fill.tif","D:\\城市内涝\\sem\\tongzhou_1m_tif\\tongzhou_raster_4548_1m_clip_river_fill.png");
    }
    public static void tifToPng(String tifPath, String pngPath) throws Exception {
        // 注册所有的 GDAL 驱动
@@ -44,14 +44,13 @@
                // 假设高度信息存储在像素值中
                double pixelHeight = pixelValue * geoTransform[5];
                //height = -10000 + ((R * 256 * 256 + G * 256 + B) * 0.1)
                int value = (int) (pixelHeight + 10000) * 10;
                int value = (int) ((pixelHeight + 10000) * 10);
                //value=(R * 256 * 256 + G * 256 + B);
                int r = value / (256 * 256);
                value = value % (256 * 256);
                int g = value / 256;
                int b = value % 256;
                Color color = new Color(r, g, b);
                System.out.printf("Pixel (%d, %d): R=%d, G=%d, B=%d%n", x, y, r, g, b);
                int newRgb = color.getRGB();
                // 将处理后的像素颜色值设置到新的 PNG 图像中
                pngImage.setRGB(x, y, newRgb);