Search code examples
javaosgiaem

Get ICC Profile information from Adobe Experience Manager asset


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?


Solution

  • 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();
                }