Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CustomStickerLayer example #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions custom_sticker_layer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
77 changes: 77 additions & 0 deletions custom_sticker_layer/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
apply plugin: 'com.android.application'

apply plugin: 'ly.img.android.sdk'
apply plugin: 'kotlin-android'

imglyConfig {

pesdk {
enabled true
licencePath 'LICENSE' // Name of the Licence file in the asset folder
}

// Define the modules you are need
modules {
// Add all the UI modules you are need
include 'ui:core'
include 'ui:text'
include 'ui:focus'
include 'ui:frame'
include 'ui:brush'
include 'ui:filter'
include 'ui:camera'
include 'ui:sticker'
include 'ui:overlay'
include 'ui:transform'
include 'ui:adjustment'
include 'ui:text-design'

// Add the serializer if you need
include 'backend:serializer'

// Allow Background Encoding [Optional]
include 'backend:headless'

// Add asset packs if you need
include 'assets:font-basic'
include 'assets:frame-basic'
include 'assets:filter-basic'
include 'assets:overlay-basic'
include 'assets:sticker-shapes'
include 'assets:sticker-emoticons'
include 'backend:sticker-smart'
}
}

android {
compileSdkVersion 30
buildToolsVersion '30.0.2'

defaultConfig {
applicationId "com.photoeditorsdk.android.app"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

resValue 'string', 'pesdk_version', "v$pesdk_version"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.3.1'
}
21 changes: 21 additions & 0 deletions custom_sticker_layer/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
26 changes: 26 additions & 0 deletions custom_sticker_layer/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.photoeditorsdk.android.app">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

<protected-broadcast android:name="android.intent.action.MEDIA_MOUNTED" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this broadcast needed? 😅


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".KEditorDemoActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>
1 change: 1 addition & 0 deletions custom_sticker_layer/src/main/assets/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"owner":"Imgly","version":"2.1","enterprise_license":false,"available_actions":["magic","filter","transform","sticker","text","adjustments","brush","focus","frames","camera"],"features":["adjustment","filter","focus","overlay","transform","text","sticker","frame","brush","camera","textdesign","library","export"],"platform":"Android","app_identifiers":["com.photoeditorsdk.android.app"],"api_token":"-lIPN9NGKvlJBI90zPVrFg","domains":["https://api.photoeditorsdk.com"],"issued_at":1528984457,"expires_at":null,"signature":"h3Q0G/1HZg+t9WXXYQ+b6YSbGHc3UOq1ktOci5tNf4xTV+A7igkTPCYbxSIELZUyAr+9idZNt8Z1thIVQ+3Kzaq7kCzaDgl+vHAglrxHTZuscg8gPjSGrfXWUeUNA0PZUXY72q5DYVc8BRaTrCdnIfZPScnvi1QmV/+M/ao2VGq98/Mu8jb4jHXpj3f/zTlsaBwUgLFCerR2nEYm8m0cpjxuGu1DSIKxCC8JgTxhPLC+w8LS0+gSlfps69lVUap7VyAj9DWCARQDfeHSAnokYzvTu0GL7xglCWuZbutaVezA//oWaa0qIDY66tOXBKosULRp2VqShATmfbvnIueAkEhWZSyMX+G6MRI1VYHtI4Q5YhMQLSWFvsIP6oVLJs+iiMpUXtcglGp85TGY9x2fQcJuWAbvFm7vYnTJqTHyiU81uZF+goC/N8zLrn8gORreOmdvDqQ7Sff65k8I9+D6cv7gtZGw4mxGohbMGqPuTFCXlbsCCA/58KTYJoqme/wPxqB0yhBE9VBU3W/y3TFjsv5ZmhZH1YheiN/fTOlnwSij8ZeaFCLtkNcfSmS7EWhPDWfZxptiRS7/BJqVRYxRwRc4W3KWMc30EiWCm0F+WDW64z0ayLH0g8KA56eaDR/8P1P0TNPJh8uS9t56HmvBmEU5pDrVm+BQRP3oPf3OFss="}
Binary file added custom_sticker_layer/src/main/ic_launcher-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package com.photoeditorsdk.android.app

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.util.Log
import android.widget.Button
import android.widget.Toast

import com.photoeditorsdk.android.app.custom.CustomImageStickerLayerSettings

import java.io.File
import java.io.IOException
import ly.img.android.pesdk.PhotoEditorSettingsList
import ly.img.android.pesdk.assets.filter.basic.FilterPackBasic
import ly.img.android.pesdk.assets.font.basic.FontPackBasic
import ly.img.android.pesdk.assets.frame.basic.FramePackBasic
import ly.img.android.pesdk.assets.overlay.basic.OverlayPackBasic
import ly.img.android.pesdk.assets.sticker.emoticons.StickerPackEmoticons
import ly.img.android.pesdk.assets.sticker.shapes.StickerPackShapes
import ly.img.android.pesdk.backend.model.EditorSDKResult
import ly.img.android.pesdk.backend.model.constant.OutputMode
import ly.img.android.pesdk.backend.model.state.LoadSettings
import ly.img.android.pesdk.backend.model.state.PhotoEditorSaveSettings
import ly.img.android.pesdk.backend.model.state.layer.ImageStickerLayerSettings
import ly.img.android.pesdk.backend.model.state.manager.StateHandler

import ly.img.android.pesdk.ui.activity.PhotoEditorBuilder
import ly.img.android.pesdk.ui.model.state.*
import ly.img.android.pesdk.ui.panels.item.PersonalStickerAddItem
import ly.img.android.pesdk.ui.utils.PermissionRequest
import ly.img.android.serializer._3.IMGLYFileWriter

class KEditorDemoActivity : Activity(), PermissionRequest.Response {

companion object {
const val PESDK_RESULT = 1
const val GALLERY_RESULT = 2
init {
StateHandler.replaceStateClass(ImageStickerLayerSettings::class.java, CustomImageStickerLayerSettings::class.java)
}
}

// Important permission request for Android 6.0 and above, don't forget to add this!
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
PermissionRequest.onRequestPermissionsResult(requestCode, permissions, grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}

override fun permissionGranted() {}

override fun permissionDenied() {
/* TODO: The Permission was rejected by the user. The Editor was not opened,
* Show a hint to the user and try again. */
}

// Create a empty new SettingsList and apply the changes on this reference.
// If you include our asset Packs and use our UI you also need to add them to the UI,
// otherwise they are only available for the backend (like Serialisation)
// See the specific feature sections of our guides if you want to know how to add our own Assets.
private fun createPesdkSettingsList() =
PhotoEditorSettingsList()
.configure<UiConfigFilter> {
it.setFilterList(FilterPackBasic.getFilterPack())
}
.configure<UiConfigText> {
it.setFontList(FontPackBasic.getFontPack())
}
.configure<UiConfigFrame> {
it.setFrameList(FramePackBasic.getFramePack())
}
.configure<UiConfigOverlay> {
it.setOverlayList(OverlayPackBasic.getOverlayPack())
}
.configure<UiConfigSticker> {
it.setStickerLists(
PersonalStickerAddItem(),
StickerPackEmoticons.getStickerCategory(),
StickerPackShapes.getStickerCategory()
)
}
.configure<PhotoEditorSaveSettings> {
// Set custom editor image export settings
it.setOutputToGallery(Environment.DIRECTORY_DCIM)
it.outputMode = OutputMode.EXPORT_IF_NECESSARY
}


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val openGallery = findViewById<Button>(R.id.openGallery)

openGallery.setOnClickListener {
openSystemGalleryToSelectAnImage()
}
}

fun openSystemGalleryToSelectAnImage() {
val intent = Intent(Intent.ACTION_PICK)
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
intent.type = "image/*"
} else {
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*")
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || intent.resolveActivity(packageManager) != null) {
startActivityForResult(intent, GALLERY_RESULT)
} else {
Toast.makeText(
this,
"No Gallery APP installed",
Toast.LENGTH_LONG
).show()
}
}

fun openEditor(inputImage: Uri?) {
val settingsList = createPesdkSettingsList()

settingsList.configure<LoadSettings> {
it.source = inputImage
}

PhotoEditorBuilder(this)
.setSettingsList(settingsList)
.startActivityForResult(this, PESDK_RESULT)
}


override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
super.onActivityResult(requestCode, resultCode, intent)

intent ?: return

if (resultCode == RESULT_OK && requestCode == GALLERY_RESULT) {
// Open Editor with some uri in this case with an image selected from the system gallery.
val selectedImage = intent.data
if (selectedImage != null) {
openEditor(selectedImage)
}
} else if (resultCode == RESULT_OK && requestCode == PESDK_RESULT) {
// Editor has saved an Image.
val data = EditorSDKResult(intent)

Log.i("PESDK", "Source image is located here ${data.sourceUri}")
Log.i("PESDK", "Result image is located here ${data.resultUri}")

// TODO: Do something with the result image

// OPTIONAL: read the latest state to save it as a serialisation
val lastState = data.settingsList
try {
IMGLYFileWriter(lastState).writeJson(File(
Environment.getExternalStorageDirectory(),
"serialisationReadyToReadWithPESDKFileReader.json"
))
} catch (e: IOException) {
e.printStackTrace()
}

} else if (resultCode == RESULT_CANCELED && requestCode == PESDK_RESULT) {
// Editor was canceled
val data = EditorSDKResult(intent)

val sourceURI = data.sourceUri
// TODO: Do something with the source...*/
}
}

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you use the source code from the latest deployed docs? That has better handling for gallery intent, doesn't use the deprecated getExternalStorageDirectory, and also doesn't have un-needed permission handling.

Loading