duanli5662 2012-12-12 03:47
浏览 88
已采纳

使用jQuery jcrop插件从网络摄像头上传照片是错误的大小。

I am trying to take a photo with a webcam, crop it using jcrop jquery library, and upload the cropped photo to a server. Regardless of the size of the thumbnail, it always ends up with a width of 300 pixels and a height of 150 pixels.

For the webcam photo capture, I've been using a jQuery webcam plugin: http://www.xarg.org/project/jquery-webcam-plugin/. I've also tried one that uses getusermedia https://github.com/MrSwitch/jquery.getUserMedia.js.

Uploading just the webcam snaptshot works correctly and is the expected size.

My javascript:

$('#canvas').Jcrop({
        boxWidth: 320,
        boxHeight: 240,
        onChange:   updatePreview,
        onSelect:   updatePreview,
        addClass: 'jcrop-dark',
        aspectRatio : 1},function(){
    jcrop_api = this;
 });

 function showPreview(coords)
{
    var rx = 100 / coords.w;
    var ry = 100 / coords.h;

    $('#preview').css({
        width: Math.round(rx * 320) + 'px',
        height: Math.round(ry * 240) + 'px',
        marginLeft: '-' + Math.round(rx * coords.x) + 'px',
        marginTop: '-' + Math.round(ry * coords.y) + 'px'
    });
}

function updatePreview(c) {
    if(parseInt(c.w) > 0) {
        // Show image preview
        var imageObj = $("#canvas")[0];
        var canvas = $("#preview")[0];
        var context = canvas.getContext("2d");
        context.drawImage(imageObj, c.x, c.y, c.w, c.h, 0, 0, canvas.width, canvas.height);
    }
};
$('#save').click(function(){
    var imgData = document.getElementById('canvas').toDataURL();
    var postStr = "img=" + encodeURIComponent(imgData);
    $.ajax({
        type: 'POST',
        url: '{{ path('photo_upload') }}',
        data: postStr,
        async: false,
        processData: false,
        error: function(){
            $('.hero-unit').prepend('<div class="alert alert-error">The photo could not be uploaded.<button type="button" class="close" data-dismiss="alert">×</button></div>');
        }
    });
});

Simple Test PHP

define('UPLOAD_DIR', 'photos/');
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR  . 'photo.png';
$fp = fopen($file, 'wb');
$success=fwrite($fp, $data);
fclose($fp);
print $success ? $file : 'Unable to save the file.';

My HTML:

<div id="webcam"></div>
<a href="javascript:webcam.capture();changeFilter();void(0);">Take a picture instantly</a>
<canvas id="canvas" height="240" width="320"></canvas>
<canvas id="preview" style="width:300px;height:300px;overflow:hidden;"></canvas>
<a class="btn btn-primary btn-large" id="save">Save Photo</a>
  • 写回答

1条回答 默认 最新

  • douxi5940 2012-12-12 04:35
    关注

    Is that code exactly what you are trying to use?

    My guess would be you have a canvas which is still at the default size (which just happens to be 300x150) which is causing that to be the image that is sent to your server and saved.

    Also, why do you have your preview with a style width and height instead of setting the actual width and height?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b