Get ICC Profile information from asset | Community
Skip to main content
marcinn
Level 2
March 10, 2021
Solved

Get ICC Profile information from asset

  • March 10, 2021
  • 1 reply
  • 1323 views

Hi

 

I'm trying to get ICC PRofile information from the asset, but with no luck. Is there any way to grab those fields during asset processing inside a workflow step? 

 

I have a code like that to get an asset object (it's just to describe my problem) implemented inside a class that inherits from WorkflowProcess:

 

ResourceResolver resolver = workflowSession.adaptTo(ResourceResolver.class);

String path = workItem.getWorkflowData().getPayload().toString();
if (StringUtils.contains(path, JcrConstants.JCR_CONTENT)) {
path = StringUtils.substringBefore(path, JcrConstants.JCR_CONTENT);
}

Resource resource = resolver.getResource(path);
Asset asset = resource == null ? null : resource.adaptTo(Asset.class);

if (asset == null) {
log.info("Asset is null, skipping metadata extraction");
}

assert asset != null;
String layerName = asset.getMetadata("photoshop:LayerName") != null ? asset.getMetadata("photoshop:LayerName").toString() : "";

Map<String, Object> meta = asset.getMetadata();

 And in the last line, I can't see metadata ICC fields. 

 

Any suggestions? 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by marcinn

OK, finally resolved

 

I've used this library 

org.apache.commons.imaging 

 and then it was easy

Asset asset = resource == null ? null : resource.adaptTo(Asset.class);
Iterator<? extends Rendition> rendition = asset.listRenditions();
if (rendition.hasNext()) {
Rendition ren = rendition.next();

try {
byte[] assetByteArray = new byte[ren.getStream().available()];
ren.getStream().read(assetByteArray);

Imaging.getImageInfo(assetByteArray).getColorType();

ICC_Profile iccProfile = Imaging.getICCProfile(assetByteArray);
if (iccProfile != null) {
int cs = iccProfile.getColorSpaceType();

String colorSpaceName = getColorSpaceName(cs);

Resource assetResource = resolver.getResource(path);
Resource metadatResource = assetResource.getChild(DamConstants.METADATA_PATH);

ModifiableValueMap mvmForAsset = metadatResource.adaptTo(ModifiableValueMap.class);
mvmForAsset.put(DamConstants.ASSET_COLORSPACE, colorSpaceName);

try {
resolver.commit();
} catch (PersistenceException e) {
log.error("Error occurred during saving metadata value {}", e.getMessage());
} finally {
resolver.close();
}

 

1 reply

marcinn
marcinnAuthorAccepted solution
Level 2
March 22, 2021

OK, finally resolved

 

I've used this library 

org.apache.commons.imaging 

 and then it was easy

Asset asset = resource == null ? null : resource.adaptTo(Asset.class);
Iterator<? extends Rendition> rendition = asset.listRenditions();
if (rendition.hasNext()) {
Rendition ren = rendition.next();

try {
byte[] assetByteArray = new byte[ren.getStream().available()];
ren.getStream().read(assetByteArray);

Imaging.getImageInfo(assetByteArray).getColorType();

ICC_Profile iccProfile = Imaging.getICCProfile(assetByteArray);
if (iccProfile != null) {
int cs = iccProfile.getColorSpaceType();

String colorSpaceName = getColorSpaceName(cs);

Resource assetResource = resolver.getResource(path);
Resource metadatResource = assetResource.getChild(DamConstants.METADATA_PATH);

ModifiableValueMap mvmForAsset = metadatResource.adaptTo(ModifiableValueMap.class);
mvmForAsset.put(DamConstants.ASSET_COLORSPACE, colorSpaceName);

try {
resolver.commit();
} catch (PersistenceException e) {
log.error("Error occurred during saving metadata value {}", e.getMessage());
} finally {
resolver.close();
}

 

kautuk_sahni
Community Manager
Community Manager
March 22, 2021
@marcinn thnak you for sharing the answer in the AEM community. Keep assisting others here.
Kautuk Sahni