This codelab guides you through changing the color, initial angle, and scaling of the AR logo by modifying the source code in the demo.
HUAWEI AR Engine is a platform for building augmented reality apps on Huawei smartphones. WorldAR is a subsystem of the HUAWEI AR Engine and allows your app to:
In this codelab, you will create an app that is able to scan the surrounding environment using the camera, identify the plane in the environment, place a virtual object on the plane, and select and drag the object.
In this codelab, you will learn how to:
Apply for the camera permission for the app.
<!--Declare the camera permission.-->
<uses-permission android:name="android.permission.CAMERA" />
adb devices
This will connect to your devices and output the device serial number.
Add the interface for updating the scaling factor and rotation angle to world/rendering/VirtualObject.java and provide the updated values before rendering.
public void updateScaleFactor(float scaleFactor){
// The scaling factor is set externally based on the 0.15f scaling.
mScaleFactor = 0.15f * scaleFactor;
}
public void updateRotation(float angle) {
// Set the rotation angle externally.
mRotationAngle = angle;
}
public float[] getModelAnchorMatrix() {
float[] modelMatrix = new float[MATRIX_SIZE];
if (mArAnchor != null) {
mArAnchor.getPose().toMatrix(modelMatrix, 0);
} else {
Matrix.setIdentityM(modelMatrix, 0);
}
float[] rets = new float[MATRIX_SIZE];
Matrix.multiplyMM(rets, 0, modelMatrix, 0, mModelMatrix, 0);
// Rotate the model around the y axis at a specified angle.
Matrix.rotateM(rets, 0, mRotationAngle, 0f, 1f, 0f);
// Scale the x, y, and z dimensions of the model according to the specified factor.
if (mScaleFactor != 0) {
Matrix.scaleM(rets, 0, mScaleFactor, mScaleFactor, mScaleFactor);
}
return rets;
}
Two SeekBars are added to the main interface of the app to control the scaling and rotation parameters respectively. Add the following code to the res/layout/world_java_activity_main.xml file.
<SeekBar
android:id="@+id/scaleSeekBar"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:min="1"
android:max="400"
android:progress="150"/>
<SeekBar
android:id="@+id/rotationSeekBar"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_below="@id/scaleSeekBar"
android:layout_alignParentEnd="true"
android:min="0"
android:max="360"
android:progress="180"/>
Obtain the corresponding SeekBar from world/rendering/WorldRenderManager.java and set it to the selected model. The implementation code is as follows.
public WorldRenderManager(Activity activity, Context context) {
mActivity = activity;
mContext = context;
mTextView = activity.findViewById(R.id.wordTextView);
mSearchingTextView = activity.findViewById(R.id.searchingTextView);
// Set the rotation and scaling factors.
initSeekBar(activity);
}
private void initSeekBar(Activity activity) {
mScaleSeekBar = activity.findViewById(R.id.scaleSeekBar);
mScaleSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mSelectedObj != null) {
mSelectedObj.updateScaleFactor(progress / (float) 150);
}
}
...
});
mRotationSeekBar = activity.findViewById(R.id.rotationSeekBar);
mRotationSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mSelectedObj != null) {
mSelectedObj.updateRotation(progress);
}
}
...
});
}
Run the program in Android Studio and select your physical device. Move your device to scan the surrounding environment. After a plane is detected, touch the plane to add virtual objects to the real world. You can also select the virtual object and drag the SeekBar on the page to have the model zoomed in or out and rotated.
Well done. You have successfully completed this codelab and learned how to:
You can download the sample code project from Sample Code and extend it.