Vue implements the function of calling the mobile phone camera and album

Vue implements the function of calling the mobile phone camera and album

This article shares the specific code of Vue to access the mobile phone camera and album for your reference. The specific content is as follows

My own summary of the native method of taking photos and albums on mobile phones

HTML Code

<div>
//Picture to be displayed <div class="imgBox name">
      <img :src="imgSrc" />
  </div>
  <van-action-sheet v-model="show1">
   <ul>
    <li class="paizhao" @click="captureImage()">Take a photo</li>
    <li class="paizhao" @click="galleryImg()">Select from album</li>
    <li class="paizhao" @click="quxiao()">Cancel</li>
   </ul>
  </van-action-sheet> 
</div>

js logic code

//Variables declared in data () {
return {
 imgSrc: "", //The displayed image path tupianlist: "", //The displayed image container }
}

Define the event name in the methods event method

methods: {
     // Select an image from the album galleryImg() {
      let This = this;
      console.log("Select a picture from the album:");
      plus.gallery.pick(function(path) {
        This.imgSrc = path; //path is a local path let img = new Image();
        img.src = path;
        img.onload = function(path) {
          var that = img;
          var w = that.width, //320
            h = that.height, //426
            scale = w / h;
          w = 320 || w;
          h = w / scale;
          var canvas = document.createElement("canvas");
          canvas.width = 300; //This setting cannot be lost, otherwise it will become the canvas default size of 300*150 canvas.height = 300; //This setting cannot be lost, otherwise it will become the canvas default size of 300*150 var ctx = canvas.getContext("2d");
          ctx.drawImage(that, 0, 0, 300, 300);
          var base64 = canvas.toDataURL(
            "image/png",
            "image/jpeg",
            "image/jpg",
            1 || 0.8
          );     
          This.tupianlist = base64;
          // console.log(This.tupianlist + "I am the transcoded base");
 
  //You can request the interface here};
      });
    },
    // Take a photo captureImage() {
      let This = this;
      var cmr = plus.camera.getCamera(); //Get the camera management object var res = cmr.supportedImageResolutions[0]; //String array, camera supported resolutions var fmt = cmr.supportedImageFormats[0]; //String array, camera supported file formats // console.log("Photo resolution: " + res + ", Photo file format: " + fmt);
      cmr.captureImage(
        function(path) {
          plus.gallery.save(path, params => {
            let file = params.file;
            //Encode as base64
            var img = new Image();
            img.src = file;
            img.onload = function() {
              var that = img;
              var w = that.width,
                h = that.height,
                scale = w / h;
              w = 320 || w;
              h = w / scale;
              var canvas = document.createElement("canvas");
              canvas.width = 300; //This setting cannot be lost, otherwise it will become the canvas default size of 300*150 canvas.height = 300; //This setting cannot be lost, otherwise it will become the canvas default size of 300*150 var ctx = canvas.getContext("2d");
              ctx.drawImage(that, 0, 0, 300, 300);
              var base64 = canvas.toDataURL(
                "image/png",
                "image/jpeg",
                "image/jpg",
                1 || 0.8
              );
              // console.log(base64);
              This.tupianlist = base64;
  //You can request the interface here};
          });
          //Perform photo taking operation// Get directory object or file object through URL parameter plus.io.resolveLocalFileSystemURL(path, function(entry) {
            var tmpPath = entry.toLocalURL(); //Get the directory path and convert it to the local path URL address This.imgSrc = tmpPath;
            // alert("Shooting successful: " + tmpPath);
          });
        },
        function (error) {
          //Callback when image capture failed // alert("Image capture failed: " + error.message);
        },
        { resolution: res, format: fmt }
      );
      this.show1 = false;
    },
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue calls the PC camera to realize the photo function
  • Vue calls the computer camera to realize the photo function
  • Vue calls the local camera to realize the photo function
  • Vue Yingshi camera live video example code
  • Vue2.0 implements the function of calling the camera to take pictures, and exif.js implements the picture upload function
  • Vue implements calling PC camera to take photos in real time

<<:  How to configure MGR single master and multiple slaves in MySQL 8.0.15

>>:  Detailed deployment of docker+gitlab+gitlab-runner

Recommend

How to use the realip module in Nginx basic learning

Preface There are two types of nginx modules, off...

Nginx URL rewriting mechanism principle and usage examples

URL rewriting helps determine the preferred domai...

MySQL 8.0.23 installation super detailed tutorial

Table of contents Preface 1. Download MySQL from ...

MySQL 8.0.12 Quick Installation Tutorial

The installation of MySQL 8.0.12 took two days an...

JavaScript Dom Object Operations

Table of contents 1. Core 1. Get the Dom node 2. ...

Front-end AI cutting tips (experience)

AI image cutting needs to be coordinated with PS....

VMware Workstation is not compatible with Device/Credential Guard

When installing a virtual machine, a prompt appea...

Vue must learn knowledge points: the use of forEach()

Preface In front-end development, we often encoun...

Detailed explanation of JavaScript's Set data structure

Table of contents 1. What is Set 2. Set Construct...

JavaScript implementation of carousel example

This article shares the specific code for JavaScr...

Sample code for making a drop-down menu using pure CSS

Introduction: When I looked at interview question...

How to set a fixed IP address for a VMware virtual machine (graphic tutorial)

1. Select Edit → Virtual Network Editor in the me...

mysql installer web community 5.7.21.0.msi installation graphic tutorial

This article example shares the specific code for...

JS implements random generation of verification code

This article example shares the specific code of ...