头图

This article explains that when using the SAP UI5 barcode scanner to call the local camera, in the PC browser test, the Cordova API is also unavailable, and the execution logic of the else branch is entered:

If Cordova API is not available, but isUserMediaAccessSupported still returns true, the scan dialog can still be opened:

/**
     * Used to detect browsers which does not have access to html5 user media api and cant use device camera
     * @private
     * @returns {boolean} true is user media access supported by html5 compatible browser
     */
    function isUserMediaAccessSupported() {
        return !!(window && window.navigator && window.navigator.mediaDevices && window.navigator.mediaDevices.getUserMedia);
    }

Here window.navigator.mediaDevices.getUserMedia of means :

The MediaDevices.getUserMedia() method prompts the user for permission to use the media input, which produces a MediaStream whose tracks contain the requested media type.

For example, the stream may include video tracks (generated by hardware or virtual video sources (eg cameras, video recording devices, screen sharing services, etc.), audio tracks (similarly generated by physical or virtual audio sources (eg microphones, A/D) converters, etc.), and possibly other track types.

It returns a Promise that resolves to a MediaStream object. If the user denies permission, or the matching media is unavailable, the promise is rejected with NotAllowedError or NotFoundError DOMException, respectively.

The getUserMedia is natively implemented by the browser.

async function getMedia(constraints) {
  let stream = null;

  try {
    stream = await navigator.mediaDevices.getUserMedia(constraints);
    /* use the stream */
  } catch(err) {
    /* handle the error */
  }
}

getUserMedia in the constraints parameter of 0623ad38ae2d7b:

constraints is an object specifying the types of media to request, and any requirements for each type.

The constraint parameter is an object with two members: video and audio, describing the requested media type. Either or both must be specified. If the browser cannot find all media tracks of the specified type that meet the given constraints, the returned Promise is rejected by NotFoundError DOMException .

If true is specified for a media type, the resulting stream needs to contain tracks of that type. If for any reason one cannot be returned, a call to getUserMedia() will result in an error.

While information about the user's camera and microphone cannot be accessed for privacy reasons, an application can use additional constraints to request the camera and microphone capabilities it needs and wants. The following represent preferences for 1280x720 camera resolution:

{
  audio: true,
  video: { width: 1280, height: 720 }
}

If no camera of this resolution or higher exists, the returned Promise will be rejected with an OverconstrainedError and the user will not be prompted.


注销
1k 声望1.6k 粉丝

invalid