Tika

时间:2021-03-07 09:52:29

1.解析图片

@Test
public void test1Image() throws IOException, SAXException, TikaException{
String filePath = "src/main/resources/baby.jpg";
//检测文件类型
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
FileInputStream inputstream = new FileInputStream(new File(filePath));
ParseContext pcontext = new ParseContext(); //Jpeg Parse
JpegParser JpegParser = new JpegParser();
JpegParser.parse(inputstream, handler, metadata,pcontext);
System.out.println("文档内容:" + handler.toString());
System.out.println("文档元数据:");
String[] metadataNames = metadata.names(); for(String name : metadataNames) {
System.out.println(name + ": " + metadata.get(name));
}
}

结果:

文档内容:
文档元数据:
Number of Components: 3
Flags 0: 192
Image Height: 466 pixels
Data Precision: 8 bits
tiff:BitsPerSample: 8
Compression Type: Baseline
Flags 1: 0
Component 1: Y component: Quantization table 0, Sampling factors 1 horiz/1 vert
Component 2: Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert
tiff:ImageLength: 466
Component 3: Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert
DCT Encode Version: 1
Color Transform: YCbCr
tiff:ImageWidth: 444
Image Width: 444 pixels

参考文档:tika