use of com.drew.imaging.ImageProcessingException in project react-native-camera by lwansbrough.
the class RCTCameraModule method fixOrientation.
private byte[] fixOrientation(byte[] data) {
final Metadata metadata;
try {
metadata = ImageMetadataReader.readMetadata(new BufferedInputStream(new ByteArrayInputStream(data)), data.length);
final ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
if (exifIFD0Directory == null) {
return data;
} else if (exifIFD0Directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
final int exifOrientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
return rotate(data, exifOrientation);
}
return data;
} catch (IOException | ImageProcessingException | MetadataException e) {
e.printStackTrace();
return data;
}
}
use of com.drew.imaging.ImageProcessingException in project nifi by apache.
the class ExtractImageMetadata method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile flowfile = session.get();
if (flowfile == null) {
return;
}
final ComponentLog logger = this.getLogger();
final AtomicReference<Metadata> value = new AtomicReference<>(null);
final Integer max = context.getProperty(MAX_NUMBER_OF_ATTRIBUTES).asInteger();
try {
session.read(flowfile, new InputStreamCallback() {
@Override
public void process(InputStream in) throws IOException {
try {
Metadata imageMetadata = ImageMetadataReader.readMetadata(in);
value.set(imageMetadata);
} catch (ImageProcessingException ex) {
throw new ProcessException(ex);
}
}
});
Metadata metadata = value.get();
Map<String, String> results = getTags(max, metadata);
// Write the results to an attribute
if (!results.isEmpty()) {
flowfile = session.putAllAttributes(flowfile, results);
}
session.transfer(flowfile, SUCCESS);
} catch (ProcessException e) {
logger.error("Failed to extract image metadata from {} due to {}", new Object[] { flowfile, e });
session.transfer(flowfile, FAILURE);
}
}
use of com.drew.imaging.ImageProcessingException in project UniversalMediaServer by UniversalMediaServer.
the class ImagesUtil method transcodeImage.
/**
* Converts and scales an image in one operation. Scaling can be with or
* without padding. Preserves aspect ratio and rotates/flips the image
* according to Exif orientation. Format support is limited to that of
* {@link ImageIO}. Only one of the three input arguments may be used in any
* given call. Note that {@code outputProfile} overrides
* {@code outputFormat}.
* <p>
* <b> This method consumes and closes {@code inputStream}. </b>
*
* @param inputByteArray the source image in a supported format.
* @param inputImage the source {@link Image}.
* @param inputStream the source image in a supported format.
* @param width the new width or 0 to disable scaling.
* @param height the new height or 0 to disable scaling.
* @param scaleType the {@link ScaleType} to use when scaling.
* @param outputFormat the {@link ImageFormat} to convert to or
* {@link ImageFormat#SOURCE} to preserve source format.
* Overridden by {@code outputProfile}.
* @param outputProfile the {@link DLNAImageProfile} to convert to. This
* overrides {@code outputFormat}.
* @param dlnaCompliant whether or not the output image should be restricted
* to DLNA compliance. This also means that the output can be
* safely cast to {@link DLNAImage}.
* @param dlnaThumbnail whether or not the output image should be restricted
* to DLNA thumbnail compliance. This also means that the output
* can be safely cast to {@link DLNAThumbnail}.
* @param padToSize whether padding should be used if source aspect doesn't
* match target aspect.
* @return The scaled and/or converted image or {@code null} if the source
* is {@code null}.
* @throws IOException if the operation fails.
*/
protected static Image transcodeImage(byte[] inputByteArray, Image inputImage, InputStream inputStream, int width, int height, ScaleType scaleType, ImageFormat outputFormat, DLNAImageProfile outputProfile, boolean dlnaCompliant, boolean dlnaThumbnail, boolean padToSize) throws IOException {
if (inputByteArray == null && inputStream == null && inputImage == null) {
return null;
}
if ((inputByteArray != null & inputImage != null) || (inputByteArray != null & inputStream != null) || (inputImage != null & inputStream != null)) {
throw new IllegalArgumentException("Use either inputByteArray, inputImage or inputStream");
}
boolean trace = LOGGER.isTraceEnabled();
if (trace) {
StringBuilder sb = new StringBuilder();
if (scaleType != null) {
sb.append("ScaleType = ").append(scaleType);
}
if (width > 0 && height > 0) {
if (sb.length() > 0) {
sb.append(", ");
}
sb.append("Width = ").append(width).append(", Height = ").append(height);
}
if (sb.length() > 0) {
sb.append(", ");
}
sb.append("PadToSize = ").append(padToSize ? "True" : "False");
LOGGER.trace("Converting {} image source to {} format and type {} using the following parameters: {}", inputByteArray != null ? "byte array" : inputImage != null ? "Image" : "input stream", outputProfile != null ? outputProfile : outputFormat, dlnaThumbnail ? "DLNAThumbnail" : dlnaCompliant ? "DLNAImage" : "Image", sb);
}
ImageIO.setUseCache(false);
dlnaCompliant = dlnaCompliant || dlnaThumbnail;
if (inputImage != null) {
inputByteArray = inputImage.getBytes(false);
} else if (inputStream != null) {
inputByteArray = ImagesUtil.toByteArray(inputStream);
}
// outputProfile overrides outputFormat
if (outputProfile != null) {
if (dlnaThumbnail && outputProfile.equals(DLNAImageProfile.GIF_LRG)) {
outputProfile = DLNAImageProfile.JPEG_LRG;
}
// Default to correct ScaleType for the profile
if (scaleType == null) {
if (DLNAImageProfile.JPEG_RES_H_V.equals(outputProfile)) {
scaleType = ScaleType.EXACT;
} else {
scaleType = ScaleType.MAX;
}
}
outputFormat = ImageFormat.toImageFormat(outputProfile);
width = width > 0 ? width : outputProfile.getMaxWidth();
height = height > 0 ? height : outputProfile.getMaxHeight();
} else if (scaleType == null) {
scaleType = ScaleType.MAX;
}
ImageReaderResult inputResult;
try {
inputResult = ImageIOTools.read(new ByteArrayInputStream(inputByteArray));
} catch (IIOException e) {
throw new UnknownFormatException("Unable to read image format", e);
}
if (inputResult.bufferedImage == null || inputResult.imageFormat == null) {
// ImageIO doesn't support the image format
throw new UnknownFormatException("Failed to transform image because the source format is unknown");
}
if (outputFormat == null || outputFormat == ImageFormat.SOURCE) {
outputFormat = inputResult.imageFormat;
}
BufferedImage bufferedImage = inputResult.bufferedImage;
boolean reencode = false;
if (outputProfile == null && dlnaCompliant) {
// if the source image has alpha and JPEG if not.
switch(outputFormat) {
case GIF:
if (dlnaThumbnail) {
outputFormat = ImageFormat.JPEG;
}
break;
case JPEG:
case PNG:
break;
default:
if (bufferedImage.getColorModel().hasAlpha()) {
outputFormat = ImageFormat.PNG;
} else {
outputFormat = ImageFormat.JPEG;
}
}
}
Metadata metadata = null;
ExifOrientation orientation;
if (inputImage != null && inputImage.getImageInfo() != null) {
orientation = inputImage.getImageInfo().getExifOrientation();
} else {
try {
metadata = getMetadata(inputByteArray, inputResult.imageFormat);
} catch (IOException | ImageProcessingException e) {
LOGGER.error("Failed to read input image metadata: {}", e.getMessage());
LOGGER.trace("", e);
metadata = new Metadata();
}
if (metadata == null) {
metadata = new Metadata();
}
orientation = parseExifOrientation(metadata);
}
if (orientation != ExifOrientation.TOP_LEFT) {
// Rotate the image before doing all the other checks
BufferedImage oldBufferedImage = bufferedImage;
bufferedImage = Thumbnails.of(bufferedImage).scale(1.0d).addFilter(ExifFilterUtils.getFilterForOrientation(orientation.getThumbnailatorOrientation())).asBufferedImage();
oldBufferedImage.flush();
// Re-parse the metadata after rotation as these are newly generated.
ByteArrayOutputStream tmpOutputStream = new ByteArrayOutputStream(inputByteArray.length);
Thumbnails.of(bufferedImage).scale(1.0d).outputFormat(outputFormat.toString()).toOutputStream(tmpOutputStream);
try {
metadata = getMetadata(tmpOutputStream.toByteArray(), outputFormat);
} catch (IOException | ImageProcessingException e) {
LOGGER.debug("Failed to read rotated image metadata: {}", e.getMessage());
LOGGER.trace("", e);
metadata = new Metadata();
}
if (metadata == null) {
metadata = new Metadata();
}
reencode = true;
}
if (outputProfile == null && dlnaCompliant) {
// Set a suitable output profile.
if (width < 1 || height < 1) {
outputProfile = DLNAImageProfile.getClosestDLNAProfile(bufferedImage.getWidth(), bufferedImage.getHeight(), outputFormat, true);
width = outputProfile.getMaxWidth();
height = outputProfile.getMaxHeight();
} else {
outputProfile = DLNAImageProfile.getClosestDLNAProfile(calculateScaledResolution(bufferedImage.getWidth(), bufferedImage.getHeight(), scaleType, width, height), outputFormat, true);
width = Math.min(width, outputProfile.getMaxWidth());
height = Math.min(height, outputProfile.getMaxHeight());
}
if (DLNAImageProfile.JPEG_RES_H_V.equals(outputProfile)) {
scaleType = ScaleType.EXACT;
}
}
boolean convertColors = bufferedImage.getType() == BufferedImageType.TYPE_CUSTOM.getTypeId() || bufferedImage.getType() == BufferedImageType.TYPE_BYTE_BINARY.getTypeId() || bufferedImage.getColorModel().getColorSpace().getType() != ColorSpaceType.TYPE_RGB.getTypeId();
// Impose DLNA format restrictions
if (!reencode && outputFormat == inputResult.imageFormat && outputProfile != null) {
DLNAComplianceResult complianceResult;
switch(outputFormat) {
case GIF:
case JPEG:
case PNG:
ImageInfo imageInfo;
// metadata is only null at this stage if inputImage != null and no rotation was necessary
if (metadata == null) {
imageInfo = inputImage.getImageInfo();
}
imageInfo = ImageInfo.create(bufferedImage.getWidth(), bufferedImage.getHeight(), inputResult.imageFormat, ImageInfo.SIZE_UNKNOWN, bufferedImage.getColorModel(), metadata, false, true);
complianceResult = DLNAImageProfile.checkCompliance(imageInfo, outputProfile);
break;
default:
throw new IllegalStateException("Unexpected image format: " + outputFormat);
}
reencode = reencode || convertColors || !complianceResult.isFormatCorrect() || !complianceResult.isColorsCorrect();
;
if (!complianceResult.isResolutionCorrect()) {
width = width > 0 && complianceResult.getMaxWidth() > 0 ? Math.min(width, complianceResult.getMaxWidth()) : width > 0 ? width : complianceResult.getMaxWidth();
height = height > 0 && complianceResult.getMaxHeight() > 0 ? Math.min(height, complianceResult.getMaxHeight()) : height > 0 ? height : complianceResult.getMaxHeight();
}
if (trace) {
if (complianceResult.isAllCorrect()) {
LOGGER.trace("Image conversion DLNA compliance check: The source image is compliant");
} else {
LOGGER.trace("Image conversion DLNA compliance check for {}: " + "The source image colors are {}, format is {} and resolution ({} x {}) is {}.\nFailures:\n {}", outputProfile, complianceResult.isColorsCorrect() ? "compliant" : "non-compliant", complianceResult.isFormatCorrect() ? "compliant" : "non-compliant", bufferedImage.getWidth(), bufferedImage.getHeight(), complianceResult.isResolutionCorrect() ? "compliant" : "non-compliant", StringUtils.join(complianceResult.getFailures(), "\n "));
}
}
}
if (convertColors) {
// Preserve alpha channel if the output format supports it
BufferedImageType outputImageType;
if ((outputFormat == ImageFormat.PNG || outputFormat == ImageFormat.PSD) && bufferedImage.getColorModel().getNumComponents() == 4) {
outputImageType = bufferedImage.isAlphaPremultiplied() ? BufferedImageType.TYPE_4BYTE_ABGR_PRE : BufferedImageType.TYPE_4BYTE_ABGR;
} else {
outputImageType = BufferedImageType.TYPE_3BYTE_BGR;
}
BufferedImage convertedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), outputImageType.getTypeId());
ColorConvertOp colorConvertOp = new ColorConvertOp(null);
colorConvertOp.filter(bufferedImage, convertedImage);
bufferedImage.flush();
bufferedImage = convertedImage;
reencode = true;
}
if (width < 1 || height < 1 || (scaleType == ScaleType.MAX && bufferedImage.getWidth() <= width && bufferedImage.getHeight() <= height) || (scaleType == ScaleType.EXACT && bufferedImage.getWidth() == width && bufferedImage.getHeight() == height)) {
// No resize, just convert
if (!reencode && inputResult.imageFormat == outputFormat) {
// Nothing to do, just return source
// metadata is only null at this stage if inputImage != null
Image result;
if (dlnaThumbnail) {
result = metadata == null ? new DLNAThumbnail(inputImage, outputProfile, false) : new DLNAThumbnail(inputByteArray, outputFormat, bufferedImage, metadata, outputProfile, false);
} else if (dlnaCompliant) {
result = metadata == null ? new DLNAImage(inputImage, outputProfile, false) : new DLNAImage(inputByteArray, outputFormat, bufferedImage, metadata, outputProfile, false);
} else {
result = metadata == null ? new Image(inputImage, false) : new Image(inputByteArray, outputFormat, bufferedImage, metadata, false);
}
bufferedImage.flush();
if (trace) {
LOGGER.trace("No conversion is needed, returning source image with width = {}, height = {} and output {}.", bufferedImage.getWidth(), bufferedImage.getHeight(), dlnaCompliant && outputProfile != null ? "profile: " + outputProfile : "format: " + outputFormat);
}
return result;
} else if (!reencode) {
// Convert format
reencode = true;
}
} else {
boolean force = DLNAImageProfile.JPEG_RES_H_V.equals(outputProfile);
BufferedImage oldBufferedImage = bufferedImage;
if (padToSize && force) {
bufferedImage = Thumbnails.of(bufferedImage).forceSize(width, height).addFilter(new Canvas(width, height, Positions.CENTER, Color.BLACK)).asBufferedImage();
} else if (padToSize) {
bufferedImage = Thumbnails.of(bufferedImage).size(width, height).addFilter(new Canvas(width, height, Positions.CENTER, Color.BLACK)).asBufferedImage();
} else if (force) {
bufferedImage = Thumbnails.of(bufferedImage).forceSize(width, height).asBufferedImage();
} else {
bufferedImage = Thumbnails.of(bufferedImage).size(width, height).asBufferedImage();
}
oldBufferedImage.flush();
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Thumbnails.of(bufferedImage).scale(1.0d).outputFormat(outputFormat.toString()).outputQuality(1.0f).toOutputStream(outputStream);
byte[] outputByteArray = outputStream.toByteArray();
Image result;
if (dlnaThumbnail) {
result = new DLNAThumbnail(outputByteArray, bufferedImage.getWidth(), bufferedImage.getHeight(), outputFormat, null, null, outputProfile, false);
} else if (dlnaCompliant) {
result = new DLNAImage(outputByteArray, bufferedImage.getWidth(), bufferedImage.getHeight(), outputFormat, null, null, outputProfile, false);
} else {
result = new Image(outputByteArray, bufferedImage.getWidth(), bufferedImage.getHeight(), outputFormat, null, null, true, false);
}
if (trace) {
StringBuilder sb = new StringBuilder();
sb.append("Convert colors = ").append(convertColors ? "True" : "False").append(", Re-encode = ").append(reencode ? "True" : "False");
LOGGER.trace("Finished converting {} {} image{}. Output image resolution: {}, {}. Flags: {}", inputResult.width + "×" + inputResult.height, inputResult.imageFormat, orientation != ExifOrientation.TOP_LEFT ? " with orientation " + orientation : "", bufferedImage.getWidth() + "×" + bufferedImage.getHeight(), dlnaCompliant && outputProfile != null ? "profile: " + outputProfile : "format: " + outputFormat, sb);
}
bufferedImage.flush();
return result;
}
use of com.drew.imaging.ImageProcessingException in project UniversalMediaServer by UniversalMediaServer.
the class RAW method parse.
@Override
public void parse(DLNAMediaInfo media, InputFile file, int type, RendererConfiguration renderer) {
boolean trace = LOGGER.isTraceEnabled();
if (media == null || file == null || file.getFile() == null) {
// Parsing is impossible
if (trace) {
if (file != null && file.getFile() != null) {
LOGGER.trace("Not parsing RAW file \"{}\" because media is null", file.getFile().getName());
} else {
LOGGER.error("Not parsing RAW file because file is null");
}
}
return;
}
PmsConfiguration configuration = PMS.getConfiguration(renderer);
try {
// Only parse using DCRaw if it is enabled
DCRaw dcraw = (DCRaw) PlayerFactory.getEnabledPlayer(DCRaw.class, this);
if (dcraw != null) {
if (trace) {
LOGGER.trace("Parsing RAW image \"{}\" with DCRaw", file.getFile().getName());
}
dcraw.parse(media, file.getFile());
media.setCodecV(FormatConfiguration.RAW);
media.setContainer(FormatConfiguration.RAW);
ImageInfo imageInfo = null;
Metadata metadata = null;
FileType fileType = null;
try (BufferedInputStream inputStream = new BufferedInputStream(Files.newInputStream(file.getFile().toPath()))) {
fileType = FileTypeDetector.detectFileType(inputStream);
metadata = ImagesUtil.getMetadata(inputStream, fileType);
} catch (IOException e) {
metadata = new Metadata();
LOGGER.debug("Error reading \"{}\": {}", file.getFile().getAbsolutePath(), e.getMessage());
LOGGER.trace("", e);
} catch (ImageProcessingException e) {
metadata = new Metadata();
LOGGER.debug("Error parsing {} metadata for \"{}\": {}", fileType.toString().toUpperCase(Locale.ROOT), file.getFile().getAbsolutePath(), e.getMessage());
LOGGER.trace("", e);
}
if (fileType == FileType.Arw && !ImagesUtil.isARW(metadata)) {
fileType = FileType.Tiff;
}
ImageFormat format = ImageFormat.toImageFormat(fileType);
if (format == null || format == ImageFormat.TIFF) {
format = ImageFormat.toImageFormat(metadata);
if (format == null || format == ImageFormat.TIFF) {
format = ImageFormat.RAW;
}
}
try {
imageInfo = ImageInfo.create(media.getWidth(), media.getHeight(), metadata, format, file.getSize(), true, false);
if (trace) {
LOGGER.trace("Parsing of RAW image \"{}\" completed: {}", file.getFile().getName(), imageInfo);
}
} catch (ParseException e) {
LOGGER.warn("Unable to parse \"{}\": {}", file.getFile().getAbsolutePath(), e.getMessage());
LOGGER.trace("", e);
}
media.setImageInfo(imageInfo);
if (media.getWidth() > 0 && media.getHeight() > 0 && configuration.getImageThumbnailsEnabled()) {
byte[] image = new DCRaw().getThumbnail(null, file.getFile().getAbsolutePath(), imageInfo);
media.setThumb(DLNAThumbnail.toThumbnail(image, 320, 320, ScaleType.MAX, ImageFormat.JPEG, false));
}
} else {
if (trace) {
LOGGER.trace("Parsing RAW image \"{}\" as a regular image because DCRaw is disabled", file.getFile().getName());
}
ImagesUtil.parseImage(file.getFile(), media);
}
media.setSize(file.getSize());
media.setImageCount(1);
media.postParse(type, file);
media.setMediaparsed(true);
} catch (IOException e) {
LOGGER.error("Error parsing RAW file \"{}\": {}", file.getFile().getAbsolutePath(), e.getMessage());
LOGGER.trace("", e);
}
}
use of com.drew.imaging.ImageProcessingException in project java-mapollage by trixon.
the class Operation method addPhoto.
private void addPhoto(File file) throws ImageProcessingException, IOException {
mPhotoInfo = new PhotoInfo(file, mProfileSource.isIncludeNullCoordinate());
try {
mPhotoInfo.init();
} catch (ImageProcessingException | IOException e) {
if (mPhotoInfo.hasExif()) {
mNumOfExif++;
}
throw e;
}
boolean hasLocation = false;
if (mPhotoInfo.hasExif()) {
mNumOfExif++;
hasLocation = mPhotoInfo.hasGps() && !mPhotoInfo.isZeroCoordinate();
if (hasLocation) {
mNumOfGps++;
}
} else {
throw new ImageProcessingException(String.format("E010 %s", file.getAbsolutePath()));
}
Date exifDate = mPhotoInfo.getDate();
if (hasLocation && mProfilePath.isDrawPath()) {
mLineNodes.add(new LineNode(exifDate, mPhotoInfo.getLat(), mPhotoInfo.getLon()));
}
if (hasLocation || mProfileSource.isIncludeNullCoordinate()) {
Folder folder = getFolder(file, exifDate);
String imageId = String.format("%08x", FileUtils.checksumCRC32(file));
String styleNormalId = String.format("s_%s", imageId);
String styleHighlightId = String.format("s_%s_hl", imageId);
String styleMapId = String.format("m_%s", imageId);
Style normalStyle = mDocument.createAndAddStyle().withId(styleNormalId);
IconStyle normalIconStyle = normalStyle.createAndSetIconStyle().withScale(1.0);
Style highlightStyle = mDocument.createAndAddStyle().withBalloonStyle(mBalloonStyle).withId(styleHighlightId);
IconStyle highlightIconStyle = highlightStyle.createAndSetIconStyle().withScale(1.1);
if (mProfilePlacemark.isSymbolAsPhoto()) {
Icon icon = KmlFactory.createIcon().withHref(String.format("%s/%s.jpg", mThumbsDir.getName(), imageId));
normalIconStyle.setIcon(icon);
normalIconStyle.setScale(mProfilePlacemark.getScale());
double highlightZoom = mProfilePlacemark.getZoom() * mProfilePlacemark.getScale();
highlightIconStyle.setIcon(icon);
highlightIconStyle.setScale(highlightZoom);
}
if (mProfilePlacemark.isSymbolAsPhoto() || mProfilePhoto.getReference() == ProfilePhoto.Reference.THUMBNAIL) {
File thumbFile = new File(mThumbsDir, imageId + ".jpg");
mFileThumbMap.put(file, thumbFile);
if (Files.isWritable(thumbFile.getParentFile().toPath())) {
mPhotoInfo.createThumbnail(thumbFile);
} else {
mListener.onOperationLog(String.format(mBundle.getString("insufficient_privileges"), mDestinationFile.getAbsolutePath()));
Thread.currentThread().interrupt();
return;
}
}
mDocument.createAndAddStyleMap().withId(styleMapId).addToPair(KmlFactory.createPair().withKey(StyleState.NORMAL).withStyleUrl("#" + styleNormalId)).addToPair(KmlFactory.createPair().withKey(StyleState.HIGHLIGHT).withStyleUrl("#" + styleHighlightId));
Placemark placemark = KmlFactory.createPlacemark().withName(getSafeXmlString(getPlacemarkName(file, exifDate))).withOpen(Boolean.TRUE).withStyleUrl("#" + styleMapId);
String desc = getPlacemarkDescription(file, mPhotoInfo, exifDate);
if (!StringUtils.isBlank(desc)) {
placemark.setDescription(desc);
}
placemark.createAndSetPoint().addToCoordinates(mPhotoInfo.getLon(), mPhotoInfo.getLat(), 0F);
if (mProfilePlacemark.isTimestamp()) {
TimeStamp timeStamp = KmlFactory.createTimeStamp();
timeStamp.setWhen(mTimeStampDateFormat.format(exifDate));
placemark.setTimePrimitive(timeStamp);
}
folder.addToFeature(placemark);
mNumOfPlacemarks++;
}
mListener.onOperationLog(file.getAbsolutePath());
}
Aggregations