Library created for simple taking images/photos/videos from gallery or camera source and then apply transformations such as crop, fit, rotate, etc.
Add repository to your root build.gradle
repositories {
jcenter()
}
dependencies {
implementation 'com.idapgroup:tnt:latest-version'
}
Activity and Fragment have the next extension functions for most common cases:
-
takeImageFromGallery, takeVideoFromGallery - opens android picker and returns taken photo/video Uri.
-
takeImageFromCamera, takeVideoFromCamera - opens android camera and returns captured photo/video Uri.
class ExampleActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
galleryButton.setOnClickListener {
takeImageFromGallery {
proccessImageUri(it)
}
}
cameraButton.setOnClickListener {
takeVideoFromCamera {
proccessVideoUri(it)
}
}
}
}
"As a developer, I don't want to write boilerplate code for permissions or saving related data to instance state."
Functions that were described above are just an extension functions for take
.
take(source: Source<*>, permissions: PermissionParams.() -> Unit = {}, callback: F.(Uri) -> Unit)
Where:
- source - one of Gallery(MimeType) or Camera(MediaType);
- permission - dispatches permission denied events ;
- callback - receiver for selected data Uri.
class ExampleActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
galleryButton.setOnClickListener {
take(
source = Source.Gallery(MimeType.Video.Any),
permissions = {
onDenied { showToast("Permission denied") }
onPermanentlyDenied { showToast("Permission permanently denied") }
}) {
proccessVideoUri(it)
}
}
cameraButton.setOnClickListener {
take(
source = Source.Camera(MediaType.Image),
permissions = {
onDenied { showToast("Permission denied") }
onPermanentlyDenied { showToast("Permission permanently denied") }
}) {
proccessImageUri(it)
}
}
}
}
Keep in mind: when you are trying to use local variables inside callback function it must be primitives or implements Serializable interface. Parcelable isn't supported yet.
open class User(var image: Uri? = null)
class SerializableUser : User(), Serializable
class MyFragment : Fragment() {
val user = User()
fun takePhotoForUser() {
val localUser = User()
val localSerializableUser = SerializableUser()
takeImageFromGallery {
user.image = it // OK!
localSerializableUser.image = it // OK!
localUser.image = it // Wrong!! Runtime exception
}
}
}
Tramsormation functions:
-
resize(width, height, scaleType) - change origin size of bitmap.
-
square(size: Int?, scaleType) - if size null - takes origin bitmap smaller side
-
Supported scale types:
cropStart
,cropCenter
,cropEnd
,fitStart
,fitCenter
,fitEnd
,fitXY
-
rotate(degrees: Float) - rotates bitmap by selected degrees
-
background(color: Int) - set bitmap background for
fitStart
,fitCenter
,fitEnd
modes -
blur(radius, sampling)
-
colorFilter(color)
val bitmap = file.transformAsBitmap {
resize(100, 200, cropEnd)
rotate(43f)
colorFilter(a = 89, r = 34, g = 120, b = 34)
background(Color.RED)
blur()
}