【问题标题】:Jcrop not cropping properly the imagesJcrop 无法正确裁剪图像
【发布时间】:2013-07-28 23:44:06
【问题描述】:

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

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

例如下图 它工作得很好,因为我得到了完美的裁剪图像。

但是对于这张图片,例如 我没有正确获得裁剪后的图像。

【问题讨论】:

  • 在我真正看到问题中包含 java 之前,我正在删除标签“java”。我很抱歉。我再次添加了“java”,但无法将其设为列表中的第一个标签。

标签: javascript java graphics bufferedimage jcrop


【解决方案1】:

我使用了下面的代码,它对我有用。请看下面的代码。

你的问题在这里:

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 ,

【讨论】:

    【解决方案2】:

    如果没有显示可能识别问题的错误的控制台日志,您将不得不接受我发现的不一致。 ID 标签应该只用于一个元素。我看到您正在使用 ID 标签,大概是用于多个图像。这不符合 HTML 5 合规性,因为 ID 仅适用于一个对象,而类适用于多个对象。您应该切换到一个类并遍历分配了该类的对象。例如:

    $(".cropimages").each(function(index) {
      $(this).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);
      });
    });
    

    使用该代码,确保您的所有图像都使用 cropimages 类。这应该遍历每个,然后裁剪它们。此外,请确保您拥有所有必需的库,并检查控制台是否有错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 2016-05-21
      • 2011-08-03
      相关资源
      最近更新 更多