0
我試圖將多條條tiff圖像轉換爲單條tiff。我能夠使用Java的JAI API進行操作。但這些不是平臺獨立的。 我現在正在與Commons Imaging合作執行此任務。在這裏,我無法找到我能夠如何寫回單條圖像數據。使用Commons Imaging Multistrip單條tiff使用Commons Imaging
這是我寫到目前爲止的代碼片段。 (請注意,這僅僅是一個例子粗,我會修改代碼之前獲得單個條帶)
//get the metadata out a tiff file
final IImageMetadata metadata = Imaging.getMetadata(file);
iffImageMetadata tiffMetadata = null;
if(metadata instanceof TiffImageMetadata){
tiffMetadata = (TiffImageMetadata) metadata;
TiffImageMetadata.Directory dir = (TiffImageMetadata.Directory)tiffMetadata.getDirectories().get(0);
TiffImageData imgData = dir.getTiffImageData();
long offset = 0;
//check number of strips(can be done by getting TIFF TAG 273
DataElement[] imgDataElements = imgData.getImageData();
int noOfSTrips = imgDataElements.length;
if(noOfSTrips == 1){
return; //already a single strip
}
//merge all single strips to a single byte array
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
for(int i = 0; i< noOfSTrips; i++){
ByteSourceData data = (ByteSourceData) imgDataElements[i];
outputStream.write(data.getData());
}
List<TiffField> tiffFields = tiffMetadata.getAllFields();
TiffOutputField outputField = null;
for(TiffField tiffField : tiffFields){
String type = tiffField.getFieldTypeName();
switch(tiffField.getTag()) {
case 273:
if (type == FieldType.LONG.getName())
{
if (tiffField.getCount() > 1){
//this will be writen back to the output set
outputField = new TiffOutputField(tiffField.getTagInfo(), tiffField.getFieldType(), 1, outputStream.toByteArray());
offset = tiffField.getByteArrayValue()[0];
}
}
break;
}
}
TiffOutputSet outputSet = tiffMetadata.getOutputSet();
List<TiffOutputDirectory> outputDirectories = outputSet.getDirectories();
TiffOutputDirectory outputDir = outputDirectories.get(0);
outputDir.removeField(273);
outputDir.add(outputField);
//NOW WHAT TO DO??
//I HAVE SEEN ExifRewritter CLASS THAT CAN UPDATE TIFF WITH EXIF, BUT HOW I CAN UPDATE THIS BACK
}
任何指針將是有幫助的。
什麼是你目前的代碼不工作?代碼示例非常有用。 –
作者是否默認編寫條紋TIFF?您是否嘗試過簡單地將'TIFF_TAG_ROWS_PER_STRIP'值設置爲'-1',或圖像的像素高度? – haraldK
@haraldK我已經添加了一個示例代碼,因爲TiffImageMetadata在本質上是不可變的,所以我無法修改它的屬性。請讓我知道你對此的看法。 –