Search in sources :

Example 6 with ImageProcessingException

use of com.drew.imaging.ImageProcessingException in project java-mapollage by trixon.

the class PhotoInfo method getGeoLocation.

private GeoLocation getGeoLocation() throws ImageProcessingException {
    GeoLocation geoLocation = null;
    if (mIncludeNullCoordinate) {
        try {
            geoLocation = mGpsDirectory.getGeoLocation();
            if (geoLocation.isZero()) {
                geoLocation = new GeoLocation(mOptions.getDefaultLat(), mOptions.getDefaultLon());
            }
        } catch (Exception e) {
        // never error
        }
    } else {
        try {
            geoLocation = mGpsDirectory.getGeoLocation();
            // Just force a reference
            geoLocation.isZero();
        } catch (NullPointerException e) {
            throw new ImageProcessingException(String.format("E012 %s", mFile.getAbsolutePath()));
        }
    }
    if (geoLocation == null) {
        geoLocation = new GeoLocation(mOptions.getDefaultLat(), mOptions.getDefaultLon());
    }
    return geoLocation;
}
Also used : ImageProcessingException(com.drew.imaging.ImageProcessingException) GeoLocation(com.drew.lang.GeoLocation) ImageProcessingException(com.drew.imaging.ImageProcessingException) MetadataException(com.drew.metadata.MetadataException) IOException(java.io.IOException)

Example 7 with ImageProcessingException

use of com.drew.imaging.ImageProcessingException in project react-native-camera by lwansbrough.

the class MutableImage method fixOrientation.

public void fixOrientation() throws ImageMutationFailedException {
    try {
        Metadata metadata = originalImageMetaData();
        ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
        if (exifIFD0Directory == null) {
            return;
        } else if (exifIFD0Directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
            int exifOrientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
            if (exifOrientation != 1) {
                rotate(exifOrientation);
                exifIFD0Directory.setInt(ExifIFD0Directory.TAG_ORIENTATION, 1);
            }
        }
    } catch (ImageProcessingException | IOException | MetadataException e) {
        throw new ImageMutationFailedException("failed to fix orientation", e);
    }
}
Also used : ImageProcessingException(com.drew.imaging.ImageProcessingException) Metadata(com.drew.metadata.Metadata) ExifIFD0Directory(com.drew.metadata.exif.ExifIFD0Directory) IOException(java.io.IOException) MetadataException(com.drew.metadata.MetadataException)

Example 8 with ImageProcessingException

use of com.drew.imaging.ImageProcessingException in project UniversalMediaServer by UniversalMediaServer.

the class ImagesUtil method parseImage.

/**
 * Parses an image file and stores the results in the given
 * {@link DLNAMediaInfo}. Parsing is performed using both
 * <a href=https://github.com/drewnoakes/metadata-extractor>Metadata Extractor</a>
 * and {@link ImageIO}. While Metadata Extractor offers more detailed
 * information, {@link ImageIO} offers information that is convenient for
 * image transformation with {@link ImageIO}. Parsing will be performed if
 * just one of the two methods produces results, but some details will be
 * missing if either one failed.
 * <p><b>
 * This method consumes and closes {@code inputStream}.
 * </b>
 * @param file the {@link File} to parse.
 * @param media the {@link DLNAMediaInfo} instance to store the parsing
 *              results to.
 * @throws IOException if an IO error occurs or no information can be parsed.
 */
public static void parseImage(File file, DLNAMediaInfo media) throws IOException {
    // 1 MB
    final int MAX_BUFFER = 1048576;
    if (file == null) {
        throw new IllegalArgumentException("parseImage: file cannot be null");
    }
    if (media == null) {
        throw new IllegalArgumentException("parseImage: media cannot be null");
    }
    boolean trace = LOGGER.isTraceEnabled();
    if (trace) {
        LOGGER.trace("Parsing image file \"{}\"", file.getAbsolutePath());
    }
    long size = file.length();
    ResettableInputStream inputStream = new ResettableInputStream(Files.newInputStream(file.toPath()), MAX_BUFFER);
    try {
        Metadata metadata = null;
        FileType fileType = null;
        try {
            fileType = FileTypeDetector.detectFileType(inputStream);
            metadata = getMetadata(inputStream, fileType);
        } catch (IOException e) {
            metadata = new Metadata();
            LOGGER.debug("Error reading \"{}\": {}", file.getAbsolutePath(), e.getMessage());
            LOGGER.trace("", e);
        } catch (ImageProcessingException e) {
            metadata = new Metadata();
            LOGGER.debug("Error parsing {} metadata for \"{}\": {}", fileType.toString().toUpperCase(Locale.ROOT), file.getAbsolutePath(), e.getMessage());
            LOGGER.trace("", e);
        }
        ImageFormat format = ImageFormat.toImageFormat(fileType);
        if (format == null || format == ImageFormat.TIFF) {
            ImageFormat tmpformat = ImageFormat.toImageFormat(metadata);
            if (tmpformat != null) {
                format = tmpformat;
            }
        }
        if (inputStream.isFullResetAvailable()) {
            inputStream.fullReset();
        } else {
            // If we can't reset it, close it and create a new
            inputStream.close();
            inputStream = new ResettableInputStream(Files.newInputStream(file.toPath()), MAX_BUFFER);
        }
        ImageInfo imageInfo = null;
        try {
            imageInfo = ImageIOTools.readImageInfo(inputStream, size, metadata, false);
        } catch (UnknownFormatException | IIOException | ParseException e) {
            if (format == null) {
                throw new UnknownFormatException("Unable to recognize image format for \"" + file.getAbsolutePath() + "\" - parsing failed", e);
            }
            LOGGER.debug("Unable to parse \"{}\" with ImageIO because the format is unsupported, image information will be limited", file.getAbsolutePath());
            LOGGER.trace("ImageIO parse failure reason: {}", e.getMessage());
            // Gather basic information from the data we have
            if (metadata != null) {
                try {
                    imageInfo = ImageInfo.create(metadata, format, size, true, true);
                } catch (ParseException pe) {
                    LOGGER.debug("Unable to parse metadata for \"{}\": {}", file.getAbsolutePath(), pe.getMessage());
                    LOGGER.trace("", pe);
                }
            }
        }
        if (imageInfo == null && format == null) {
            throw new ParseException("Parsing of \"" + file.getAbsolutePath() + "\" failed");
        }
        if (format == null) {
            format = imageInfo.getFormat();
        } else if (imageInfo != null && imageInfo.getFormat() != null && format != imageInfo.getFormat()) {
            if (imageInfo.getFormat() == ImageFormat.TIFF && format.isRaw()) {
                if (format == ImageFormat.ARW && !isARW(metadata)) {
                    // XXX Remove this if https://github.com/drewnoakes/metadata-extractor/issues/217 is fixed
                    // Metadata extractor misidentifies some Photoshop created TIFFs for ARW, correct it
                    format = ImageFormat.toImageFormat(metadata);
                    if (format == null) {
                        format = ImageFormat.TIFF;
                    }
                    LOGGER.trace("Correcting misidentified image format ARW to {} for \"{}\"", format, file.getAbsolutePath());
                } else {
                    /*
						 * ImageIO recognizes many RAW formats as TIFF because
						 * of their close relationship let's treat them as what
						 * they really are.
						 */
                    imageInfo = ImageInfo.create(imageInfo.getWidth(), imageInfo.getHeight(), format, size, imageInfo.getBitDepth(), imageInfo.getNumComponents(), imageInfo.getColorSpace(), imageInfo.getColorSpaceType(), metadata, false, imageInfo.isImageIOSupported());
                    LOGGER.trace("Correcting misidentified image format TIFF to {} for \"{}\"", format.toString(), file.getAbsolutePath());
                }
            } else {
                LOGGER.debug("Image parsing for \"{}\" was inconclusive, metadata parsing " + "detected {} format while ImageIO detected {}. Choosing {}.", file.getAbsolutePath(), format, imageInfo.getFormat(), imageInfo.getFormat());
                format = imageInfo.getFormat();
            }
        }
        media.setImageInfo(imageInfo);
        if (format != null) {
            media.setCodecV(format.toFormatConfiguration());
            media.setContainer(format.toFormatConfiguration());
        }
        if (trace) {
            LOGGER.trace("Parsing of image \"{}\" completed", file.getName());
        }
    } finally {
        inputStream.close();
    }
}
Also used : ImageProcessingException(com.drew.imaging.ImageProcessingException) Metadata(com.drew.metadata.Metadata) IIOException(javax.imageio.IIOException) IIOException(javax.imageio.IIOException) FileType(com.drew.imaging.FileType) UnknownFormatException(net.pms.util.UnknownFormatException) ParseException(net.pms.util.ParseException) ResettableInputStream(net.pms.util.ResettableInputStream)

Example 9 with ImageProcessingException

use of com.drew.imaging.ImageProcessingException in project java-mapollage by trixon.

the class Operation method run.

@Override
public void run() {
    if (!Files.isWritable(mDestinationFile.getParentFile().toPath())) {
        mListener.onOperationLog(String.format(mBundle.getString("insufficient_privileges"), mDestinationFile.getAbsolutePath()));
        Thread.currentThread().interrupt();
        mListener.onOperationInterrupted();
        return;
    }
    mStartTime = System.currentTimeMillis();
    Date date = new Date(mStartTime);
    SimpleDateFormat dateFormat = new SimpleDateFormat();
    mListener.onOperationStarted();
    mListener.onOperationLog(new SimpleDateFormat().format(new Date()));
    String status;
    mRootFolder = mDocument.createAndAddFolder().withName(getSafeXmlString(mProfileFolder.getRootName())).withOpen(true);
    String href = "<a href=\"https://trixon.se/mapollage/\">Mapollage</a>";
    String description = String.format("<p>%s %s, %s</p>%s", Dict.MADE_WITH.toString(), href, dateFormat.format(date), mProfileFolder.getRootDescription().replaceAll("\\n", "<br />"));
    mRootFolder.setDescription(getSafeXmlString(description));
    mListener.onOperationProcessingStarted();
    try {
        mInterrupted = !generateFileList();
    } catch (IOException ex) {
        logError(ex.getMessage());
    }
    if (!mInterrupted && !mFiles.isEmpty()) {
        if (mProfilePlacemark.isSymbolAsPhoto()) {
            mThumbsDir = new File(mDestinationFile.getParent() + String.format("/%s-thumbnails", FilenameUtils.getBaseName(mDestinationFile.getAbsolutePath())));
            try {
                FileUtils.forceMkdir(mThumbsDir);
            } catch (IOException ex) {
                logError(String.format("E000 %s", ex.getMessage()));
            }
        }
        mListener.onOperationLog(String.format(mBundle.getString("found_count"), mFiles.size()));
        mListener.onOperationLog("");
        mListener.onOperationProgressInit(mFiles.size());
        for (File file : mFiles) {
            mListener.onOperationProgress(file.getAbsolutePath());
            try {
                addPhoto(file);
            } catch (ImageProcessingException ex) {
                logError(String.format("E000 %s", ex.getMessage()));
            } catch (IOException ex) {
                logError(String.format("E000 %s", file.getAbsolutePath()));
            }
            if (Thread.interrupted()) {
                mInterrupted = true;
                break;
            }
        }
        if (mProfilePath.isDrawPath() && mLineNodes.size() > 1) {
            addPath();
        }
    }
    if (mInterrupted) {
        status = Dict.TASK_ABORTED.toString();
        mListener.onOperationLog("\n" + status);
        mListener.onOperationInterrupted();
    } else if (!mFiles.isEmpty()) {
        if (mProfilePath.isDrawPolygon()) {
            addPolygons();
        }
        saveToFile();
    }
    if (mNumOfErrors > 0) {
        logError(mBundle.getString("error_description"));
    }
}
Also used : ImageProcessingException(com.drew.imaging.ImageProcessingException) LineString(de.micromata.opengis.kml.v_2_2_0.LineString) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) Date(java.util.Date)

Example 10 with ImageProcessingException

use of com.drew.imaging.ImageProcessingException in project react-native-camera by lwansbrough.

the class MutableImage method writeDataToFile.

public void writeDataToFile(File file, ReadableMap options, int jpegQualityPercent) throws IOException {
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(toJpeg(currentRepresentation, jpegQualityPercent));
    fos.close();
    try {
        ExifInterface exif = new ExifInterface(file.getAbsolutePath());
        // copy original exif data to the output exif...
        for (Directory directory : originalImageMetaData().getDirectories()) {
            for (Tag tag : directory.getTags()) {
                int tagType = tag.getTagType();
                Object object = directory.getObject(tagType);
                exif.setAttribute(tag.getTagName(), object.toString());
            }
        }
        // Add missing exif data from a sub directory
        ExifSubIFDDirectory directory = originalImageMetaData().getFirstDirectoryOfType(ExifSubIFDDirectory.class);
        for (Tag tag : directory.getTags()) {
            int tagType = tag.getTagType();
            // As some of exif data does not follow naming of the ExifInterface the names need
            // to be transformed into Upper camel case format.
            String tagName = tag.getTagName().replaceAll(" ", "");
            Object object = directory.getObject(tagType);
            if (tagName.equals(ExifInterface.TAG_EXPOSURE_TIME)) {
                exif.setAttribute(tagName, convertExposureTimeToDoubleFormat(object.toString()));
            } else {
                exif.setAttribute(tagName, object.toString());
            }
        }
        writeLocationExifData(options, exif);
        if (hasBeenReoriented)
            rewriteOrientation(exif);
        exif.saveAttributes();
    } catch (ImageProcessingException | IOException e) {
        Log.e(TAG, "failed to save exif data", e);
    }
}
Also used : ExifSubIFDDirectory(com.drew.metadata.exif.ExifSubIFDDirectory) ImageProcessingException(com.drew.imaging.ImageProcessingException) FileOutputStream(java.io.FileOutputStream) ExifInterface(androidx.exifinterface.media.ExifInterface) Tag(com.drew.metadata.Tag) IOException(java.io.IOException) ExifSubIFDDirectory(com.drew.metadata.exif.ExifSubIFDDirectory) Directory(com.drew.metadata.Directory) ExifIFD0Directory(com.drew.metadata.exif.ExifIFD0Directory)

Aggregations

ImageProcessingException (com.drew.imaging.ImageProcessingException)13 IOException (java.io.IOException)10 Metadata (com.drew.metadata.Metadata)8 ExifIFD0Directory (com.drew.metadata.exif.ExifIFD0Directory)6 MetadataException (com.drew.metadata.MetadataException)5 File (java.io.File)3 Date (java.util.Date)3 ExifInterface (androidx.exifinterface.media.ExifInterface)2 FileType (com.drew.imaging.FileType)2 Directory (com.drew.metadata.Directory)2 Tag (com.drew.metadata.Tag)2 ExifSubIFDDirectory (com.drew.metadata.exif.ExifSubIFDDirectory)2 LineString (de.micromata.opengis.kml.v_2_2_0.LineString)2 BufferedImage (java.awt.image.BufferedImage)2 FileOutputStream (java.io.FileOutputStream)2 IIOException (javax.imageio.IIOException)2 ParseException (net.pms.util.ParseException)2 UnknownFormatException (net.pms.util.UnknownFormatException)2 GeoLocation (com.drew.lang.GeoLocation)1 ArachneSystemRuntimeException (com.odysseusinc.arachne.portal.exception.ArachneSystemRuntimeException)1