2015-10-21 43 views
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 

}

任何指針將是有幫助的。

+1

什麼是你目前的代碼不工作?代碼示例非常有用。 –

+1

作者是否默認編寫條紋TIFF?您是否嘗試過簡單地將'TIFF_TAG_ROWS_PER_STRIP'值設置爲'-1',或圖像的像素高度? – haraldK

+1

@haraldK我已經添加了一個示例代碼,因爲TiffImageMetadata在本質上是不可變的,所以我無法修改它的屬性。請讓我知道你對此的看法。 –

回答

2

所以我終於能夠將Multistrip tif圖像轉換爲單條。 基本上我從TiffImageWriterBase類Commons Imaging複製了一些代碼,這是由於rowsPerStrip被計算的原因,結果圖像總是一個Multistrip。 。我修改了這段代碼來設置rowsPerStrip = image.height。 這裏是我寫的代碼:

//source is any multistrip image 
File file = new File("D:\\Tiff\\image.tif"); 
BufferedImage src = Imaging.getBufferedImage(file); 
OutputStream os = new FileOutputStream(new File("D:\\Tiff\\modified_image.tif")); 
os = new BufferedOutputStream(os); 
final int width = src.getWidth(); 
final int height = src.getHeight(); 
//These are set as per CCITT 4 compression, you can modify these as per requirement 
int samplesPerPixel = 1; 
int bitsPerSample = 1; 
int photometricInterpretation =0; 
//setting rowsPerStrip equal to heigh of the image, this did the trick for me 
int rowsPerStrip = height;//This code was present originally -> stripSizeInBits/(1 * bitsPerSample * samplesPerPixel); 
rowsPerStrip = Math.max(1, rowsPerStrip); // must have at least one. 
//you can copy getStrips method from TiffImageWriterBase class or work upon yours own 
final byte[][] strips = getStrips(src, samplesPerPixel, bitsPerSample, rowsPerStrip); 
for (int i = 0; i < strips.length; i++) { 
    strips[i] = T4AndT6Compression.compressT6(strips[i], width, 
      strips[i].length/((width + 7)/8)); 
} 
final TiffElement.DataElement[] imageData = new TiffElement.DataElement[strips.length]; 
for (int i = 0; i < strips.length; i++) { 
    imageData[i] = new TiffImageData.Data(0, strips[i].length, strips[i]); 
} 
final TiffOutputSet outputSet = new TiffOutputSet(ByteOrder.LITTLE_ENDIAN); 
final TiffOutputDirectory directory = outputSet.addRootDirectory(); 
directory.add(TiffTagConstants.TIFF_TAG_IMAGE_WIDTH, width); 
directory.add(TiffTagConstants.TIFF_TAG_IMAGE_LENGTH, height); 
directory.add(TiffTagConstants.TIFF_TAG_PHOTOMETRIC_INTERPRETATION, 
     (short) photometricInterpretation); 
directory.add(TiffTagConstants.TIFF_TAG_COMPRESSION, 
     (short) TiffConstants.TIFF_COMPRESSION_CCITT_GROUP_4); 
directory.add(TiffTagConstants.TIFF_TAG_SAMPLES_PER_PIXEL, 
     (short) samplesPerPixel); 
directory.add(TiffTagConstants.TIFF_TAG_BITS_PER_SAMPLE, 
     (short) bitsPerSample); 
directory.add(TiffTagConstants.TIFF_TAG_ROWS_PER_STRIP, 
     rowsPerStrip); 
directory.add(TiffTagConstants.TIFF_TAG_RESOLUTION_UNIT, 
     (short) 2); 
PixelDensity pixelDensity = PixelDensity.createFromPixelsPerInch(72, 72); 
directory.add(TiffTagConstants.TIFF_TAG_XRESOLUTION, 
     RationalNumber.valueOf(pixelDensity.horizontalDensityInches())); 
directory.add(TiffTagConstants.TIFF_TAG_YRESOLUTION, 
     RationalNumber.valueOf(pixelDensity.verticalDensityInches())); 
final TiffImageData tiffImageData = new TiffImageData.Strips(imageData, 
     rowsPerStrip); 
directory.setTiffImageData(tiffImageData); 
//single strip image will be written to output stream 
new TiffImageWriterLossy().write(os, outputSet); 
相關問題