Esempio n. 1
0
 private void readInfo() {
   try {
     IImageMetadata metadata = Sanselan.getMetadata(source);
     if (metadata instanceof JpegImageMetadata) {
       JpegImageMetadata jpegMeta = (JpegImageMetadata) metadata;
       TiffField exifDate = jpegMeta.findEXIFValue(TiffConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
       if (exifDate != null) {
         String date = (String) exifDate.getValue();
         dateTaken = parseExifDate(date);
         successful = true;
       }
     }
   } catch (ImageReadException e) {
   } catch (IOException e) {
     throw new RuntimeException("IO Error while getting metadata: " + e.getMessage(), e);
   }
 }
 @Override
 public Document handleDocument(IndexWriter indexWriter, IndexState state, File file)
     throws Exception {
   Document doc = super.handleDocument(indexWriter, state, file);
   /* create/update document */
   if (doc != null && file.length() > 0) {
     /* ultra fast header analysis, with fileinputstream - too many filedescripor
      * exeption on samba network */
     try {
       ImageInfo imageInfo = Sanselan.getImageInfo(file);
       /* default image meta informations */
       int w = imageInfo.getWidth();
       int h = imageInfo.getHeight();
       /* image width */
       get(ImageWidthField.NAME).add(doc, w);
       /* image height */
       get(ImageHeightField.NAME).add(doc, h);
       /* bits per pixel */
       get(BitsPerPixelField.NAME).add(doc, imageInfo.getBitsPerPixel());
       /* complete size */
       get(PixelSizeField.NAME).add(doc, w * h);
       /* image aspect ratio */
       get(AspectRatioField.NAME).add(doc, w / (float) h);
       /* try to extract metadata from jpeg files */
       String mime = imageInfo.getMimeType();
       /* override because they are different content types than extensions */
       doc.removeFields(MimeTypeField.NAME);
       get(MimeTypeField.NAME).add(doc, mime);
       /* special jpeg handling */
       if (mime != null && mime.equals("image/jpeg")) {
         IImageMetadata metadata = Sanselan.getMetadata(file);
         if (metadata instanceof JpegImageMetadata) {
           JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
           /* create date of the image */
           TiffField field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_CREATE_DATE);
           if (field != null) {
             Date date = DateUtil.parseEXIFFormat(field.getStringValue());
             /* else unknown format */
             if (date != null) {
               /* override file last modified date */
               doc.removeFields(LastModifiedField.NAME);
               get(LastModifiedField.NAME).add(doc, date.getTime());
             }
           }
           /* user comment tag */
           field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_USER_COMMENT);
           if (field != null) {
             get(CommentField.NAME).add(doc, field.getStringValue().trim());
           }
           /* make tag */
           field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_MAKE);
           if (field != null) {
             get(ExifMakeField.NAME).add(doc, field.getStringValue().trim());
           }
           /* model tag */
           field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_MODEL);
           if (field != null) {
             get(ExifModelField.NAME).add(doc, field.getStringValue().trim());
           }
           /* date time tag */
           field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_CREATE_DATE);
           if (field != null) {
             Date date = DateUtil.parseEXIFFormat(field.getStringValue());
             /* else unknown format */
             if (date != null) {
               get(ExifDateField.NAME).add(doc, date.getTime());
             }
           }
           /* try to find gps informations */
           TiffImageMetadata exifMetadata = jpegMetadata.getExif();
           if (exifMetadata != null) {
             try {
               TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS();
               if (null != gpsInfo) {
                 get(LatField.NAME).add(doc, gpsInfo.getLatitudeAsDegreesNorth());
                 get(LonField.NAME).add(doc, gpsInfo.getLongitudeAsDegreesEast());
                 //                                    doc.add(new
                 // NumericField(FIELD_EXIF_GPS_IMG_DIRECTION, Field.Store.YES,
                 //                                    true).setIntValue(gps.getRational(
                 //
                 // GpsDirectory.TAG_GPS_IMG_DIRECTION).intValue()));
                 //
                 // System.out.println(exifMetadata.findField(GPSTagConstants.GPS_TAG_GPS_IMG_DIRECTION));
               }
             } catch (ImageReadException e) {
             }
           }
         }
       }
       /* run some classification */
       if (classifyImage) {
         int[] v = ImageUtil.analyzeRGB(file);
         for (int i = 0; i < v.length; i++) {
           doc.add(new IntPoint(COLORMEAN + i, v[i]));
         }
       }
     } catch (Throwable e) {
       /* only log */
       log.log(Level.INFO, "can not extract image informations!", e);
     }
   }
   return doc;
 }