Talk to us
Talk to us
menu

How to Choose Avatar SDK for Developers

How to Choose Avatar SDK for Developers

The Avatar market is a trendy market of gaming, virtual reality, and augmented reality applications. Therefore, the demand for avatar SDK is growing as more industries adopt virtual and augmented reality technologies. This article will show you how to choose a professional avatar SDK to build your own avatar.

What is Avatar SDK

Avatar SDK is a software development kit (SDK) that helps developers create realistic 3D avatars from real-life photos or videos. It uses advanced algorithms and machine learning techniques to generate 3D models of human faces that can be used in virtual and augmented reality applications, video games, and other digital experiences.

It is available on various platforms, including iOS, Android, Windows, and Unity, making it accessible to developers working on a wide range of projects. With Avatar SDK, developers can easily create immersive avatar experiences that allow users to interact with virtual characters more naturally and intuitively.

Whether it’s a video game, a virtual reality application, or an augmented reality experience, Avatar SDK provides the tools and technology needed to bring digital characters to life in a previously impossible way.

How to Create a Virtual Avatar with ZEGOCLOUD

Avatar SDK from ZEGOCLOUD is a powerful tool that allows you to create your virtual avatar. With the SDK, you can choose from various pre-made avatars or create your own from scratch. You can also customize your avatar’s appearance, including hair, skin color, and clothing.

Once you have created your avatar, you can use it in various applications, such as video chat, live streaming, and gaming. Let’s dive in and explore the exciting process of creating our avatars using this incredible SDK!

Preparation

  • A ZEGOCLOUD developer account – Sign up
  • Create a project in the ZEGOCLOUD admin console to get app credentials.
  • An updated version of Android Studio
  • Android device or emulator (device recommended)
  • Fundamentals of Android app development

If you have completed the above preparation steps, you can move on to the following steps to develop your virtual avatars.

Create a project

  1. To kickstart your project creation, start by opening Android Studio. Access the “File” menu, then select “New” and click on “New Project.”
  1. Afterward, provide a name for your project and select a storage location for the project files.
  1. Feel free to keep the remaining fields as their default settings. Finally, click on the “Next” and “Finish” buttons to complete the creation of your project.

Import the SDK

  1. To begin, visit the SDK Downloads section and obtain the latest version of the SDK.
  1. Afterward, unpack the SDK package and move the ZegoAvatar.aar file from the package to a suitable project directory, such as app/libs.
  1. Next, access the app folder and locate the build.gradle file. Open it and include all the JAR files in the folder in the dependencies section.
    implementation fileTree(dir: 'libs', include: ['*.jar', "*.aar"]) //Wildcard introduction

Set permissions

Tailor the permissions to suit your application’s specific needs. Locate the AndroidManifest.xml file situated in the app/src/main directory and modify it by including the necessary permissions. This allows your application to function smoothly and securely, aligning with its unique requirements.

<!-- Permissions required by the SDK -->

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<!-- Some permissions required by the app -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

To prevent the SDK public class names from being obfuscated, add the following configuration to the proguard-rules.pro file:

-keep class **.zego.**{*;}

Import resource packages

Before using the AI capabilities of the ZegoAvatar SDK, you must import the appropriate resource packages. You can download the resource packages from the Download section. Once you have downloaded the resource packages, you can import them using the dynamic downloading method or by adding them from local storage.

To add the resource packages from local storage, follow these steps:

  1. Go to the SDK Downloads section and download the corresponding resource packages.
  2. Extract the downloaded resource packages and locate the assets folder.
  1. Copy the assets folder to the assets directory of your project.
  2. While running your project, use the following code to copy the AIModel.bundle, base.bundle, and Packages files to the private directory (/data/data/package_name/files) of the device.
AssetsFileTransfer.copyAssetsDir2Phone(this.getApplication(),
        "AIModel.bundle"/*The assets root directory in APK.*/, "assets"/* Directory in the SD card. The value is: getFilesDir().getAbsolutePath() + File.separator + destPath. */);
AssetsFileTransfer.copyAssetsDir2Phone(this.getApplication(),
        "base.bundle", "assets");
AssetsFileTransfer.copyAssetsDir2Phone(this.getApplication(),
        "Packages", "assets");

/*
Copy the entire contents of the assets/${filePath} directory to ${destPath}/ in the mobile phone's storage directory. This method requires an activity parameter and accepts file paths for the source (e.g., AIModel.bundle) and destination directories (e.g., /data/data/package_name/files/assets/).
*/
public static void copyAssetsDir2Phone(Context activity, String filePath, String destPath) {
    try {
        String[] fileList = activity.getAssets().list(filePath);
        if (fileList.length > 0) {//If it is a directory
            File file = new File(activity.getFilesDir().getAbsolutePath() + File.separator + destPath + File.separator + filePath);
            if (file.exists()) {
                deleteAllFiles(file);
            }
            file.mkdirs();//If the folder does not exist, recursion is performed.
            for (String fileName : fileList) {
                filePath = filePath + File.separator + fileName;

                copyAssetsDir2Phone(activity, filePath, destPath);

                filePath = filePath.substring(0, filePath.lastIndexOf(File.separator));
                Log.i(TAG, filePath);
            }
        } else {//If it is a file
            InputStream inputStream = activity.getAssets().open(filePath);
            File file = new File(activity.getFilesDir().getAbsolutePath() + File.separator + destPath + File.separator + filePath);
            if (file.exists()) {
                boolean delete = file.delete();
            }
            if (!file.exists() || file.length() == 0) {
                FileOutputStream fos = new FileOutputStream(file);
                int len = -1;
                byte[] buffer = new byte[1024];
                while ((len = inputStream.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
                fos.flush();
                inputStream.close();
                fos.close();
            }
        }
    } catch (IOException e) {
        Log.e(TAG, "copy file failed, src:" + filePath + " dest:" + destPath);
        e.printStackTrace();
    }
}
  1. The resulting directory structure will vary depending on the specific device after successfully copying the resources and running the project on a mobile device. Note that the directory returned by getFilesDir().getAbsolutePath() may differ. For example, on a Huawei phone, it would be /data/data/im.zego.zegoavatarexample.
  2. To enable a feature, provide the absolute path of the required resource to the corresponding API.

Apply for authentication

Follow these steps for a successful online authentication process, to obtain the license file for the Virtual Avatar:

Enable the ZegoAvatar permission in your project settings.

Generate the AppID and AppSign for your project by creating them in the ZEGOCLOUD Admin Console. To activate the necessary permissions, contact ZEGOCLOUD tech support and provide your project’s package name.

Obtain the reference code:

  1. Include the code from im.zego.zegoavatarexample.licensehelper found in the SDK sample source code in your project.
  2. Integrate the required library files as dependencies.
// Incorporating the referenced code necessitates including these library files as dependencies.


implementation "com.google.code.gson:gson:2.8.8"
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
  1. To ensure the successful execution of the sample source code, it is vital to accurately enter the AppID and AppSign that you obtained. Locate the ZegoAvatarConfig.java file and input the AppID AppSign values accordingly.
public class ZegoAvatarConfig {
    // The address of the authentication server.
    public final static String BACKEND_API_URL = "https://aieffects-api.zego.im?Action=DescribeAvatarLicense";

// ZEGOCLOUD offers an AppID that is associated with the ApplicationId. To utilize it, update the ApplicationId in app/build.gradle with the package name provided during AppID application submission.


    public final static long APP_ID = YOUR_APP_ID;
    // The AppSign is obtained from ZEGOCLOUD.
    public final static String APP_SIGN = YOUR_APP_SIGN;
}
  1. To align the application Id with the provided package name during the AppID application, it is necessary to update the app/build.gradle file. Locate the applicationId field within the file and replace it with the designated package name.

Get the license

Obtain the authentication license by utilizing the getLicense API in ZegoLicenseHelper, which involves initiating a network request.

ZegoLicenseHelper.getLicense(this, (code, message, response) -> {
    // License obtained.
    if (code == 0) {
        String license = response.getLicense();

        // A valid license is necessary for ZegoAvatarService initialization. Incorrect licenses result in the unavailability of related features in the ZegoAvatar SDK.

        ZegoServiceConfig config = new ZegoServiceConfig(license,
                    getFilesDir().getAbsolutePath() + "/assets/AIModel.bundle/");

        // Prior to utilizing the ZegoAvatarService SDK, it is crucial to initiate the asynchronous initialization method. To monitor the SDK's status, employ the addServiceObserver method.

        ZegoAvatarService.init(MainActivity.this.getApplication(), config);
    } else {
        // Failed to obtain the license.
        Toast.makeText(MainActivity.this, "Failed to obtain the license. code: " + code, Toast.LENGTH_LONG).show();
    }
});

Initialize ZegoAvatarService

Before initializing the AvatarService, make sure to import the following code snippets:

import com.zego.avatar.OnRecordStartCallback;
import com.zego.avatar.OnRecordStopCallback;
import com.zego.avatar.ZegoAvatarService;
import com.zego.avatar.ZegoAvatarView;
import com.zego.avatar.bean.ZegoGenderType;
import com.zego.avatar.bean.ZegoAvatarQualityLevel;
import com.zego.avatar.bean.ZegoAvatarServiceState;
import com.zego.avatar.bean.ZegoExpressionDetectMode;
import com.zego.avatar.bean.ZegoModelType;
import com.zego.avatar.bean.ZegoServiceConfig;

After importing the required codes, initiate the AvatarService by calling the init method and providing the obtained authentication license. This step initializes the AvatarService and ensures its proper functioning in your application.

// Ensure resources are copied to an SD card to avoid duplication. Check if resources are already copied before use, and consider downloading them from the Internet.


AssetsFileTransfer.copyAssetsDir2Phone(this.getApplication(),
            "AIModel.bundle"/*assets root directory in apk*/, "assets"/* Directory in the SD card. The value is getFilesDir().getAbsolutePath() + File.separator + destPath. */);
AssetsFileTransfer.copyAssetsDir2Phone(this.getApplication(),
            "base.bundle", "assets");
AssetsFileTransfer.copyAssetsDir2Phone(this.getApplication(),
            "Packages", "assets");
String license = "xxxxxxx"; // The license obtained from the authentication server.
String AIPath = getFilesDir().getAbsolutePath() + "/assets/AIModel.bundle"; // The absolute path of the AI model.
ZegoServiceConfig config = new ZegoServiceConfig(license, AIPath);
ZegoAvatarService.init(AvatarApplication.this, config);

Stay updated on the initialization status of the AvatarService by listening for the onStateChange callback.

ZegoAvatarService.addServiceObserver(new ZegoAvatarServiceDelegate(){
    @Override
    public void onError(ZegoAvatarErrorCode code, String desc) {
        mMainHandler.post(() ->{
            ZALog.e("errorcode : " + code.getErrorCode() + ",desc : " + desc);
        });
    }
    @Override
    public void onStateChange(ZegoAvatarServiceState state) {
        // initialize status callback.
    }
});

Create a virtual avatar

To create a virtual avatar, first initialize an AvatarService object. Then, initialize a ZegoCharacterHelper object and provide the appearance data for the avatar, such as facial features, clothing materials, and makeup. Finally, provide the view parameters for the avatar, such as width, height, and position.

public class AvatarMainActivity extends BaseActivity implements ZegoAvatarServiceDelegate {
    private ZegoAvatarView mZegoAvatarView;
    private IZegoInteractEngine mZegoInteractEngine;
    private ZegoCharacterHelper mCharacterHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // Assuming the layout file is activity_main.xml

        // Pause execution until the initialization process of ZegoAvatarService is finished.
        ZegoAvatarService.addServiceObserver(this);

        mZegoAvatarView = findViewById(R.id.zego_avatar_view);

        // Other initialization code for your activity
    }

    @Override
    public void onStateChange(ZegoAvatarServiceState state) {
        if (state == ZegoAvatarServiceState.InitSucceed) {
            runOnUiThread(() -> {
                // Develop a ZegoCharacterHelper class for streamlined API call implementation.
                mCharacterHelper = new ZegoCharacterHelper(getFilesDir().getAbsolutePath() + "/assets/base.bundle"); // The absolute path of basic resources.

                mCharacterHelper.setExtendPackagePath(getFilesDir().getAbsolutePath() + "/assets/Packages");

                // Specify the resource package directory and set the default avatar for makeup, hair, glasses, and other resources.
                mCharacterHelper.setDefaultAvatar("female");

                // Show the avatar and call the API on the UI thread.
                mCharacterHelper.setCharacterView(mZegoAvatarView);

                /**
                 * Instantiate the facial expression mirroring model and ensure to obtain necessary permissions,
                 * such as camera or voice, from users if needed.
                 **/
                mZegoInteractEngine = ZegoAvatarService.getInteractEngine();
                if (mZegoInteractEngine != null) {
                    // Enable facial expression mirroring.
                    mZegoInteractEngine.startDetectExpression(ZegoExpressionDetectMode.Camera, expression -> {
                        mCharacterHelper.setExpression(expression);
                    });
                }
            });

            ZegoAvatarService.removeServiceObserver(this);
        }
    }
}

Explore the ZegoCharacterHelper Instructions and Virtual Avatar documentation for additional avatar customization options. These valuable resources offer in-depth insights and guidance on expanding your avatar’s customization capabilities.

Talk to Expert

Learn more about our solutions and get your question answered.

Talk to us

Take your apps to the next level with our voice, video and chat APIs

Free Trial
  • 10,000 minutes for free
  • 4,000+ corporate clients
  • 3 Billion daily call minutes

Stay updated with us by signing up for our newsletter!

Don't miss out on important news and updates from ZEGOCLOUD!

* You may unsubscribe at any time using the unsubscribe link in the digest email. See our privacy policy for more information.