Java 调用 GDAL,使用编译好的库方法,获取 tif 文件信息及 tif 波段信息

2019/04 12 15:04

嫌麻烦者可直接下载已处理好的基于Java8的bin目录:https://download.csdn.net/download/antma/11110753

(含Java调用GDAL的所有DLL和gdal.jar包)

1、下载 releases of MapServer and GDAL

下载地址:http://www.gisinternals.com/release.php

下载相应版本:

下载解压:

2、将解压后 bin文件夹下所有 dll文件拷贝到jdk的bin目录下:

将bin/gdal/java目录下的gdalaljni.dll 文件也拷至jdk的bin目录下:

idea 建项目,引用上面的 gdal.jar,添加一个类 GDALTestApp.java ,执行即可拿到 tif 信息:

GDALTestApp.java 代码如下:

package gdaltest;

import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.Driver;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconstConstants;

public class GDALTestApp {

    public static void main(String[] args) {
        gdal.AllRegister();

        String fileName_tif = "E:\postGISToos\tif\1.tif";
        Dataset hDataset = gdal.Open(fileName_tif, gdalconstConstants.GA_ReadOnly);
        if (hDataset == null) {
            System.err.println("GDALOpen failed - " + gdal.GetLastErrorNo());
            System.err.println(gdal.GetLastErrorMsg());
            System.exit(1);
        }

        Driver hDriver = hDataset.GetDriver();
        System.out.println("Driver: " + hDriver.getShortName() + "/" + hDriver.getLongName());
        System.out.println("Size is " + hDataset.getRasterXSize() + ", " + hDataset.getRasterYSize());

        Band band = hDataset.GetRasterBand(1);
        System.out.println(band.getXSize());

        hDataset.delete();

        // 可选
        gdal.GDALDestroyDriverManager();
    }
}

这里是直接引的gdal.jar:

--转载请注明: https://www.macs.vip/archives/43