是否可以使用JAVA程序知道TIFF图像是否为Group4?
谢谢。
答案 0 :(得分:1)
根据Oracle Jai Docs,您可以使用getCompression
方法并检查您的压缩是否等于COMPRESSION_GROUP4
(int)。
或者您可以尝试使用RenderedOp和TIFFDirectory字段:
RenderedOp tiffFile= JAI.create("fileload","C:/test/20060829105323B10506002500200000000011.tiff");
int comp = getCompression(tiffFile);
if(comp == COMPRESSION_GROUP4)
System.out.println("Compression is : GROUP4");
对于getCompression自定义方法:
private static int getCompression(RenderedOp op) throws Exception {
int TAG_COMPRESSION = 259;
TIFFDirectory dir = (TIFFDirectory)op.getProperty("tiff_directory");
if(dir.isTagPresent(TAG_COMPRESSION)) {
TIFFField compField = dir.getField(TAG_COMPRESSION);
return compField.getAsInt(0);
}
return 0;
}
更新:要使用SeekableStream,您需要一个ImageReader来避免加载异常。
Iterator readersIterator = ImageIO.getImageReadersByFormatName("tif");
ImageReader imageReader = (ImageReader)readersIterator.next();
ImageInputStream imageInputStream;
try {
imageInputStream = new FileImageInputStream(new File("C:/test/20060829105323B10506002500200000000011.tiff"));
imageReader.setInput(imageInputStream,false, true);
/* Take a input from a file */
FileSeekableStream fileSeekableStream;
fileSeekableStream = new FileSeekableStream(inputTifImagePath);
/* create ImageDecoder to count your pages from multi-page tiff */
ImageDecoder iDecoder = ImageCodec.createImageDecoder("tiff", fileSeekableStream, null);
/* count the number of pages inside the multi-page tiff */
int pageCount = iDecoder.getNumPages();
/* use first for loop to get pages one by one */
for(int page = 0; page < pageCount; page++){
/* get image metadata for each page */
IIOMetadata imageMetadata = imageReader.getImageMetadata(page);
/*
* The root of all the tags for this image is the IFD (Image File Directory).
* Get the IFD from where we can get all the tags for the image.
*/
TIFFDirectory ifd = TIFFDirectory.createFromMetadata(imageMetadata);
/* Create a Array of TIFFField*/
TIFFField[] allTiffFields = ifd.getTIFFFields();
/* use second for loop to get all field data */
for (int i = 0; i < allTiffFields.length; i++) {
TIFFField tiffField = allTiffFields[i];
/* name of property */
String nameOfField = tiffField.getTag().getName();
/* Type of property (optional) */
String typeOfField = TIFFField.getTypeName(tiffField.getType());
/* Tag no. of the property (optional) */
int numberOfField = tiffField.getTagNumber(); }// we are looking for 259, jump optional step out of loop
TIFFField compField = ifd.getField(259);
/* Value of Property*/
int valueOfField = compField.getAsInt(0);
让我知道它是否有效,以便我可以更新我的答案。
答案 1 :(得分:1)
谢谢。我的新代码如下
public static void TesterImage() throws Exception
{
Iterator readersIterator = ImageIO.getImageReadersByFormatName("tif");
ImageReader imageReader = (ImageReader)readersIterator.next();
ImageInputStream imageInputStream;
try {
File file = new File("/Users/houssam/Pictures/statue.tiff");
imageInputStream = new FileImageInputStream(file);
imageReader.setInput(imageInputStream);
FileSeekableStream fileSeekableStream;
fileSeekableStream = new FileSeekableStream(file.getCanonicalPath());
//ImageDecoder iDecoder = ImageCodec.createImageDecoder("tiff", fileSeekableStream, null);
IIOMetadata imageMetadata = imageReader.getImageMetadata(0);
TIFFDirectory ifd = TIFFDirectory.createFromMetadata(imageMetadata);
if(getCompression(ifd)==COMPRESSION_GROUP4)
System.out.println("Est de type Group4");
else
System.out.println("N'est pas de type Group4");
}
catch (Exception e)
{
e.printStackTrace();
}
}
static int TAG_COMPRESSION = 259;
public static int getCompression(TIFFDirectory ifd) throws Exception {
TIFFField[] allTiffFields = ifd.getTIFFFields();
for (int i = 0; i < allTiffFields.length; i++) {
TIFFField tiffField = allTiffFields[i];
String nameOfField = tiffField.getTag().getName();
String typeOfField = TIFFField.getTypeName(tiffField.getType());
System.out.println("Le nom est:"+nameOfField);
System.out.println("Le type est:"+typeOfField);
int numberOfField = tiffField.getTagNumber(); // we are looking for 259, jump optional step out of loop
System.out.println(numberOfField + " Is The Number");
if(numberOfField==TAG_COMPRESSION)
{
TIFFField compField = ifd.getTIFFField(TAG_COMPRESSION);
int valueOfField = compField.getAsInt(0);
return valueOfField;
}
}
return 0;
}