In my daily life, I enjoy taking photos with my smartphone and uploading them to various websites. So I started thinking, “Is it possible to implement these functions in web browser?” Although Dynamsoft ImageCapture Suite allows us to capture images from webcam, it is designed for a desktop App development on windows and Mac, not for mobile platforms. Fortunately, the new HTML5 SDK is capable of uploading local images or captured images to web servers. Everything becomes easier if we have a mobile web browser, like Chrome, which is fully compatible to HTML5.
Take Photos in Browsers of Android and iOS
Using HTML5 to invoke the camera is very simple.
For more information, you can reference HTML Media Capture. Due to the platform-dependency, you need to read Mobile HTML5 and search HTML Media Capture for relevant information. Referring to the article Html5 File Upload with Progress, I have made some improvements. The source code has been tested on Android 4.1 and iOS 7.0.6. You can see the following figures.
Android:
iOS:
Source Code
Client:
<!DOCTYPE html> <html> <head> <title>Take or select photo(s) and upload</title> <script type="text/javascript"> function fileSelected() { var count = document.getElementById('fileToUpload').files.length; document.getElementById('details').innerHTML = ""; for (var index = 0; index < count; index ++) { var file = document.getElementById('fileToUpload').files[index]; var fileSize = 0; if (file.size > 1024 * 1024) fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB'; else fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB'; document.getElementById('details').innerHTML += 'Name: ' + file.name + '<br>Size: ' + fileSize + '<br>Type: ' + file.type; document.getElementById('details').innerHTML += '<p>'; } } function uploadFile() { var fd = new FormData(); var count = document.getElementById('fileToUpload').files.length; for (var index = 0; index < count; index ++) { var file = document.getElementById('fileToUpload').files[index]; fd.append(file.name, file); } var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", uploadProgress, false); xhr.addEventListener("load", uploadComplete, false); xhr.addEventListener("error", uploadFailed, false); xhr.addEventListener("abort", uploadCanceled, false); xhr.open("POST", "savetofile.aspx"); xhr.send(fd); } function uploadProgress(evt) { if (evt.lengthComputable) { var percentComplete = Math.round(evt.loaded * 100 / evt.total); document.getElementById('progress').innerHTML = percentComplete.toString() + '%'; } else { document.getElementById('progress').innerHTML = 'unable to compute'; } } function uploadComplete(evt) { /* This event is raised when the server send back a response */ alert(evt.target.responseText); } function uploadFailed(evt) { alert("There was an error attempting to upload the file."); } function uploadCanceled(evt) { alert("The upload has been canceled by the user or the browser dropped the connection."); } </script> </head> <body> <form id="form1" enctype="multipart/form-data" method="post" action="Upload.aspx"> <div> <label for="fileToUpload">Take or select photo(s)</label><br /> <input type="file" name="fileToUpload" id="fileToUpload" onchange="fileSelected();" accept="image/*" capture="camera" /> </div> <div id="details"></div> <div> <input type="button" onclick="uploadFile()" value="Upload" /> </div> <div id="progress"></div> </form> </body> </html>
Server savetofile.aspx:
<%@ Page Language="C#" %> <% HttpFileCollection files = HttpContext.Current.Request.Files; for (int index = 0; index < files.Count; index ++) { HttpPostedFile uploadfile = files[index]; // You must create “upload” sub folder under the wwwroot. uploadfile.SaveAs(Server.MapPath(".") + "upload" + uploadfile.FileName); } HttpContext.Current.Response.Write("Upload successfully!"); %>
For PHP
Modify the client:
1. Change
fd.append(file.name, file);
To
fd.append('myFile', file);
2. Change
xhr.open("POST", "savetofile.aspx");
To
xhr.open("POST", "savetofile.php");
Create savetofile.php:
<?php if (isset($_FILES['myFile'])) { // Example: move_uploaded_file($_FILES['myFile']['tmp_name'], "uploads/" . $_FILES['myFile']['name']); echo 'successful'; } ?>
Source Code
Github: https://github.com/DynamsoftRD/HTML5-Photo-Upload
Get the source code: git clone https:
//github.com/DynamsoftRD/HTML5-Photo-Upload.git
原文:http://www.codepool.biz/take-a-photo-and-upload-it-on-mobile-phones-with-html5.html
转自:Take a Photo and Upload it on Mobile Phones with HTML5
相关推荐
Building Websites with HTML5 to Work with Mobile Phones.pdf 使用HTML5创建与移动电话协同的网站
3D Game Engine Design for mobile phones with openGL es 2.0
NFC on mobile phones issues, lessons and future research
Advanced Game Design with HTML5 and JavaScript is a down to earth education in how to make video games from scratch, using the powerful HTML5 and JavaScript technologies. This book is a point-by-...
java 16:mobile phones and android software development
HTML5 can be used with JavaScript, CSS3, and WebGL to create beautiful, unique, engaging games that can be played on the web or mobile devices like the iPad or Android phones. Gone are the days where...
Mobile phones, in particular, are impacting our everyday lives, as the devices we carry with us are evolving into full mobile computers. Soon we will be living in a world where most of us get our ...
Symbian OS C++ for Mobile Phones(Volume1-3全)
Soon, more web browsing will take place on phones and tablets than PCs. Your business needs a mobile strategy, but where do you start? Head First Mobile Web shows how to use the web tech- nology you...
You will embark on a journey of building effective responsive web pages that work across a range of devices, from mobile phones to smart TVs, with nothing more than standard markup and styling ...
written by some of the key technical experts...provides a comprehensive but practical and easily understood introduction for any software engineer seeking to delight the consumer with rich 3D ...
Charisma is a fully featured, free, premium quality, responsive, HTML5 admin template (or backend template) based on Bootstrap from Twitter, it comes with 9 different themes to suit your style and ...
SAMSUNG_USB_Driver_for_Mobile_Phones.rar
It is designed for the web builders who want to take advantage of the new design features or the infrastructure improvements of HTML5. It is geared toward individuals who want practical, real-world ...
Symbian OS C++ For Mobile Phones 开发宝典
基于智能手机的三维重建。让用户将看到的物体拍摄成为视频后准实时三维重建,并且可以将重建的三维模型传输到云端,并能够3D打印出来。
Xcode and iOS Simulator Create a Simple Android app and iOS app and run it in Emulator and Simulator respectively Set up the Android and iOS Pentesting Environment Explore mobile malware, reverse ...
SAMSUNG_USB_Driver_for_Mobile_Phones.exe
recognized text and displays it on a screen, 3. On Android, speech recognition automatically detects when user finishes speaking, but on iOS we have to wait for user clicking “Stop Recording” to ...