Search
Loading...
Skip to content

Create From Image

Starting from an existing image allows you to use the editor for customizing individual assets. This is done by using suspend fun createFromImage(imageUri: URI, dpi: Float = 300F, pixelScaleFactor: Float = 1F): DesignBlock and passing a URI as argument. The dpi argument sets the dots per inch of the scene. The pixelScaleFactor sets the display’s pixel scale factor.

Create a Scene From an Image URL#

In this example, we will show you how to initialize the CreativeEditor SDK with an initial image. Create an instance of Uri using the remote url. Use the object imageRemoteUri as a source for the initial image.

val imageRemoteUri = Uri.parse("https://img.ly/static/ubq_samples/sample_4.jpg")
val scene = engine.scene.createFromImage(imageRemoteUri)

When starting with an initial image, the scene’s page dimensions match the given resource and the scene is configured to be in pixel design units.

To later save your scene, see Saving Scenes.

import android.net.Uri
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import ly.img.engine.DesignBlockType
import ly.img.engine.Engine
fun createSceneFromImageURL(
license: String,
userId: String,
) = CoroutineScope(
Dispatchers.Main,
).launch {
val engine = Engine.getInstance(id = "ly.img.engine.example")
engine.start(license = license, userId = userId)
engine.bindOffscreen(width = 100, height = 100)
val imageRemoteUri = Uri.parse("https://img.ly/static/ubq_samples/sample_4.jpg")
val scene = engine.scene.createFromImage(imageRemoteUri)
engine.stop()
}

Create A Scene From an Image Blob#

In this example, we will show you how to initialize the CreativeEditor SDK with an initial image provided from a blob. First, get hold of a blob by fetching an image from the web. This is just for demonstration purposes and your blob object may come from a different source.

val blobUrl = URL("https://img.ly/static/ubq_samples/sample_4.jpg")
val blob = withContext(Dispatchers.IO) {
val outputStream = ByteArrayOutputStream()
blobUrl.openStream().use { inputStream ->
outputStream.use(inputStream::copyTo)
}
outputStream.toByteArray()
}

Afterward, create a temporary file and save the Data. Create an instance of Uri using the temporary file.

val blobFile = withContext(Dispatchers.IO) {
File.createTempFile(UUID.randomUUID().toString(), ".tmp").apply {
outputStream().use { it.write(blob) }
}
}
val blobUri = Uri.fromFile(blobFile)

Use the object blobUri as a source for the initial image.

val scene = engine.scene.createFromImage(blobUri)

When starting with an initial image, the scenes page dimensions match the given image, and the scene is configured to be in pixel design units.

To later save your scene, see Saving Scenes.

Full Code#

Here’s the full code:

import android.net.Uri
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import ly.img.engine.DesignBlockType
import ly.img.engine.Engine
import java.io.ByteArrayOutputStream
import java.io.File
import java.net.URL
import java.util.UUID
fun createSceneFromImageBlob(
license: String,
userId: String,
) = CoroutineScope(
Dispatchers.Main,
).launch {
val engine = Engine.getInstance(id = "ly.img.engine.example")
engine.start(license = license, userId = userId)
engine.bindOffscreen(width = 100, height = 100)
val blobUrl = URL("https://img.ly/static/ubq_samples/sample_4.jpg")
val blob = withContext(Dispatchers.IO) {
val outputStream = ByteArrayOutputStream()
blobUrl.openStream().use { inputStream ->
outputStream.use(inputStream::copyTo)
}
outputStream.toByteArray()
}
val blobFile = withContext(Dispatchers.IO) {
File.createTempFile(UUID.randomUUID().toString(), ".tmp").apply {
outputStream().use { it.write(blob) }
}
}
val blobUri = Uri.fromFile(blobFile)
val scene = engine.scene.createFromImage(blobUri)
engine.stop()
}