imageCropper.xhtml
ImageCropperTestBean.java
@ViewScoped
@ManagedBean
public class ImageCropperTestBean implements Serializable {
private static final long serialVersionUID = 6313123425208215609L;
private CroppedImage croppedImage;
private CroppedImage croppedImage2;
private String croppedImagePreview;
private String croppedImagePreview2;
private double aspectRatio = Double.MIN_VALUE;
private String minSize = "";
private String maxSize = "";
private String backgroundColor = "black";
private double backgroundOpacity = 0.5;
private String initialCoords = "40,0,105,115";
private boolean allowSelect = true;
private boolean allowMove = true;
private boolean allowResize = true;
public ImageCropperTestBean() {
System.out.println("ImageCropperTestBean created");
}
public boolean isAllowSelect() {
return allowSelect;
}
public void setAllowSelect(boolean allowSelect) {
this.allowSelect = allowSelect;
}
public boolean isAllowMove() {
return allowMove;
}
public void setAllowMove(boolean allowMove) {
this.allowMove = allowMove;
}
public boolean isAllowResize() {
return allowResize;
}
public void setAllowResize(boolean allowResize) {
this.allowResize = allowResize;
}
public double getAspectRatio() {
return aspectRatio;
}
public void setAspectRatio(double aspectRatio) {
this.aspectRatio = aspectRatio;
}
public String getMinSize() {
return minSize;
}
public void setMinSize(String minSize) {
this.minSize = minSize;
}
public String getMaxSize() {
return maxSize;
}
public void setMaxSize(String maxSize) {
this.maxSize = maxSize;
}
public String getBackgroundColor() {
return backgroundColor;
}
public void setBackgroundColor(String backgroundColor) {
this.backgroundColor = backgroundColor;
}
public double getBackgroundOpacity() {
return backgroundOpacity;
}
public void setBackgroundOpacity(double backgroundOpacity) {
this.backgroundOpacity = backgroundOpacity;
}
public String getInitialCoords() {
return initialCoords;
}
public void setInitialCoords(String initialCoords) {
this.initialCoords = initialCoords;
}
public CroppedImage getCroppedImage() {
return croppedImage;
}
public void setCroppedImage(CroppedImage croppedImage) {
this.croppedImage = croppedImage;
}
public CroppedImage getCroppedImage2() {
return croppedImage2;
}
public void setCroppedImage2(CroppedImage croppedImage2) {
this.croppedImage2 = croppedImage2;
}
public String getCroppedImagePreview() {
return croppedImagePreview;
}
public void setCroppedImagePreview(String croppedImagePreview) {
this.croppedImagePreview = croppedImagePreview;
}
public String getCroppedImagePreview2() {
return croppedImagePreview2;
}
public void setCroppedImagePreview2(String croppedImagePreview2) {
this.croppedImagePreview2 = croppedImagePreview2;
}
public void crop(AjaxBehaviorEvent actionEvent) {
System.out.println("Crop ajax action executed!");
CroppedImage image = (CroppedImage)
((UIImageCropper) actionEvent
.getComponent()).getValue();
String guid = UUID.randomUUID().toString();
setCroppedImagePreview(guid + ".jpg");
updateCroppedImage(image, getCroppedImagePreview());
}
public String formSubmit() {
System.out.println("Form Submit");
String guid = UUID.randomUUID().toString();
setCroppedImagePreview2(guid + ".jpg");
updateCroppedImage(croppedImage2, getCroppedImagePreview2());
return "";
}
private void updateCroppedImage(CroppedImage image,
String imageName) {
if (image == null) return;
this.initialCoords = image.getLeft() + ","
+ image.getTop() + ","
+ image.getWidth() + "," + image.getHeight();
ServletContext servletContext = (ServletContext)
FacesContext.getCurrentInstance()
.getExternalContext().getContext();
String newFileName = servletContext.getRealPath("")
+ File.separator
+ "img" + File.separator + imageName;
FileImageOutputStream imageOutput;
try {
imageOutput = new FileImageOutputStream(
new File(newFileName));
imageOutput.write(image.getBytes(), 0,
image.getBytes().length);
imageOutput.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}