Several ways to realize the camera function of the mobile terminal H5

The following are several ways to realize the H5 camera function on the mobile terminal:

1. Use<input type="file"><input type="file"> : call the system camera through in the HTML5 specification , and select the photo to be taken. But this way may cause page refresh.

The code to realize the H5 camera function on the mobile terminal:

  1. Create an <input type="file"> in HTML:
<input type="file" accept="image/*" capture="camera">

 2. Bind the change event to the element in JavaScript, and read the selected image file:

 

var input = document.querySelector("input[type=file]");
input.addEventListener("change", function(e) {
  var file = e.target.files[0];
  var reader = new FileReader();
  reader.onload = function(e) {
    var dataURL = e.target.result;
    // 在此处对 dataURL 进行操作,例如显示图片
  };
  reader.readAsDataURL(file);
});

The method of using <input type="file"> to realize the H5 camera function on the mobile terminal is simple and easy to understand, but it may cause page refresh problems .

2. Use WebRTC : realize the camera access through WebRTC technology, that is, MediaDevices.getUserMedia()access the camera through the API in the HTML5 specification, and realize the camera function.

WebRTC is a set of APIs that enable real-time communication capabilities in the browser, including access to cameras and microphones. If you want to implement the camera function in the mobile H5 application, you can use the WebRTC API to access the camera and implement the camera function.

Use the MediaDevices.getUserMedia() API in the WebRTC API to realize the mobile H5 camera function. This API allows you to access the user's camera and microphone for taking pictures. Note that permission to access the camera needs to be requested from the user, and it needs to be run on an HTTPS protocol website.

The following is a code example to implement the camera function:

<button id="startbutton">Take photo</button>
<video id="video"></video>
<canvas id="canvas"></canvas>

<script>
  // 获取视频和画布元素
  const video = document.querySelector('#video');
  const canvas = document.querySelector('#canvas');
  const startButton = document.querySelector('#startbutton');

  // 启动摄像头
  async function startCamera() {
    const stream = await navigator.mediaDevices.getUserMedia({
      video: true
    });
    video.srcObject = stream;
    video.play();
  }

  // 拍照
  function takePhoto() {
    canvas.width = video.videoWidth;
    canvas.height = video.videoHeight;
    canvas.getContext('2d').drawImage(video, 0, 0);
  }

  // 启动摄像头
  startCamera();

  // 在按钮上绑定拍照事件
  startButton.addEventListener('click', takePhoto);
</script>

 

By using the MediaDevices.getUserMedia() API you can avoid the refresh problem and let your H5 application have camera function. The MediaDevices.getUserMedia() API is only available on supported browsers and requires the user to grant camera access.

Also, if you need to implement complex image processing in H5, you can use JavaScript libraries such as fabric.js, p5.js or Three.js. These libraries can help you implement complex image processing more easily without having to manually write complex code.

Using WebRTC API to implement the mobile H5 camera function requires an in-depth understanding of WebRTC API and proper error handling to ensure normal work on different browsers and mobile devices.

All of the above methods can help you realize the H5 camera function on the mobile terminal. Of course, these methods are also applicable to the PC-side web pages with callable cameras. You can choose the appropriate method according to your needs and technical level.

Guess you like

Origin blog.csdn.net/lwf3115841/article/details/129038227