All Products
Search
Document Center

Mobile Platform as a Service:Advanced guide

Last Updated:Oct 30, 2023

Window scan code means using the scan code function under the old standard UI. If you need to use the full-screen code scanning function that supports multi-code recognition, please upgrade the mPaaS baseline version to 10.1.68.33 or above.

Use the Scan function in the standard UI

Scan code in full screen

If you need to scan the code continuously, that is, continue to recognize without exiting after the code is scanned successfully, you can implement it according to the following code.

ScanRequest scanRequest = new ScanRequest();
        MPScan.startMPaasScanFullScreenActivity(this, scanRequest, new MPScanCallbackAdapter() {
            @Override
            public boolean onScanFinish(Context context, MPScanResult mpScanResult, final MPScanStarter mpScanStarter) {
                new android.app.AlertDialog.Builder(context)
                        .setMessage(mpScanResult != null ? mpScanResult.getText() : "No code recognized")
                        .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mpScanStarter.restart();
                            }
                        })
                        .create()
                        .show();
                // Returning false means that the callback is not consumed, and the callback will continue for the next recognition.
                return false;
            }
        });

Override other methods of MPScanCallbackAdapter to monitor other events:

MPScan.startMPaasScanFullScreenActivity(this, scanRequest, new MPScanCallbackAdapter() {
    @Override
    public boolean onScanFinish(final Context context, MPScanResult mpScanResult, final MPScanStarter mpScanStarter) {
        return true;
    }

    @Override
    public boolean onScanError(Context context, MPScanError error) {
        // recognition error
        return super.onScanError(context, error);
    }

    @Override
    public boolean onScanCancel(Context context) {
        // recognition cancelled
        return super.onScanCancel(context);
    }
});

Before starting the full-screen code scanning function, you can set the startup parameters according to the following code.

ScanRequest scanRequest = new ScanRequest();

// Set the prompt text
scanRequest.setViewText("prompt text");

// Set the prompt text for turning on the torch
scanRequest.setOpenTorchText("turning on the torch");

// Set the prompt text for turning off the torch
scanRequest.setCloseTorchText("turning off the torch");

// Set the code recognition type
// This setting is only valid when a code is scanned directly. It is invalid when an album picture needs to be recognized.
scanRequest.setRecognizeType(
    ScanRequest.RecognizeType.QR_CODE,    // QR code
    ScanRequest.RecognizeType.BAR_CODE,   // Barcode
    ScanRequest.RecognizeType.DM_CODE,    // DM code
    ScanRequest.RecognizeType.PDF417_Code // PDF417 code
); // If not set, the first three types are scanned by default.

// Set the Hide Album button
scanRequest.setNotSupportAlbum(true);

// Set a multi-code tagged image
scanRequest.setMultiMaMarker(R.drawable.green_arrow);

// Set a multi-code tagged text

Window scan

When using the window scan function, you can set the start-up parameters according to the following code snippet.

ScanRequest scanRequest = new ScanRequest();

// Set the UI style of the scan page.
scanRequest.setScanType(ScanRequest.ScanType.QRCODE); // The style of the QR code.
scanRequest.setScanType(ScanRequest.ScanType.BARCODE); // The default style of the barcode.

// Set the scan page title
scanRequest.setTitleText("Standard Scan")

// Set the prompt text under the scan window.
scanRequest.setViewText("Prompt Text");

// Set the prompt text for turning on the torch, for baseline 10.1.60 and later versions only.
scanRequest.setOpenTorchText("Turn on the torch");

// Set the prompt text for turning off the torch, for baseline 10.1.60 and later versions only.
scanRequest.setCloseTorchText("Turn off the torch");

// Set the code recognition type, for the baseline 10.1.60.6 and later veresions and 10.1.68.2 and later veresions.
// This setting is only valid when a code is scanned directly. It is invalid when an album picture needs to be recognized.
scanRequest.setRecognizeType(
    ScanRequest.RecognizeType.QR_CODE, // QR code
    ScanRequest.RecognizeType.BAR_CODE, // Barcode
    ScanRequest.RecognizeType.DM_CODE, // DM code
    ScanRequest.RecognizeType.PDF417_Code // PDF417 code
); // If not set, the first three types are scanned by default.

// Set the transparent status bar (valid on Android 4.4 and later versions), for the baseline 10.1.68.15 and later versions only.
scanRequest.setTranslucentStatusBar(true);

// Set the Hide Album button, for the baseline 10.1.68.22 and later versions only.
scanRequest.setNotSupportAlbum(true);

Use the Scan component in the custom UI

See Sample code.

Upgrade adaptation in the custom UI

  • Since baseline 10.2.3.35, the Scan SDK adds class MPCustomScanView and related APIs to replace the original APIs such as MPScanner,which was previously used for customizing the scan feature. Compared with MPScanner, the solution of using MPCustomScanView encapsulates the core processes of the scan service such as camera management, code recognition, multi-code recognition, screen zoom in and out, and code result analysis. You don't need to pay attention to related operations when developing, just focus on implementing your customized UI in MPCustomScanView. You can still continue to use MPScanner, but the solution will no longer be maintained and you will not be able to obtain feature updates consistent with the full-screen UI (such as multi-code recognition) in subsequent upgrades. It is recommended that you switch to the MPCustomScanView solution to implement custom UI when the time is right. This solution will remain consistent with the full-screen UI in subsequent feature upgrades.

  • Since baseline 10.1.68.5 and 10.1.60.11, the Scan SDK adds class MPScanner and related APIs to replace the original APIs such as BQCScanCallback and MaScanCallback. These two APIs were previously used for customizing the scan feature. Compared with the original APIs, MPScanner provides complete encapsulation, easy-to-use APIs, and support for more new features such as callbacks for insufficient environmental brightness. If you are still using original APIs such as BQCScanCallback and MaScanCallback, you may need to adapt the following changes when you upgrade from an earlier version:

    • Version 10.1.68.22: Some APIs are added for the MaScanCallback class, BQCScanCallback class, and IOnMaSDKDecodeInfo class. You only need to handle these APIs with empty implementation, among which “false” is returned for the MaScanCallback.onMaCodeInterceptor method.

    • Version 10.1.60.6: Some APIs are added for the BQCScanCallback class. You only need to handle these APIs with empty implementation.

    • Version 10.1.60: Some APIs are added for the BQCScanCallback class. You only need to handle these APIs with empty implementation.

    • Version 10.1.20: For MaScanCallback class, the interface void onResultMa(MaScanResult maScanResult) changed to void onResultMa(MultiMaScanResult multiMaScanResult). You can obtain MaScanResult in the following code snippet:

    MaScanResult maScanResult = multiMaScanResult.maScanResults[0];

API description for the custom UI

MPCustomScanView

To useMPCustomScanView, you need to let Activity inherit MPaasToolsCaptureActivity, implement the getCustomScanView method and return the customized MPCustomScanView.

public class MyScanActivity extends MPaasToolsCaptureActivity {

    private MyScanView myScanView;

    @Override
    protected MPCustomScanView getCustomScanView() {
        myScanView = new MyScanView(this);
        // For details, please refer to the github code example.
        return myScanView;
    }
  
}

In MPCustomScanView you can implement or call the following methods:

/**
 * Scan start callback
 */
public void onStartScan();

/**
 * Callback for camera first frame display
 * 
 * There is no guarantee which one will be executed first between this method and the scan start callback.
 */
public void onPreviewShow();

/**
 * Scan end callback
 */
public void onStopScan();

/**
 * Grayscale value callback of camera frame
 * Each frame during the scanning process will be called back once.
 * 
 * @param gray The average gray value can be used to measure the brightness of the environment
 */
public void onGetAvgGray(int gray);

/**
 * Callback for successful scanning (code recognized)
 * 
 * @param context Current context
 * @param list Recognized code result
 */
public abstract void onScanFinished(Context context, List<MPScanResult> list);

/**
 * Scan failed callback
 * 
 * @param context Current context
 * @param list Reason for failure
 */
public abstract void onScanFailed(Context context, MPScanError error);

/**
 * Callback for failure to open camera
 */
public void onCameraOpenFailed();

/**
 * Turn flash on or off
 * 
 * @return The status of the flash after calling this method
 */
public boolean switchTorch();

/**
 * Identification code from file
 * 
 * @param path File path
 * @return Recognized code result
 */
public List<MPScanResult> scanFromPath(String path);

MPScanResult

/**
 * Recognize result strings
 */
private String text;

/**
 * Recognized code type
 */
private MPRecognizeType mpRecognizeType;

/**
 * The coordinates of the center point of the recognized code
 */
private Point centerPoint;

MPScanner (abandoned)

The settings related to the custom UI are as follows:

/**
 * Set View to display the camera content.
 * It is recommended to call in the onConfiguration method of {@link MPScanListener}.
 *
 * @param textureView Customize TextureView in the scan page.
 */
public void setDisplayView(TextureView textureView);

/**
 * Set the area to be scanned.
 * 
 * @param rect The recognized area.
 */
public void setScanRegion(Rect rect);

/**
 * Set the scan listener.
 */
public void setMPScanListener(MPScanListener mpScanListener);

/**
 * Set the listener for identifying the gray value of the image.
 */
public void setMPImageGrayListener(MPImageGrayListener mpImageGrayListener);

/**
 * Obtain the Camera object.
 * 
 * @return Camera object.
 */
public Camera getCamera();

/**
 * Set the code type to be scanned.
 * Only valid for direct scan. Invalid for scanning a code from the bitmap.
 *
 *
 * @param recognizeTypes BAR_CODE Barcode;
 *                       QR_CODE QR code;
 *                       DM_CODE DM code;
 *                       PDF417_CODE PDF417 code;
 *                       If not set, the first three types are scanned by default.
 */
public void setRecognizeType(MPRecognizeType... recognizeTypes);

The scan content related to the custom UI is as follows:

/**
 * Open the camera and start scanning.
 * 
 * Call the API when entering the page for the first time or the camera exits.
 */
public void openCameraAndStartScan();

/**
 * Open the camera and stop scanning.
 */
public void closeCameraAndStopScan();

/**
 * Start scanning.
 * 
 * The camera state will not be changed. The invocation of this method takes effect only when the camera is turned on.
 */
public void startScan();

/**
 * Stop scanning.
 *
 * The camera state will not be changed.
 */
public void stopScan();

/**
 * Scan the code from the bitmap.
 *
 * @param bitmap The bitmap to be scanned.
 * @return: The scan result.
 */
public MPScanResult scanFromBitmap(Bitmap bitmap);

Others:

/**
 * Turn on or off the torch.
 *
 * @return Whether the torch is turned on after the method is called.
 */
public boolean switchTorch();

/**
 * Turn on the torch.
 */
public void openTorch();

/**
 * Turn off the torch.
 */
public void closeTorch();

/**
 * Play the default "beep" sound.
 */
public void beep();

/**
 * Release the resource.
 * 
 * Call in onDestroy.
 */
public void release();

MPScanListener (abandoned)

/**
 * Scanning parameter configuration completed.
 */
void onConfiguration();

/**
 * Scanning starts.
 */
void onStart();

/**
 * Scanned.
 *
 * @param result The scan result.
 */
void onSuccess(MPScanResult result);

/**
 * Scan error.
 *
 * @param error The error.
 */
void onError(MPScanError error);

MPImageGrayListener (abandoned)

/**
 * Obtain the average gray value of the recognized image.
 *
 * The normal range is from 50 to 140, 
 * When the gray value is lower or higher than the normal range, it usually means that the ambient brightness is too low or too high. The user can be prompted to turn on or off the torch.
 * Note: This method will be called continuously during the scan process.
 *
 * @param gray The average gray value of the image.
 */
void onGetImageGray(int gray);