Search code examples
javajqueryspringjcrop

Cropping image using jcrop and java


I am trying to crop the image using JQuery Jcrop plugin with Java Stuff, but I am unable to get the correct result.

Jsp code:

<script type="text/javascript">
$(function () {
    $('#actualImage').Jcrop({
     setSelect: [0, 0, 268, 180],
     addClass: 'custom',
     bgColor: 'yellow',
     bgOpacity: .8,
     //sideHandles: true
     allowResize: false,
     allowSelect: false,
     onSelect: storeCoords
 });
});

function storeCoords(c) {

 jQuery('#X1').val(c.x);
 jQuery('#Y1').val(c.y); 
 jQuery('#X2').val(c.x2);
 jQuery('#Y2').val(c.y2); 
 jQuery('#W').val(c.w);
 jQuery('#H').val(c.h);
}

function cropPicture(){
    $.ajax({
        url: "cropPhoto.htm",
        type: "POST", 
        data :{
            x : $('input#X1').val(),
            y : $('input#Y1').val(),
            x2 : $('input#X2').val(),
            y2 : $('input#Y2').val(), 
            w : $('input#W').val(),
            h : $('input#H').val(),
            imageName : $('input#imageName').val()
        },
        success: function (data) { 
            window.location = 'photo.htm';
         }
}
</script>

Java code:

@RequestMapping(value = "cropPhoto.htm", method = RequestMethod.POST)
    public String cropPhoto(HttpServletRequest request,HttpServletResponse response,
            HttpSession session) throws IOException{
         int x1=Integer.parseInt(request.getParameter("x"));
         int y1=Integer.parseInt(request.getParameter("y"));
         int x2=Integer.parseInt(request.getParameter("x2"));
         int y2=Integer.parseInt(request.getParameter("y2"));
         int w=Integer.parseInt(request.getParameter("w"));
         int h=Integer.parseInt(request.getParameter("h"));
         System.out.println(x1+" "+y1+" "+x2+" "+y2+" "+w+" "+" "+h);

         String image = request.getParameter("imageName");
         System.out.println("imageName"+image);

         String sourcePath =   request.getRealPath("") + "/FreeTemp/";
         String serverPath = sourcePath + session.getAttribute("uploadFile");
         serverPath = serverPath.replace("\\", "/");
         System.out.println(serverPath);

         BufferedImage bi = ImageIO.read(new File(serverPath));

         BufferedImage out = bi.getSubimage(x1, y1, w, h);

         ImageIO.write(out,"jpg",new File(sourcePath + image));

         session.setAttribute("croppedImage", image);

         PrintWriter writer = response.getWriter();
         response.setContentType("text/html");

        return "redirect:/savephoto.htm";
    }

I am able to crop the photo, but the result is not correct. For example look at the following images:

Original Image with selection to crop
cropped image which is incorrect


Solution

  • Yes i got the problem.I need to resize the image.So, i have done using the following code:

    public static BufferedImage resizeImage(BufferedImage originalImage, int type){
            BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
            Graphics2D g = resizedImage.createGraphics();
            g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
            g.dispose();
    
            return resizedImage;
        }
    

    Now i am able to get the cropped image.