javascript - Jcrop 没有正确裁剪图像

标签 javascript java graphics bufferedimage jcrop

我的jcrop代码

$(function(){

// Create variables (in this scope) to hold the API and image size
var jcrop_api,
    boundx,
    boundy,

    // Grab some information about the preview pane
    $preview = $('#preview-pane'),
    $pcnt = $('#preview-pane .preview-container'),
    $pimg = $('#preview-pane .preview-container img'),

    xsize = $pcnt.width(),
    ysize = $pcnt.height();

//console.log('init',[xsize,ysize]);
$('#target').Jcrop({
  onChange: updateInfo,
  onSelect: updateInfo,
  onRelease: clearInfo,
  setSelect: [0, 0, 150, 180],
  boxWidth: 400, boxHeight: 300,
  allowMove: true, 
  allowResize: true, 
  allowSelect: true,
  aspectRatio: xsize / ysize
},function(){
  // Use the API to get the real image size
  var bounds = this.getBounds();
  boundx = bounds[0];
  boundy = bounds[1];
  // Store the API in the jcrop_api variable
  jcrop_api = this;

  // Move the preview into the jcrop container for css positioning
  $preview.appendTo(jcrop_api.ui.holder);
});


   // update info by cropping (onChange and onSelect events handler)
function updateInfo(e) {
    if (parseInt(e.w) > 0) {
        var rx = xsize / e.w;
        var ry = ysize / e.h;

        $pimg.css({
            width : Math.round(rx * boundx) + 'px',
            height : Math.round(ry * boundy) + 'px',
            marginLeft : '-' + Math.round(rx * e.x) + 'px',
            marginTop : '-' + Math.round(ry * e.y) + 'px'
        });
    }
    $('#x1').val(e.x);
    $('#y1').val(e.y);
    $('#w').val(e.w);
    $('#h').val(e.h);
};

// clear info by cropping (onRelease event handler)
function clearInfo() {
    $('#w').val('');
    $('#h').val('');
};


   });

   Java controller which handles it

@RequestMapping(value = "/editProfileImage", method = RequestMethod.POST)
public @ResponseBody
FileMeta edit(MultipartHttpServletRequest request,
        @RequestParam(value = "x1") final int x1,
        @RequestParam(value = "y1") final int y1,
        @RequestParam(value = "w") final int w,
        @RequestParam(value = "h") final int h) throws Exception {
    Iterator<String> itr = fileIterator(request);
    MultipartFile mpf = null;
    final FileMeta fileMeta = new FileMeta();
    // 2. get each file
    while (itr.hasNext()) {
        mpf = getMultipartFile(request, itr);
        checkIfEmpty(mpf);
        checkifValidFormat(mpf);

        final BufferedImage subImage = getBufImage(mpf).getSubimage(x1, y1, w, h);

        //final BufferedImage resizedImage = resizeImage(subImage);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(subImage,
                mpf.getContentType().replace("image/", ""), baos);
        final Account account = accountManager.findBySigin((String) request
                .getAttribute("account"));
        profilePictureService.saveProfilePicture(account.getId(),
                baos.toByteArray());

        prepareMetaInformation(mpf, fileMeta, account, baos);
    }
    return fileMeta;
}

此代码适用于某些图像,但不适用于大多数图像。有没有人有任何线索。

例如下图enter image description here 它工作完美,因为我得到了完美的裁剪图像。

但是对于这个图像,例如 enter image description here 我没有正确获取裁剪后的图像。

最佳答案

我使用了下面的代码及其对我的工作..请通过下面的代码。

你的问题在这里:

setSelect: [0, 0, 150, 180],

你传递的是常量

jQuery(function ($) {

        // Create variables (in this scope) to hold the API and image size
        var jcrop_api,
            boundx,
            boundy,

            // Grab some information about the preview pane
            $preview = $('#preview-pane'),
            $pcnt = $('#preview-pane .preview-container'),
            $pimg = $('#preview-pane .preview-container img'),

            xsize = $pcnt.width(),
            ysize = $pcnt.height();

        console.log('init', [xsize, ysize]);
        $('#target').Jcrop({
            onChange: updatePreview,
            onSelect: updatePreview,
            onSelect: storeCoords,
            aspectRatio: xsize / ysize,
            boxWidth: 350, boxHeight: 350
        }, function () {
            // Use the API to get the real image size
            var bounds = this.getBounds();
            boundx = bounds[0];
            boundy = bounds[1];
            // Store the API in the jcrop_api variable
            jcrop_api = this;

            // Move the preview into the jcrop container for css positioning
            $preview.appendTo(jcrop_api.ui.holder);
        });

        function updatePreview(c) {
            if (parseInt(c.w) > 0) {
                var rx = xsize / c.w;
                var ry = ysize / c.h;

                $pimg.css({
                    width: Math.round(rx * boundx) + 'px',
                    height: Math.round(ry * boundy) + 'px',
                    marginLeft: '-' + Math.round(rx * c.x) + 'px',
                    marginTop: '-' + Math.round(ry * c.y) + 'px'
                });
            }
            // storeCoords(c);
        };
        function storeCoords(c) {

            jQuery('#X').val(c.x);
            jQuery('#Y').val(c.y);
            jQuery('#W').val(c.w);
            jQuery('#H').val(c.h);


        };

    });

请将此函数从您的代码中分离出来。

     function storeCoords(c) {

        jQuery('#X').val(c.x);
        jQuery('#Y').val(c.y);
        jQuery('#W').val(c.w);
        jQuery('#H').val(c.h);


    };

并在您之前设置的固定坐标处调用 storeCoords 作为

setSelect:storeCoords ,

关于javascript - Jcrop 没有正确裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17919733/

相关文章:

javascript - 如何更改 DOM 对象原型(prototype)中的方法

javascript - 查看传递给抛出异常的构造函数的字符串

java - 嵌套 Scala Future 是否需要托管阻塞?

java - 每当我单击 java 中弹出菜单中的任何项目时,如何触发 click() 事件?

c++ - 在 renderPass 中更新 VkDescriptorSet

java - 什么时候是 Swing 组件 'displayable' ?

javascript - onblur 和链接元素在 Chrome 中无法正常工作 - 在 IE 10 中正常

javascript - 淡入淡出在 FF 上有效,但在 IE 上无效

java - 计算字符串中最长的回文子串

xml - 对 SVG 的 Ruby 支持