实际项目-移动端调用摄像头获取照片加水印

H5移动端打开摄像头和打开相册

参考文档:http://www.html5plus.org/doc/zh_cn/camera.html
!!!plus在PC端运行时会报错,只有打包后在手机端运行时才能运行

//选择打开摄像头还是打开相册
  takePhoto(){
            let that = this;
            let bts = [
                {
                title: "拍照"
                },
                {
                title: "从相册选择"
                }
            ];
            plus.nativeUI.actionSheet(
                {
                cancel: "取消",
                buttons: bts
                },
                function(e) {
                if (e.index == 1) {
                    that.getCamera();
                } else if (e.index == 2) {
                    that.getImg();
                }
                }
            );


        },
//打开摄像头
getCamera(){
            let that = this;
            let cmr = plus.camera.getCamera();
            //这里配置其实不起作用
            let res = cmr.supportedImageResolutions[14];
            let fmt = cmr.supportedImageFormats[0];
            cmr.captureImage(
                function(path){
                    //获取拍照后的真实地址
                    plus.io.resolveLocalFileSystemURL(path,function(entry) {//通过URL参数获取目录对象或文件对象
                        console.log("拍照获取的真实路径",entry.fullPath)
                         that.takeImgs.push(entry.fullPath);//直接可以显示的类型
                         var imgSrc = entry.toLocalURL();//获取目录路径转换为本地路径URL地址
                         console.log("imgSrcimgSrc",imgSrc)
                         that.takeImgSrc.push(imgSrc);//URL地址型方便于转换base64
                        
                    },
                    function(e) {
                        console.log(e.message);
                    },{ resolution: res, format: fmt }
                    );

                }
            )
        },
  //打开相册
   getImg(){
            let that = this;
            plus.gallery.pick(
                function(path) {
                    that.takeImgs.push(path);
                    that.takeImgSrc.push(path);
                },
                function(e) {
                console.log("取消选择图片");
                },
                { filter: "image" }
            );

        },

以上就是如何在移动端打开摄像头和相册获取照片的方式。但是在实际项目可能后端想要的图片格式是不确定的,可能是base64.可能是file文件对象,所以我们要对获取到的照片处理成相对应的格式

/* 照片转码成base64加上时间水印 */
      getBase64Time(url){
      let that =this;
      return new Promise((resolve,reject)=>{//异步处理
        let canvas = document.createElement("canvas"),
        ctx = canvas.getContext("2d"),
        image = new Image(),
        fontSize,//水印的大小
        MAX_WH = 800;//图片的最大宽高比,因为在以上方法中获取的照片size太大,上传时耗时太多所以需要做处理
        image.crossOrigin = "Anonymous";
        image.onload = function() {//这里是一个异步,所以获取到的base64文件需要用回调
              if (image.height>MAX_WH) {
                image.width *= MAX_WH/image.height;
                image.height = MAX_WH
              }
              if (image.width>MAX_WH) {
                image.height *= MAX_WH/ image.width;
                image.width = MAX_WH;
              }
              canvas.height = image.height;
              canvas.width = image.width;
              ctx.drawImage(image, 0, 0,image.width,image.height);
              if(image.width>100 && image.width<500){
                  fontSize = '24px'
              }else if(image.width>=500 && image.width<1000){
                  fontSize = '44px' 
              }else if(image.width>=1000 && image.width<1500){
                  fontSize = '64px'
              }
              ctx.font =`${fontSize} Arial`;
              ctx.fillStyle = "tomato"; 
              let time = that.getCurrnetTime("timeSign");//获取当前的时间
              ctx.textAlign = "end";
              ctx.textBaseline = "middle";
              ctx.fillText(time,image.width-20,image.height-60);
              let dataURL = canvas.toDataURL( "image/png/jpg"); 
              if(dataURL) {
                resolve(dataURL)
              }else{
                reject("err")
              }
          };
        image.src = url
      })
    },
    //调用此方法之后文件就转为base64格式了

如需要file文件格式,见https://blog.csdn.net/weixin_42307283/article/details/103613682

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Vue移动端调用摄像头进行二维码扫描,可以使用第三方插件“vue-qrcode-reader”来实现。首先,通过npm命令安装该插件:npm install vue-qrcode-reader。 在项目的main.js中引入插件,并全局注册: import Vue from 'vue'; import VueQrcodeReader from 'vue-qrcode-reader'; Vue.use(VueQrcodeReader); 然后,在使用二维码扫描的组件中,可以使用v-qrcode指令来调用摄像头进行扫描。例如,在一个按钮上使用v-qrcode指令: <button v-qrcode="{ callback: scanResult }">扫描二维码</button> 在组件的methods中定义scanResult方法来处理扫描结果: methods: { scanResult(result) { // 处理扫描结果 console.log(result) } } 当用户点击“扫描二维码”按钮时,会弹出摄像头扫描界面。用户将二维码对准摄像头,插件会自动识别二维码,并将结果传递给scanResult方法进行处理。 需要注意的是,为了保证扫描成功,需要在移动端使用https协议,或者在localhost上运行。另外,在某些移动端浏览器上可能需要用户授权摄像头权限。 通过以上的步骤,Vue移动端就可以实现调用摄像头进行二维码扫描,并将扫描结果进行处理。 ### 回答2: 在vue移动端调用摄像头进行二维码扫描,可以使用第三方库vue-qrcode-reader。首先,需要在项目中引入该库。可以通过npm进行安装: ```javascript npm install vue-qrcode-reader --save ``` 然后,在需要调用摄像头扫描二维码的组件中,引入并注册该库。在template中,添加一个按钮,用于调用摄像头扫描二维码: ```html <template> <div> <button @click="scanQRCode">扫描二维码</button> </div> </template> ``` 在script中,引入并注册该库,然后编写用于调用摄像头扫描二维码的方法: ```javascript <script> import { QrcodeStream } from 'vue-qrcode-reader' export default { components: { QrcodeStream }, methods: { scanQRCode() { // 使用QrcodeStream的start方法启动摄像头扫描二维码 this.$refs.qrcodeReader.start() }, // 当二维码扫描成功时,会触发该方法 onDecode(result) { console.log(result) // 扫描到的二维码结果可以在这里处理 } } } </script> ``` 最后,在该组件的template中,添加一个QrcodeStream的标签,并绑定onDecode方法,用于接收扫描到的二维码结果: ```html <template> <div> <button @click="scanQRCode">扫描二维码</button> <qrcode-stream @decode="onDecode" ref="qrcodeReader"></qrcode-stream> </div> </template> ``` 现在,当点击扫描二维码按钮后,摄像头会被启动,用户可以通过摄像头扫描二维码,当扫描成功后,结果会通过onDecode方法接收并处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值