public void test() throws Exception { List images = getImagesWithExifData(300); for (int i = 0; i < images.size(); i++) { if (i % 10 == 0) Debug.purgeMemory(); File imageFile = (File) images.get(i); // Debug.debug(); // Debug.debug("imageFile", imageFile); if (imageFile.getParentFile().getName().toLowerCase().equals("@broken")) continue; try { Map params = new HashMap(); boolean ignoreImageData = isPhilHarveyTestImage(imageFile); params.put(PARAM_KEY_READ_THUMBNAILS, new Boolean(!ignoreImageData)); JpegImageMetadata metadata = (JpegImageMetadata) Sanselan.getMetadata(imageFile, params); if (null == metadata) continue; TiffImageMetadata exifMetadata = metadata.getExif(); if (null == exifMetadata) continue; TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS(); if (null == gpsInfo) continue; Debug.debug("imageFile", imageFile); Debug.debug("gpsInfo", gpsInfo); Debug.debug("gpsInfo longitude as degrees east", gpsInfo.getLongitudeAsDegreesEast()); Debug.debug("gpsInfo latitude as degrees north", gpsInfo.getLatitudeAsDegreesNorth()); Debug.debug(); } catch (Exception e) { Debug.debug("imageFile", imageFile.getAbsoluteFile()); Debug.debug("imageFile", imageFile.length()); Debug.debug(e, 13); // File brokenFolder = new File(imageFile.getParentFile(), "@Broken"); // if(!brokenFolder.exists()) // brokenFolder.mkdirs(); // File movedFile = new File(brokenFolder, imageFile.getName()); // imageFile.renameTo(movedFile); throw e; } } }
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; }