使用jcrop和java裁剪图像

发布于 2024-12-12 17:42:06 字数 2683 浏览 0 评论 0原文

我正在尝试使用 JQuery Jcrop 插件和 Java Stuff 来裁剪图像,但我无法获得正确的结果。

Jsp代码:

<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代码:

@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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日情怀 2024-12-19 17:42:07

实际的问题是图像裁剪:您没有计算图像的实际自然高度和自然宽度。您可以尝试以下操作:

function showCoords(c) {
  // get image natural height/width for server site crop image.
    var imageheight = document.getElementById('cropbox').naturalHeight;
    var imagewidth = document.getElementById('cropbox').naturalWidth;
    var xper = (c.x * 100 / jQuery('#cropbox').width());
    var yper = (c.y * 100 / jQuery('#cropbox').height());
    var wPer = (c.w * 100 / jQuery('#cropbox').width());
    var hper = (c.h * 100 / jQuery('#cropbox').height());

    var actX = (xper * imagewidth / 100);
    var actY = (yper * imageheight / 100);
    var actW = (wPer * imagewidth / 100);
    var actH = (hper * imageheight / 100);
    jQuery('#x').val(parseInt(actX));
    jQuery('#y').val(parseInt(actY));
    jQuery('#w').val(parseInt(actW));
    jQuery('#h').val(parseInt(actH));

};

如果您想要完整的示例,请参阅此链接

The actual problem is the image cropping: you are not calculating the actual natural height and natural width of the image. You can try this:

function showCoords(c) {
  // get image natural height/width for server site crop image.
    var imageheight = document.getElementById('cropbox').naturalHeight;
    var imagewidth = document.getElementById('cropbox').naturalWidth;
    var xper = (c.x * 100 / jQuery('#cropbox').width());
    var yper = (c.y * 100 / jQuery('#cropbox').height());
    var wPer = (c.w * 100 / jQuery('#cropbox').width());
    var hper = (c.h * 100 / jQuery('#cropbox').height());

    var actX = (xper * imagewidth / 100);
    var actY = (yper * imageheight / 100);
    var actW = (wPer * imagewidth / 100);
    var actH = (hper * imageheight / 100);
    jQuery('#x').val(parseInt(actX));
    jQuery('#y').val(parseInt(actY));
    jQuery('#w').val(parseInt(actW));
    jQuery('#h').val(parseInt(actH));

};

If you want a complete example see this link

不寐倦长更 2024-12-19 17:42:07

是的,我遇到了问题。我需要调整图像的大小。所以,我已经使用以下代码完成了:

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;
    }

现在我能够获得裁剪后的图像。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文