In Customized View mode, you do not need to worry about developing the scanning process or camera control. Scan Kit will do all these tasks for you. However, you will need to design the scanning UI. This can also be easily completed based on sample code.
Before using this mode, ensure that hardware acceleration is not disabled for your app. That is, ensure that the value of hardwareAccelerated is true. Otherwise, the black screen may occur on some models.
The process of using Scan Kit in Customized View mode is as follows.
For details, please refer to Custom Elements for the Scanning UI in Customized View Mode.
In the preview stream, Scan Kit recognizes only the image in the scanning area. (To better magnify the captured image, it is recommended that the scanning box be placed in the middle of the screen and be slightly smaller than the scanning area.)
Scan Kit that supports only certain barcode formats will scan faster. For example, if only the QR code and DataMatrix code need to be detected, use the setFormat(HmsScan.QRCODE_SCAN_TYPE, HmsScan.DATAMATRIX_SCAN_TYPE) method to set Scan Kit to support only the two types of barcodes. If scanning barcodes in all formats supported by Scan Kit is acceptable for you, you do not need to call the setFormat method to specify the barcode format.
Java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_defined);
// Bind the camera preview layout.
frameLayout = findViewById(R.id.rim);
// Set the scanning area. Set the parameters as required.
DisplayMetrics dm = getResources().getDisplayMetrics();
float density = dm.density;
mScreenWidth = getResources().getDisplayMetrics().widthPixels;
mScreenHeight = getResources().getDisplayMetrics().heightPixels;
// Set the width and height of the barcode scanning box to 300 dp.
final int SCAN_FRAME_SIZE = 300;
int scanFrameSize = (int) (SCAN_FRAME_SIZE * density);
Rect rect = new Rect();
rect.left = mScreenWidth / 2 - scanFrameSize / 2;
rect.right = mScreenWidth / 2 + scanFrameSize / 2;
rect.top = mScreenHeight / 2 - scanFrameSize / 2;
rect.bottom = mScreenHeight / 2 + scanFrameSize / 2;
// Initialize the remote view. Use setContext() to pass the context (mandatory), use setBoundingBox() to set the scanning area, and use setFormat() to set the barcode format. Then call the build() method to create the remote view. Set the non-consecutive scanning mode by using the setContinuouslyScan method (optional).
remoteView = new RemoteView.Builder().setContext(this).setBoundingBox(rect).setContinuouslyScan(false).setFormat(HmsScan.QRCODE_SCAN_TYPE, HmsScan.DATAMATRIX_SCAN_TYPE).build();
// Load the customized view to the frameLayout of the activity.
remoteView.onCreate(savedInstanceState);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
frameLayout.addView(remoteView, params);
} Kotlin
override fun onCreate(savedInstanceState:Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_defined)
// Bind the camera preview layout.
frameLayout = findViewById(R.id.rim)
// Set the scanning area. Set the parameters as required.
val dm = getResources().getDisplayMetrics()
val density = dm.density
mScreenWidth = getResources().getDisplayMetrics().widthPixels
mScreenHeight = getResources().getDisplayMetrics().heightPixels
// Set the width and height of the barcode scanning box to 300 dp.
val SCAN_FRAME_SIZE = 300
val scanFrameSize = (SCAN_FRAME_SIZE * density).toInt()
val rect = Rect()
rect.left = (mScreenWidth / 2 - scanFrameSize / 2).toInt()
rect.right = (mScreenWidth / 2 + scanFrameSize / 2).toInt()
rect.top = (mScreenHeight / 2 - scanFrameSize / 2).toInt()
rect.bottom = (mScreenHeight / 2 + scanFrameSize / 2).toInt()
// Initialize the remote view. Use setContext() to pass the context (mandatory), use setBoundingBox() to set the scanning area, and use setFormat() to set the barcode format. Then call the build() method to create the remote view. Set the non-consecutive scanning mode by using the setContinuouslyScan method, which is optional.
remoteView = RemoteView.Builder().setContext(this).setBoundingBox(rect).setContinuouslyScan(false).setFormat(HmsScan.QRCODE_SCAN_TYPE, HmsScan.DATAMATRIX_SCAN_TYPE).build()
// Load the customized view to the frameLayout of the activity.
remoteView.onCreate(savedInstanceState)
val params = FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
frameLayout.addView(remoteView, params)
} Java
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Subscribe to the recognition result callback event.
remoteView.setOnResultCallback(new OnResultCallback() {
@Override
public void onResult(HmsScan[] result) {
// Obtain the scanning result object HmsScan.
...
}
});
} Kotlin
override
fun onCreate(savedInstanceState:Bundle?) {
…
// Subscribe to the recognition result callback event.
remoteView?.setOnResultCallback(OnResultCallback {
// Obtain the scanning result object HmsScan.
...
})
} Java
@Override
protected void onStart() {
super.onStart();
// Listen to the onStart method of the activity.
remoteView.onStart();
}
@Override
protected void onResume() {
super.onResume();
// Listen to the onResume method of the activity.
remoteView.onResume();
}
@Override
protected void onPause() {
super.onPause();
// Listen to the onPause method of the activity.
remoteView.onPause();
}
@Override
protected void onStop() {
super.onStop();
// Listen to the onStop method of the activity.
remoteView.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
// Listen to the onDestroy method of the activity.
remoteView.onDestroy();
} Kotlin
override
protected fun onStart() {
super.onStart()
// Listen to the onStart method of the activity.
remoteView?.onStart()
}
override
protected fun onResume() {
super.onResume()
// Listen to the onResume method of the activity.
remoteView?.onResume()
}
override
protected fun onPause() {
super.onPause()
// Listen to the onPause method of the activity.
remoteView?.onPause()
}
override
protected fun onStop() {
super.onStop()
// Listen to the onStop method of the activity.
remoteView?.onStop()
}
override
protected fun onDestroy() {
super.onDestroy()
// Listen to the onDestroy method of the activity.
remoteView?.onDestroy()
} Java
protected void onCreate(Bundle savedInstanceState) {
...
// Call back this API under dim lighting to control the flashlight button.
flushBtn = findViewById(R.id.flush_btn);
remoteView.setOnLightVisibleCallback(new OnLightVisibleCallBack() {
@Override
public void onVisibleChanged(boolean visible) {
if(visible){
flushBtn.setVisibility(View.VISIBLE);
}
}
});
...
} Kotlin
protected fun onCreate(savedInstanceState:Bundle?) {
…
// Call back this API under dim lighting to control the flashlight button.
flushBtn = findViewById(R.id.flush_btn)
remoteView?.setOnLightVisibleCallback(OnLightVisibleCallBack{
if(it){
flushBtn?.setVisibility(View.VISIBLE)
}
})
…
}
Pausing barcode scanning will reset the camera's focal length. For example, if the focal length is adjusted to zoom in, it will be restored to the initial state after the barcode scanning is paused and then resumed.
Java
// Set setContinuouslyScan to false to enable non-consecutive scanning.
remoteView = new RemoteView.Builder().setContext(this).setContinuouslyScan(false).build(); Kotlin
// Set setContinuouslyScan to false to enable non-consecutive scanning.
remoteView = RemoteView.Builder().setContext(this).setContinuouslyScan(false).build() Java
remoteView = new RemoteView.Builder().setContext(this).enableReturnBitmap().build(); Kotlin
remoteView = RemoteView.Builder().setContext(this).enableReturnBitmap().build()
If the original image submitted by the user needs to be returned after code scanning, notify the user. In addition, you must comply with local laws and regulations.