DPI的获取:
Tiiff文件保存 压缩 处理多页 DPI的设定 :
private long[] getDPI(SeekableStream s) {
long[] dpi = new long[] { 0L, 0L };
try {
TIFFDirectory tiffDir = new TIFFDirectory(s, 0);
long IFDOffset = tiffDir.getIFDOffset();
if (IFDOffset != 0L) {
boolean hasX = false;
boolean hasY = false;
tiffDir = new TIFFDirectory(s, IFDOffset, 0);
TIFFField[] tiffFields = tiffDir.getFields();
if (tiffFields != null && tiffFields.length > 0) {
for (int k = 0; k < tiffFields.length; k++) {
TIFFField field = tiffFields[k];
int tag = field.getTag();
if (tag == TIFFImageDecoder.TIFF_X_RESOLUTION) {
long data[][] = field.getAsRationals();
if(data.length>0 && data[0].length>0){
dpi[0] = data[0][0];
}
hasX = true;
} else if (tag == TIFFImageDecoder.TIFF_Y_RESOLUTION) {
long data[][] = field.getAsRationals();
if(data.length>0 && data[0].length>0){
dpi[1] = data[0][0];
}
hasY = true;
}
if(hasX && hasY){
break;
}
}
}
}
} catch (Exception ex) {
System.out.println("DPI取得中エラー発生しました。");
}
return dpi;
}
Tiiff文件保存 压缩 处理多页 DPI的设定 :
private void saveAsMultipageTIFF(RenderedImage[] image, String filename, long[] dpiData)
throws java.io.IOException {
OutputStream out = new FileOutputStream(filename);
TIFFEncodeParam param = new TIFFEncodeParam();
param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
ImageEncoder encoder = ImageCodec
.createImageEncoder("TIFF", out, param);
Vector vector = new Vector();
for (int i = 1; i < image.length; i++) {
vector.add(image[i]);
}
param.setExtraImages(vector.iterator());
if(dpiData[0]>0 || dpiData[1] >0){
int fieldCount = 1;
if(dpiData[0]>0 && dpiData[1] >0){
fieldCount = 2;
}
TIFFField[] tiffFields = new TIFFField[fieldCount];
int index =0;
if(dpiData[0]>0){
long xdpiArray[][] = new long[1][2];
xdpiArray[0] = new long[] { dpiData[0], 1 };
TIFFField xRes = new TIFFField(TIFFImageDecoder.TIFF_X_RESOLUTION,
TIFFField.TIFF_RATIONAL, 1, xdpiArray);
tiffFields[index++] = xRes;
}
if(dpiData[1]>0){
long ydpiArray[][] = new long[1][2];
ydpiArray[0] = new long[] { dpiData[1], 1 };
TIFFField yRes = new TIFFField(TIFFImageDecoder.TIFF_Y_RESOLUTION,
TIFFField.TIFF_RATIONAL, 1, ydpiArray);
tiffFields[index] = yRes;
}
param.setExtraFields(tiffFields);
}
encoder.encode(image[0]);
out.close();
}