Code: Select all
<p:growl id="msgs" showDetail="true"/>
<h:form>
<h:outputText value="Crop Image" styleClass="labelText" style="margin-left: 10px;"/><br/>
<table cellspacing="10">
<tr>
<td>
<p:imageCropper value="#{CropImage.croppedImage}" image="#{CropImage.me.profilePic}"
initialCoords="225,75,300" aspectRatio="1.25"/>
</td>
<td style="vertical-align: top;">
<h:outputText value="My Thumb Nail" styleClass="labelText"/><br/>
<p:graphicImage value="#{CropImage.imageName}" styleClass="icon"/><br/><br/>
<p:commandButton value="Crop" actionListener="#{CropImage.crop}" update="msgs"/>
</td>
</tr>
</table>
</h:form>
Code: Select all
@ManagedBean(name="CropImage")
@ViewScoped
public class CropImage {
@ManagedProperty(value="#{SessionBean}")
private SessionBean sessionBean;
private User me;
private CroppedImage croppedImage;
private String imageName;
private String ext;
private static final Logger logger = Logger.getLogger(CropImage.class.getName());
public CropImage() {
}
@PostConstruct
public void init(){
me = sessionBean.getMe();
imageName = me.getProfilePic();
//obtain the extension
ext = imageName.substring(imageName.lastIndexOf("."), imageName.length());
}
public String getImageName() {
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
public SessionBean getSessionBean() {
return sessionBean;
}
public void setSessionBean(SessionBean sessionBean) {
this.sessionBean = sessionBean;
}
public User getMe() {
return me;
}
public void setMe(User me) {
this.me = me;
}
public CroppedImage getCroppedImage() {
return croppedImage;
}
public void setCroppedImage(CroppedImage croppedImage) {
this.croppedImage = croppedImage;
}
public String crop(){
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
UUID uuid = UUID.randomUUID();
imageName = servletContext.getInitParameter("resources") + File.separator;
imageName += "cropped" + File.separator + uuid.toString() + ext;
FileImageOutputStream imageOutput;
try {
imageOutput = new FileImageOutputStream(new File(imageName));
imageOutput.write(croppedImage.getBytes(), 0, croppedImage.getBytes().length);
imageOutput.close();
} catch (FileNotFoundException e) {
logger.log(Level.SEVERE, e.getMessage());
} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage());
}
return null;
}
}