Automate Custom Image Using Android Emulator Virtual Scene Camera

M Habib
Blibli.com Tech Blog
3 min readJan 12, 2023
Photo by Albert Hu on Unsplash

Getting Started

Okay, 3.. 2.. 1. let's begin
This is my first medium article. How bad am I as a writer? Starting an article with a disclaimer HAHAA, but please read it.

Disclaimer...
I'm giving an example using a QR code as a custom image and Java as language.
No guarantee this is the best practice, but this method marked my Jira ticket as completed.
Make it clap and share the article… don't be shy to psst psssst on the comment section.

Getting Started(2)

This story begins with my teammate on mobile QA automation facing an issue with his automation. There are some scenarios regarding the camera. One of them is scanning a QR code. Yess, we thought also using 2 devices, so the first device opened the QR, and the other opened the camera by focusing on the first device, but this is not a great thing we should do, so after diving into Mr. Google and Mrs. Stackoverflow, I found some bright sign using this method. LET'S BEGIN!

Output

The output of this implementation.

It will show an image on a virtual scene directly without moving to the board or seeing it on the table (default), and the image is replaceable by calling a single method using code realtime.

Implementation

Make sure you check on your local for ANDROID_HOME or ANDROID_SDK_ROOT. It will return the path to your SDK directory.

echo $ANDROID_HOME
/Users/username/Library/Android/sdk

Make sure you already have an android emulator on your system.

Change the content of this file

%ANDROID_HOME%/emulator/resources/Toren1BD.posters

With this:

poster custom
size 1 1
position 0 0 -1.5
rotation 0 0 0
default your-image.png

Next, on the emulator setting, we need to change the primary camera using VirtualScene.

  1. on Device Manager in Android
  2. edit this AVD
  3. show advanced settings
  4. camera — back — select the VirtualScene
  5. finish

Are you still there? COOL will continue with the java method

public static void setVirtualImage(Path path, String fileName) throws Exception {
String dir = "";
if (!Files.isDirectory(path)) {
throw new IllegalArgumentException("Path must be a directory!");
}
File imageFile = Files.walk(path)
.filter(Files::isReadable)
.filter(Files::isRegularFile)
.filter(p -> p.getFileName().toString().equalsIgnoreCase(fileName))
.map(Path::toFile)
.findFirst()
.orElse(null);
if (imageFile != null || Files.probeContentType(imageFile.toPath()) != null
&& Files.probeContentType(imageFile.toPath()).split(File.separator)[1].equals("png")) {
if (System.getenv("ANDROID_SDK_ROOT") != null)
dir = System.getenv("ANDROID_SDK_ROOT");
else if (System.getenv("ANDROID_HOME") != null)
dir = System.getenv("ANDROID_HOME");
else
throw new Exception("System environment not found.");

ImageIO.write(ImageIO.read(imageFile),
"png",
new File(
dir + File.separator + "emulator" + File.separator + "resources" + File.separator
+ "your-image.png"));
} else {
throw new Exception("File is not a png image.");
}
}

This one is the only method we need, call the method, and it will automatically change the image on your emulator VirtualScene.

setVirtualImage(Paths.get("src/test/resources/"), "your-image.png");

Paths.get(“src/test/resources/”) — is the location of your custom image file
your-image.png — is you custom image file name

your-image.png
your-image.png

--

--