[Huawei Developers] John Wo:
After taking a stunning picture, the user may want to use it as their lock screen image, but even more so, to add text art to give the lock screen a flawless magazine cover effect.

Fortunately, Image Kit makes this easy, by endowing your app with the smart layout service, which empowers users to create magazine-reminiscent lock screen images on a whim.

2. Scenario

The smart layout service provides apps with nine different text layout styles, which correspond to the text layouts commonly seen in images, and help users add typeset text to images. By arranging images and text in such a wide range of different styles, the service enables users to relive memorable life experiences in exhilarating new ways.

3. Key Steps and Code for Integration

Preparations

1. Configure the Huawei Maven repository address.

Open the build.gradle file in your Android Studio project.

Go to buildscript > repositories and allprojects > repositories, and add the Maven repository address for the HMS Core SDK.

<p style="line-height: 1.5em;">buildscript {
repositories{
...
maven {url 'http://developer.huawei.com/repo/'}
}
}
allprojects {
repositories {
…
maven { url 'http://developer.huawei.com/repo/'}
}
}
</p>

2. Add the build dependencies for the HMS Core SDK.

Open the build.gradle file in the app directory of your project.

Add build dependencies in the dependencies section and use the Full-SDK of Scene Kit and AR Engine SDK.

<p style="line-height: 1.5em;">dependencies {
….
implementation "com.huawei.hms:image-vision:1.0.3.301"
implementation "com.huawei.hms:image-vision-fallback:1.0.3.301"
}
</p>

3. Add permissions in the AndroidManifest.xml file.

Open the AndroidManifest.xml file in the main directory, and add the relevant permissions above the <application line.

<p style="line-height: 1.5em;"><!—Permissions to access Internet, and read data from and write data to the external storage-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</p>

Development Procedure

1. Layout

The following parameters need to be configured to facilitate smart layout display:

1. title: copywriting title, which is mandatory and can contain a maximum of 10 characters. If the title exceeds 10 characters, extra characters will be forcibly truncated.

2. description: copywriting content, which can contain a maximum of 66 characters. If the content exceeds 66 characters, extra characters will be forcibly truncated and replaced with an ellipsis (...).

3. copyRight: name of the copyright holder, which can contain a maximum of 10 characters. If the name exceeds 10 characters, extra characters will be forcibly truncated and replaced with an ellipsis (...).

4. anchor: link that redirects users to the details, which can contain a maximum of 6 characters. If the link exceeds 6 characters, extra characters will be forcibly truncated and replaced with an ellipsis (...).

5. info: copywriting layout style. You can select one from info1 to info9. info8 represents a vertical layout, which currently applies only to Chinese text. info3 is used by default if the user selects info8 but the input title and text are not in Chinese.

Functions of certain buttons:

INIT_SERVICE: Initializes the service.

SOTP_SERVICE: Stops the service.

IMAGE: Opens an image on the mobile phone.

GET_RESULT: Obtains the typeset image to be displayed.

2. INIT_SERVICE: Initializing the Service

To call setVisionCallBack during service initialization, your app must implement the ImageVision.VisionCallBack API and override its onSuccess(int successCode) and onFailure(int errorCode) methods. The onSuccess method is called after successful framework initialization. In the onSuccess method, the smart layout service will be initialized again.

private void initTag(final Context context) {
// Obtain an ImageVisionImpl object.
imageVisionTagAPI = ImageVision.getInstance(this);
imageVisionTagAPI.setVisionCallBack(new ImageVision.VisionCallBack() {
@Override
public void onSuccess(int successCode) {
int initCode = imageVisionTagAPI.init(context, authJson);
}

@Override
public void onFailure(int errorCode) {
LogsUtil.e(TAG, "getImageVisionAPI failure, errorCode = " + errorCode);
}
});
}

For more details about the format of authJson, please visit Filter Service.

The token is mandatory. For more details about how to obtain a token, please visit How to Obtain a Token.

/**
* Gets token.
*
* @param context the context
* @param client_id the client id
* @param client_secret the client secret
* @return the token
*/
public static String getToken(Context context, String client_id, String client_secret) {
String token = "";
try {
String body = "grant_type=client_credentials&client_id=" + client_id + "&client_secret=" + client_secret;
token = commonHttpsRequest(context, body, context.getResources().getString(R.string.urlToken));
if (token != null && token.contains(" ")) {
token = token.replaceAll(" ", "+");
token = URLEncoder.encode(token, "UTF-8");
}
} catch (UnsupportedEncodingException e) {
LogsUtil.e(TAG, e.getMessage());
}
return token;
}

For more, you can check https://forums.developer.huawei.com/forumPortal/en/topic/0201465106068040043