Hello, I've read lots but still haven't got a way to get asset metadata specifically height x width.
I get asset data by copy pasting "file" to my cq:dialog.
I'm using Java models (as what I have read) to get asset like this.
it returns a string to my DAM asset ex.
/content/assets/myfolder/asset.jpg.
how can I get it's metadata?
Previous thread:
This example seems cool but I can't populate the image resource field.
Solved! Go to Solution.
Try this--
import javax.annotation.PostConstruct;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
import com.day.cq.dam.api.Asset;
@Model(adaptables = SlingHttpServletRequest.class) // map it to resource as applicable
public class HelloWorldModel {
@Self
protected SlingHttpServletRequest request;
@PostConstruct
protected void init() {
Resource resource = request.getResourceResolver()
.getResource("/content/dam/we-retail/en/activities/biking/cycling_1.jpg");
Asset asset = resource.adaptTo(Asset.class);
String width = asset.getMetadataValue("tiff:ImageWidth");
String height = asset.getMetadataValue("tiff:ImageLength");
// do something
}
}
Asset is created by AssetManager
and can be retrieved either via AssetManager
or by adapting a asset Resource
to an Asset
.
eg: // to create an asset AssetManager assetManager = resolver.adaptTo(AssetManager.class); Asset newAsset = assetManager.createAsset("/path/to/asset/document.pdf"); // to get an existing asset Asset asset = assetManager.getAsset("/path/to/asset/document.pdf"); // to get asset by adapting Asset asset = resource.adaptTo(Asset.class);
Asset properties can be retrieved or set by adapting it to a ValueMap.
eg: // to get Asset properties value map ValueMap map = asset.adaptTo(ValueMap.class);
You can also use DAMUtil to get asset properties
String assetPath = DamUtil.getAssetFromMetaRes(resource.getPath());
Here's my updated code
@Model(adaptables = SlingHttpServletRequest.class)
public class SlingModel {
@ValueMapValue
public String fileReference;
@Self
protected SlingHttpServletRequest request;
@PostConstruct
protected void init() {
final Resource assetResource = request.getResourceResolver().getResource(fileReference);
Asset asset = assetResource.adaptTo(Asset.class);
AssetMetadata assetMetadata = asset.getAssetMetadata();
ValueMap properties = asset.adaptTo(ValueMap.class);
//output ValueMap.
}
}
But how can I get the Metadata? this is what I'm currently getting
I want to get width and height. or if possible all JCR metadata. thanks!
Try this--
import javax.annotation.PostConstruct;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
import com.day.cq.dam.api.Asset;
@Model(adaptables = SlingHttpServletRequest.class) // map it to resource as applicable
public class HelloWorldModel {
@Self
protected SlingHttpServletRequest request;
@PostConstruct
protected void init() {
Resource resource = request.getResourceResolver()
.getResource("/content/dam/we-retail/en/activities/biking/cycling_1.jpg");
Asset asset = resource.adaptTo(Asset.class);
String width = asset.getMetadataValue("tiff:ImageWidth");
String height = asset.getMetadataValue("tiff:ImageLength");
// do something
}
}
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies