我的任务是将 tiff 图像旋转 90°。我找到了一些如何通过 JAI 执行此操作的示例。所以我写了一段代码,形成了一个我发现的例子:
BufferedImage im = ImageIO.read(new FileInputStream(rasterFilePath));
PlanarImage pi = PlanarImage.wrapRenderedImage(im);
BufferedImage bi = pi.getAsBufferedImage();
AffineTransform at = new AffineTransform();
at.translate(-(im.getWidth() - im.getHeight()) / 2, (im.getWidth() - im.getHeight()) / 2);
at.rotate(Math.toRadians(90), bi.getWidth() / 2, bi.getHeight() / 2);
AffineTransformOp opRotated = new AffineTransformOp(at,AffineTransformOp.TYPE_BILINEAR);
im = opRotated.filter(bi, null);
ImageIO.write(im, "TIFF", new FileOutputStream(rasterFilePath));
现在,一个问题是我旋转了一个图像,但我失去了颜色。我得到新的旋转图像,其中颜色为灰度。我使用gdal工具(gdalinfo)检查了操作前后图像的差异,我得到:
前:
Driver: GTiff/GeoTIFF
Files: C:\test.tif
Size is 6000, 7000
Coordinate System is `'
Metadata:
TIFFTAG_IMAGEDESCRIPTION=image description
TIFFTAG_XRESOLUTION=1000
TIFFTAG_YRESOLUTION=1000
TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
Image Structure Metadata:
COMPRESSION=LZW
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 7000.0)
Upper Right ( 6000.0, 0.0)
Lower Right ( 6000.0, 7000.0)
Center ( 3000.0, 3500.0)
Band 1 Block=6000x1 Type=Byte, ColorInterp=Palette
Color Table (RGB with 256 entries)
0: 0,0,0,255
1: 0,0,51,255
2: 0,0,102,255
3: 0,0,153,255
4: 0,0,204,255
5: 0,0,255,255
6: 0,51,0,255
7: 0,51,51,255
8: 0,51,102,255
.
.
.
254: 0,0,0,255
255: 0,0,0,255
Band 2 Block=6000x1 Type=Byte, ColorInterp=Undefined
后:
Driver: GTiff/GeoTIFF
Files: C:\test.tif
Size is 7000, 6000
Coordinate System is `'
Metadata:
TIFFTAG_XRESOLUTION=1
TIFFTAG_YRESOLUTION=1
TIFFTAG_RESOLUTIONUNIT=1 (unitless)
Image Structure Metadata:
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 6000.0)
Upper Right ( 7000.0, 0.0)
Lower Right ( 7000.0, 6000.0)
Center ( 3500.0, 3000.0)
Band 1 Block=7000x8 Type=Byte, ColorInterp=Gray
Band 2 Block=7000x8 Type=Byte, ColorInterp=Undefined
有谁知道我在这段代码中缺少什么?