How to check if image has clipping path in java servlet | Community
Skip to main content
Level 2
July 13, 2023
Question

How to check if image has clipping path in java servlet

  • July 13, 2023
  • 2 replies
  • 1799 views

I tried to get this info from image directly with twelvemonkeys libraries but it crashed my bundle. Can I get this info from metadata? Thanks.

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

2 replies

sarahxxu
Adobe Employee
Adobe Employee
July 13, 2023

Hi @jam2021 

 

Which adobe product are you working with?

Jam2021Author
Level 2
July 13, 2023

AEM 6.5

sarahxxu
Adobe Employee
Adobe Employee
July 14, 2023

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/ct-p/adobe-experience-manager-community might be a better spot for this question as thats where the aem experts are hanging out! 

partyush
Community Advisor
Community Advisor
August 13, 2023

Hi @jam2021 

 

Try this solution I hope this information will help you ! 

1. Add Dependency: Add the Apache Sanselan library to your project's dependencies. If you're using Maven, add this to your pom.xml:

 

 

<dependency> <groupId>org.apache.sanselan</groupId> <artifactId>sanselan</artifactId> <version>0.98</version> <!-- Use the latest version --> </dependency>

 

 

2. Implement Servlet: Create a servlet to check if an image has a clipping path in its metadata. Replace /path/to/your/image.jpg with the actual path to your image.

 

 

import org.apache.sanselan.ImageInfo; import org.apache.sanselan.Sanselan; import org.apache.sanselan.formats.tiff.constants.TiffTagConstants; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/checkclippingpath") public class CheckClippingPathServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); try (InputStream imageInputStream = getServletContext().getResourceAsStream("/path/to/your/image.jpg")) { ImageInfo imageInfo = Sanselan.getImageInfo(imageInputStream, null); if (imageInfo != null && imageInfo.getComments() != null) { boolean hasClippingPath = false; for (String comment : imageInfo.getComments()) { if (comment.contains("Clipping Path")) { hasClippingPath = true; break; } } if (hasClippingPath) { response.getWriter().println("Image has clipping path."); } else { response.getWriter().println("Image does not have clipping path."); } } else { response.getWriter().println("Image format not supported or metadata not available."); } } catch (Exception e) { e.printStackTrace(); response.getWriter().println("An error occurred: " + e.getMessage()); } } }

 

 

This servlet reads the image metadata using Apache Sanselan's ImageInfo class and checks for comments that indicate the presence of a clipping path.

3. Configure Web Deployment: Make sure your servlet container (e.g., Apache Tomcat) is set up correctly to deploy and run your servlet.

4. Access the Servlet: Access the servlet in your web browser using the URL mapped to the servlet, like http://localhost:8080/your-web-app-context/checkclippingpath.

 

Thanks let me know if this works for you ! 

Jam2021Author
Level 2
August 17, 2023

Thank you very much for the detailed information. I tried but got error for maven library:

org.eclipse.aether.resolution.ArtifactDescriptorException: Failed to read artifact descriptor for org.apache.sanselan:sanselan:jar:0.98.

 

It may need to change maven settings or version. I will try to fix the issue. Thanks