Skip to main content
Version: 4.1.4

Examples

Region of interest and Camera orientation

This example can be used to scan DOCUMENT in a portrait application:

Display display = getActivity().getWindowManager().getDefaultDisplay();
int stageWidth = display.getWidth();
int stageHeight = display.getHeight();
int width = stageWidth, height = stageHeight;
if(width > height)
// Set new ROI
mSmartScanner.setRegionOfInterest(new Rect(height / 100 * 15, width / 10, width / 100 * 95, height / 100 * 80));
else {
/**
* Set the new orientation decoding on the device. By activating this mode you must manage the ROI manually.
* @param forceOrientationEnabled true if the decoder should decode in manual orientation
* @param scanOrientation the orientation for decoding.The options are: LANDSCAPE_MODE, LANDSCAPE_REVERSE_MODE, PORTRAIT_MODE, PORTRAIT_REVERSE_MODE
*
*/
mSmartScanner.setOrientationDecoding(true, SmartScanner.LANDSCAPE_MODE);
mSmartScanner.setRegionOfInterest(new Rect(height / 100 * 10, width / 100 * 15, width / 100 * 85, height / 100 * 90));
}

Flash

This example shows how to enable/disable the flash using a toggle button:

...
someToggleButton.setOnCheckedChangeListener(flashModeLister);
...

private CompoundButton.OnCheckedChangeListener flashModeLister = new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mSmartScanner.setFlashEnabled(isChecked);
}
};

Auto-Focus

This example shows how to enable/disable the auto-focus using a toggle button:

...
someToggleButton.setOnCheckedChangeListener(focusModeLister);
...

private CompoundButton.OnCheckedChangeListener focusModeLister = new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mSmartScanner.setAutoFocusEnabled(isChecked);
}
};


Manual focus

This example shows how to manually focus by double-tapping on the screen.

Add a new rectangle shape to the drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#00000000" />
<stroke
android:width="1dp"
android:color="#8BC34A" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>

Add a focus view to the main layout:

<View
android:id="@+id/focus"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"
android:background="@drawable/rectangle" />

Switch to manual focus on double-tap event:

/**
* @param x Position along the x-axis on the screen
* @param y Position along the y-axis on the screen
* @param rectangleView View where the focus rectangle will be drawn
* @param isFocusRectangleVisible Option to show or hide the focus rectangle
*/

mSmartScanner.manualFocus(x, y, rectangleView, isFocusRectangleVisible);

Fixed focus

The setFixedFocusDistance function allows you to fix the focus at a specific distance.

  • The value passed as a parameter is expressed in centimeters.
  • If the value is too low, it will automatically be adjusted to the device’s minimum supported focus distance.
  • A null value disables the fixed focus.
    /**
* Forces the camera lens to focus at a fixed distance.
* <p>
* This disables autofocus and locks the lens focus to the specified distance,
* which is useful for close-range use cases such as document or card scanning.
* </p>
*
* @param distanceCm The desired focus distance in centimeters.
* Pass null to disable fixed focus.
*/
public void setFixedFocusDistance(Float distanceCm)

The following example shows how to enable or disable fixed camera focus at a specific distance:

...
someToggleButton.setOnCheckedChangeListener(fixedFocusLister);
...

private CompoundButton.OnCheckedChangeListener fixedFocusLister = new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
mSmartScanner.setFixedFocusDistance(10f);
} else {
mSmartScanner.setFixedFocusDistance(null);
mSmartScanner.setAutoFocusEnabled(true);
}
}
};


Hight resolution

This example shows how to enable/disable the hight-resolution with a toggle button:

...
someToggleButton.setOnCheckedChangeListener(focusModeLister);
...

private CompoundButton.OnCheckedChangeListener focusModeLister = new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mSmartScanner.setHighResolution(isChecked);
}
};


Capture

This example shows how to capture the image seen by the decoder.

Add a view to display the image in the main layout:

<ImageView
android:id="@+id/picture_view"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_gravity="center"/>
</LinearLayout>

Implement the OnSavedPictureReadyListener listener:

mSmartScanner.setOnSavedPictureReadyListener(pictureBytes -> {
if (pictureBytes != null) {
Bitmap bmp = BitmapFactory.decodeByteArray(pictureBytes, 0, pictureBytes.length);
((ImageView) findViewById(R.id.picture_view)).setImageBitmap(bmp);
}
});

Enable automatic capture when the image is successfully decoded:

/**
* If enabled, each successfully decoded image is saved and returned via the OnSavedPictureReadyListener listener
* @param enabled Set the state of the returned image
* @param onlyROI Set the area of the picture capture
*/
mSmartScanner.setOnLoadedListener(roi -> {
mSmartScanner.setSaveDecodedPictureEnabled(enabled, onlyRoi);
});

Or trigger a capture whenever you need it:

/**
* Take a picture of the scanning process
* @param onlyROI Set the area of the picture capture
*/
mSmartScanner.savePicture(onlyRoi)

Frame rate

This example shows how to set the ratio between decoded frames and ignored frames. By default, all images are decoded, but you can reduce energy consumption by adjusting this ratio.


/**
* Set ratio between processed frames and skipped frames
* By default all frames are processed
* @param processedFrameNumber number of successive frames send to decoder
* @param skippedFrameNumber number of successive frames skipped
*/
mSmartScanner.setOnLoadedListener(roi -> {
mSmartScanner.setProcessedFrameRate(int processedFrameNumber, int skippedFrameNumber)
});