因为之前OA系统财务上传凭证的时候,要拿手机拍,传到电脑上上传,为了方便财务上传凭证,老大让做一个利用电脑摄像头拍照上传的功能:

    使用技术:
    WebCam.js
    FastDFS

    实现原理:
    前端调用webcam的方法进行拍照,生成base64格式,然后把base64传入后端进行解码与保存

    前端代码(只选取关键代码):

    1. <script language="JavaScript">
    2. Webcam.set({
    3. width: 800, //照片的宽度
    4. height: 600, //照片的高度
    5. image_format: 'jpeg', //照片格式
    6. jpeg_quality: 90 //照片质量
    7. });
    8. Webcam.attach( '#my_camera' );//将拍摄区域展示在id为my_camera的div中
    9. </script>
    10. <!-- A button for taking snaps -->
    11. <form>
    12. <input type=button value="Take Snapshot" onClick="take_snapshot()">
    13. </form>
    14. <!-- Code to handle taking the snapshot and displaying it locally -->
    15. <script language="JavaScript">
    16. function take_snapshot() {
    17. // take snapshot and get image data
    18. Webcam.snap( function(data_uri) {
    19. // display results in page
    20. console.log(data_uri);//Base64格式
    21. document.getElementById('results').innerHTML =
    22. '<h2>Here is your image:</h2>' +
    23. '<img src="'+data_uri+'"/>';
    24. } );
    25. }
    26. </script>

    可以设置的参数:
    image.png
    后台上传(Base64转MultipartFile):

    1. import org.springframework.web.multipart.MultipartFile;
    2. import sun.misc.BASE64Decoder;
    3. import java.io.*;
    4. public class BASE64DecodedMultipartFile implements MultipartFile {
    5. private final byte[] imgContent;
    6. private final String header;
    7. public BASE64DecodedMultipartFile(byte[] imgContent, String header) {
    8. this.imgContent = imgContent;
    9. this.header = header.split(";")[0];
    10. }
    11. @Override
    12. public String getName() {
    13. // TODO - implementation depends on your requirements
    14. return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
    15. }
    16. @Override
    17. public String getOriginalFilename() {
    18. // TODO - implementation depends on your requirements
    19. return System.currentTimeMillis() + (int) Math.random() * 10000 + "." + header.split("/")[1];
    20. }
    21. @Override
    22. public String getContentType() {
    23. // TODO - implementation depends on your requirements
    24. return header.split(":")[1];
    25. }
    26. @Override
    27. public boolean isEmpty() {
    28. return imgContent == null || imgContent.length == 0;
    29. }
    30. @Override
    31. public long getSize() {
    32. return imgContent.length;
    33. }
    34. @Override
    35. public byte[] getBytes() throws IOException {
    36. return imgContent;
    37. }
    38. @Override
    39. public InputStream getInputStream() throws IOException {
    40. return new ByteArrayInputStream(imgContent);
    41. }
    42. @Override
    43. public void transferTo(File dest) throws IOException, IllegalStateException {
    44. new FileOutputStream(dest).write(imgContent);
    45. }
    46. /**
    47. * base64转MultipartFile文件
    48. *
    49. * @param base64
    50. * @return
    51. */
    52. public static MultipartFile base64ToMultipart(String base64) {
    53. try {
    54. String[] baseStrs = base64.split(",");
    55. BASE64Decoder decoder = new BASE64Decoder();
    56. byte[] b = new byte[0];
    57. b = decoder.decodeBuffer(baseStrs[1]);
    58. for (int i = 0; i < b.length; ++i) {
    59. if (b[i] < 0) {
    60. b[i] += 256;
    61. }
    62. }
    63. return new BASE64DecodedMultipartFile(b, baseStrs[0]);
    64. } catch (IOException e) {
    65. e.printStackTrace();
    66. return null;
    67. }
    68. }
    69. }

    转为MultipartFile,再调用fasetdfs的上传接口即可完成上传文件的操作。

    chrome在http调用摄像头设置开启:

    1. chrome://flags/#unsafely-treat-insecure-origin-as-secure

    然后:
    image.png
    改为enabled,在框中输入你的地址:http://xxx.xx.xx.xx:8080