使用webcam.js转换base64图像时,我的图像已损坏

时间:2015-09-15 12:50:50

标签: javascript php mysql webcam

这是我用html使用webcam.js的html。我只是按照https://github.com/jhuckaby/webcamjs关于如何使用现有表单实现它。

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>WebcamJS Test Page</title>
    <style type="text/css">
        body { font-family: Helvetica, sans-serif; }
        h2, h3 { margin-top:0; }
        form { margin-top: 15px; }
        form > input { margin-right: 15px; }
        #results { float:right; margin:20px; padding:20px; border:1px solid; background:#ccc; }
    </style>
</head>
<body>
    <div id="results">Your captured image will appear here...</div>

    <h1>WebcamJS Test Page</h1>
    <h3>Demonstrates simple 320x240 capture &amp; display</h3>

    <div id="my_camera"></div>

    <!-- First, include the Webcam.js JavaScript Library -->
    <script type="text/javascript" src="../webcam.js"></script>

    <!-- Configure a few settings and attach camera -->
    <script language="JavaScript">
        Webcam.set({
            width: 320,
            height: 240,
            image_format: 'jpeg',
            jpeg_quality: 90
        });
        Webcam.attach( '#my_camera' );

         Webcam.snap( function(data_uri) {
        var raw_image_data = data_uri.replace(/^data\:image\/\w+\;base64\,/, '');
        document.getElementById('mydata').value = raw_image_data;
        document.getElementById('myform').submit();
    } );
    </script>

    <!-- A button for taking snaps -->
     <form id="myform" method="post" action="myscript.php">
        <input id="mydata" type="hidden" name="mydata" value=""/>
     <input type=button value="Take Snapshot" onClick="take_snapshot()">
     <input type="submit" value="submit">
    </form>

    <!-- Code to handle taking the snapshot and displaying it locally -->
    <script language="JavaScript">
        function take_snapshot() {
            // take snapshot and get image data
            Webcam.snap( function(data_uri) {
                // display results in page
                document.getElementById('results').innerHTML = 
                    '<h2>Here is your image:</h2>' + 
                    '<img src="'+data_uri+'"/>';
            } );
        }
    </script>

这是保存图像的myscript.php。我成功地将PATH保存在数据库中但我得到了一个损坏的.jpg文件(文件大小总是以7个字节为单位)。

<?php
include 'connect.php';
  $encoded_data = $_POST['mydata'];  // to get the base 64 code image link
  $name = base64_decode($encoded_data); // to convert base 64 code
  $name = date('YmdHis');
$newname="images/".$name.".jpg";
$file = file_put_contents( $newname, file_get_contents('php://input') );
if (!$file) {
    print "Error occured here";
    exit();
}
else
{
    $sql="INSERT INTO image (images) VALUES('$newname')";
    $result=mysqli_query($con,$sql);
    $value=mysqli_insert_id($con);
    $_SESSION["myvalue"]=$value;

}

$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . '/' . $newname;
print "$url\n";

?>

1 个答案:

答案 0 :(得分:0)

在我发现所有跟踪和错误之后,您需要将base64字符串转换为blob,然后在发送之前附加到文件。

    var binaryImg = atob(base64string);
    var length = binaryImg.length;
    var ab = new ArrayBuffer(length);
    var ua = new Uint8Array(ab);
    for (var i = 0; i < length; i++) {
        ua[i] = binaryImg.charCodeAt(i);
    }

    var blob = new Blob([ab], {
        type: "image/jpeg"
    });enter code here

    var imgFile = new File([blob], 'photo.jpeg', {type: 'image/jpeg'});

现在您可以使用imgFile发送到远程服务器。