I have encountered the following problem with cropper. I tried to crop a PNG file (downloaded from the Internet), and I got the following result:
So far, so good... Then I found the same image on the Internet, but didn't save it the usual way (Right click->Save image as), but instead I used Windows' Snipping tool to take a snapshot of it and then I saved it as a new PNG file. When I tried to crop the new file, here's what I got:
It looks kinda negative.
I also tried to open the original downloaded image, then use a snipping tool on it, and when I tried to crop the new snipped image, I got the same (bad looking) result.
So it looks to me that for some reason Cropper isn't working well with the PNGs that were made using the Snipping tool. Does anybody have an idea what could be the problem?
Btw, it works well with all the JPG files I have tried so far...
Here's my code:
imageCropper.xhtml:
Code: Select all
<p:imageCropper value="#{cropperView.croppedImage}" image="#{cropperView.imgPath}" initialCoords="0,0,100,100" />
<p:graphicImage id="cropped" name="img/crop/#{cropperView.newImageName}.jpg" />
<p:commandButton value="Crop" action="#{cropperView.crop}" update="cropped msgs" icon="ui-icon-scissors"/>
Code: Select all
public void crop() {
if (croppedImage == null) {
return;
}
setNewImageName(getRandomImageName());
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String newFileName = servletContext.getRealPath("") + File.separator + "resources" + File.separator + "img"
+ File.separator + "crop" + File.separator + getNewImageName() + ".jpg";
FileImageOutputStream imageOutput;
try {
imageOutput = new FileImageOutputStream(new File(newFileName));
imageOutput.write(croppedImage.getBytes(), 0, croppedImage.getBytes().length);
imageOutput.close();
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Cropping failed."));
}
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success", "Cropping finished."));
}