mui调取手机摄像头,拍照,上传

我就是我 2022-06-05 10:07 415阅读 0赞

最近在做H5,调取手机摄像头,拍照,上传功能,本想上传base64编码,却没实现,后改为上传二进制文件的方式上传的,废话不多说,上代码:

  1. <script src="js/fw/jquery/jquery-1.12.2.min.js"></script>
  2. <script src="vendor/libs/mui.min.js"></script>
  3. <script type="text/javascript">
  4. var file_url;
  5. var timestamp;
  6. var dataBase64;
  7. mui.init();
  8. mui.plusReady(function() {
  9. // 扩展API加载完毕后调用onPlusReady回调函数
  10. document.getElementById('faceVali').addEventListener('tap', function() {
  11. openCamera();
  12. },false);
  13. });
  14. //打开手机摄像头
  15. function openCamera() {
  16. var cmr = plus.camera.getCamera();
  17. cmr.captureImage(function(p) {
  18. plus.io.resolveLocalFileSystemURL(p, function(entry) {
  19. plus.nativeUI.showWaiting("人脸识别中", ""); //显示系统loading框
  20. plus.zip.compressImage({
  21. src: entry.toLocalURL(),
  22. dst: '_doc/camera/' + p,
  23. overwrite: true,
  24. format: "jpg",
  25. width: "30%"
  26. }, function(zip) {
  27. if(zip.size > (1 * 1024 * 1024)) {
  28. return mui.toast('文件超大,请调整相机重新拍照');
  29. }
  30. file_url = zip.target;
  31. //转为base64
  32. // getBase64(file_url);
  33. uploadToServer(file_url);
  34. }, function(zipe) {
  35. plus.nativeUI.closeWaiting();
  36. mui.toast('压缩失败!')
  37. });
  38. }, function(e) {
  39. plus.nativeUI.closeWaiting(); //获取图片失败,loading框取消
  40. mui.toast('失败:' + e.message); //打印失败原因,或给出错误提示
  41. });
  42. }, function(e) {
  43. plus.nativeUI.closeWaiting(); //开启照相机失败,关闭loading框
  44. mui.toast('失败:' + e.message); //打印错误原因,给出错提示
  45. }, {
  46. filename: '_doc/camera/', //图片名字
  47. index: 1 //摄像头id
  48. });
  49. }
  50. //上传
  51. function uploadToServer(file_url) {
  52. var url = ITFC_ADDR.JP_FACEVERIFY;
  53. var task = plus.uploader.createUpload(url, {method:"POST",priority:100}, function(t, status) {
  54. plus.nativeUI.closeWaiting();
  55. // mui.toast("t="+JSON.stringify(t));
  56. if(status == 200) {
  57. var msg = JSON.parse(task.responseText);
  58. if(msg.message.code=='00'){
  59. //人脸验证通过
  60. mui.toast(msg.message.message);
  61. console.log(msg.message.message);
  62. postImg();
  63. }else{
  64. //验证失败
  65. mui.toast(msg.message.errorMessage);
  66. }
  67. } else {
  68. console.log(':上传失败');
  69. mui.toast("上传失败: " + status);
  70. }
  71. });
  72. task.addFile(file_url, {key: 'faceImg'});
  73. /*task.addEventListener("statechanged",function(upload, status ){ mui.toast(upload+"==="+ status ); },false);*/
  74. task.start();
  75. }
  76. //获取base64方法
  77. function getBase64(url) { //传入图片路径
  78. function getBase64Image(img,width,height) {
  79. var canvas = document.createElement("canvas");
  80. canvas.width = width ? width : img.width;
  81. canvas.height = height ? height : img.height;
  82. var ctx = canvas.getContext("2d");
  83. ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
  84. dataBase64 = canvas.toDataURL("image/jpg");
  85. postImg(dataBase64.substr(22));//dataBase64上传到后台
  86. }
  87. var image = new Image();
  88. image.onload=function(){ //onload事件不执行,后查是因为onloand事件是基于http协议的,file://。。.jpg路径没法执行,弃之
  89. mui.toast("load2");
  90. getBase64Image(image);
  91. };
  92. image.src=url;
  93. }
  94. </script>
  95. java后台:
  96. private File faceImg;
  97. byte[] imgCurrent = getBytesFromFile(faceImg);
  98. public static byte[] getBytesFromFile(File f){
  99. if(f!=null){
  100. BufferedInputStream bis = null;
  101. try {
  102. ByteArrayOutputStream bos = new ByteArrayOutputStream((int)f.length());
  103. bis = new BufferedInputStream(new FileInputStream(f));
  104. int buf_size = 1024;
  105. int len = 0;
  106. byte[] buffer = new byte[buf_size];
  107. while(-1 != (len = bis.read(buffer, 0, buf_size))){
  108. bos.write(buffer,0,len);
  109. }
  110. bos.flush();
  111. return bos.toByteArray();
  112. } catch (IOException e) {
  113. log.error("人脸上传出错:"+e.getMessage());
  114. }finally{
  115. if(bis != null){
  116. try {
  117. bis.close();
  118. } catch (IOException e) {
  119. e.printStackTrace();
  120. }
  121. }
  122. }
  123. }
  124. return null;
  125. }

上传到后台时,如果返回status=500,很可能你后台程序的上传解析有问题。注意下struts的配置

发表评论

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

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

相关阅读

    相关 JS 调取摄像头

    JS 调取摄像头 截止目前(2016-06-23)为止,js 调取摄像头实现视频聊天,部分浏览器还是不怎么支持的。 示例1 : 代码附上: <!DOCT