使用 HTML5 控制摄像头摄像和拍照并保存图像文件到本地和JSP上传到服务器

£神魔★判官ぃ 2023-10-02 11:06 72阅读 0赞
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  4. "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8. <title>File Upload</title>
  9. </head>
  10. <body>
  11. <video id="video" width="640" height="480" autoplay></video>
  12. <button id="snap">Snap Photo</button>
  13. <button id="save">Save Photo</button>
  14. <canvas id="canvas" width="640" height="480"></canvas>
  15. <form method="post" action="save.jsp">
  16. <input type="hidden" name="file" id="file" /> <input type="submit"
  17. id="upload" value="upload" />
  18. </form>
  19. <script type="text/javascript">
  20. // Grab elements, create settings, etc.
  21. var video = document.getElementById('video');
  22. // Get access to the camera!
  23. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  24. // Not adding `{ audio: true }` since we only want video now
  25. navigator.mediaDevices.getUserMedia({
  26. video : true
  27. }).then(function(stream) {
  28. //video.src = window.URL.createObjectURL(stream);
  29. video.srcObject = stream;
  30. video.play();
  31. });
  32. }
  33. /* Legacy code below: getUserMedia
  34. else if(navigator.getUserMedia) { // Standard
  35. navigator.getUserMedia({ video: true }, function(stream) {
  36. video.src = stream;
  37. video.play();
  38. }, errBack);
  39. } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
  40. navigator.webkitGetUserMedia({ video: true }, function(stream){
  41. video.src = window.webkitURL.createObjectURL(stream);
  42. video.play();
  43. }, errBack);
  44. } else if(navigator.mozGetUserMedia) { // Mozilla-prefixed
  45. navigator.mozGetUserMedia({ video: true }, function(stream){
  46. video.srcObject = stream;
  47. video.play();
  48. }, errBack);
  49. }
  50. */
  51. // Elements for taking the snapshot
  52. var canvas = document.getElementById('canvas');
  53. var context = canvas.getContext('2d');
  54. var video = document.getElementById('video');
  55. // Trigger photo take
  56. document.getElementById("snap").addEventListener("click", function() {
  57. context.drawImage(video, 0, 0, 640, 480);
  58. });
  59. function saveAsLocalImage() {
  60. var myCanvas = document.getElementById("canvas");
  61. // here is the most important part because if you dont replace you will get a DOM 18 exception.
  62. // var image = myCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream;Content-Disposition: attachment;filename=foobar.png");
  63. var image = myCanvas.toDataURL("image/png").replace("image/png","image/octet-stream");
  64. // window.location.href = image; // it will save locally
  65. // create temporary link
  66. var tmpLink = document.createElement('a');
  67. tmpLink.download = 'image.png'; // set the name of the download file
  68. tmpLink.href = image;
  69. // temporarily add link to body and initiate the download
  70. document.body.appendChild(tmpLink);
  71. tmpLink.click();
  72. document.body.removeChild(tmpLink);
  73. }
  74. function upload() {
  75. var myCanvas = document.getElementById("canvas");
  76. // here is the most important part because if you dont replace you will get a DOM 18 exception.
  77. // var image = myCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream;Content-Disposition: attachment;filename=foobar.png");
  78. var image = myCanvas.toDataURL("image/png");
  79. // window.location.href = image; // it will save locally
  80. document.getElementById('file').value = image;
  81. }
  82. // Trigger photo save
  83. document.getElementById("save").addEventListener("click", function() {
  84. saveAsLocalImage();
  85. });
  86. // Trigger photo upload
  87. document.getElementById("upload").addEventListener("click",function() {
  88. upload();
  89. });
  90. </script>
  91. </body>
  92. </html>
  93. <%@page import="java.io.FileOutputStream"%>
  94. <%@page import="java.io.IOException"%>
  95. <%@page import="java.io.InputStream"%>
  96. <%@page import="java.util.Base64"%>
  97. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  98. pageEncoding="ISO-8859-1"%>
  99. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  100. "http://www.w3.org/TR/html4/loose.dtd">
  101. <html>
  102. <head>
  103. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  104. <title>Upload</title>
  105. </head>
  106. <body>
  107. <%
  108. FileOutputStream fos = null;
  109. try {
  110. String imageString = request.getParameter("file");
  111. imageString = imageString.substring("data:image/png;base64,".length());
  112. byte[] contentData = imageString.getBytes();
  113. byte[] decodedData = Base64.getDecoder().decode(contentData);
  114. String imgName = request.getServletContext().getRealPath("uploadFiles") + "\\" + String.valueOf(System.currentTimeMillis()) + ".png";
  115. out.println(imgName);
  116. fos = new FileOutputStream(imgName);
  117. fos.write(decodedData);
  118. } catch (Exception e) {
  119. e.printStackTrace();
  120. } finally {
  121. if (fos != null) {
  122. fos.close();
  123. }
  124. }
  125. %>
  126. </body>
  127. </html>

发表评论

表情:
评论列表 (有 0 条评论,72人围观)

还没有评论,来说两句吧...

相关阅读