All Products
Search
Document Center

Scan code

Last Updated: Feb 18, 2021

This operation is used to call the scan component. It applies only to Android. Before using this interface, make sure you have added Scan component to your project. actionType in the interface is used to get code value.

scan API usage instruction

 
  1. AlipayJSBridge.call('scan', {
  2. type: 'bar',
  3. actionType: 'scan'
  4. }, function(result) {
  5. alert(JSON.stringify(result));
  6. });

Code sample

Obtain QR code information:

 
  1. <h1>Click the information corresponding to the output code after scanning.</h1>
  2. <a href="#" class="btn read">Start scanning.</a>
  3. <script>
  4. function ready(callback) {
  5. // Call JS Bridge if it has been injected.
  6. if (window.AlipayJSBridge) {
  7. callback && callback();
  8. } else {
  9. // Listen to the injection event if it has not been injected.
  10. document.addEventListener('AlipayJSBridgeReady', callback, false);
  11. }
  12. }
  13. ready(function() {
  14. document.querySelector('.read').addEventListener('click', function() {
  15. AlipayJSBridge.call('scan', {
  16. type: 'qr'
  17. }, function(result) {
  18. alert(JSON.stringify(result));
  19. });
  20. });
  21. });
  22. </script>

API

 
  1. AlipayJSBridge.call('scan', {
  2. type, actionType, qrcode
  3. }, fn);

Input parameters

Name Type Description Required Default value
type string Target scan type, QR code or bar code. Y “”
actionType string Operation type: “scan”: scan only the code value. N “scan”
qrcode string Indicates the code value used for the “route” operation. N “”
fn function Callback function after scanning the QR code to obtain code information. N -

Output parameters

Parameter carried in the callback function: result: {error, barCode, qrCode, cardNumber}.

Name Type Description
barCode string Bar code data obtained through scan.
qrCode string QR code data obtained through scan.
error int Error code (10: operation canceled, 11: operation failed).