Skip to content

Commit

Permalink
Bump libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
jeziellago committed Jul 29, 2022
1 parent a1448ae commit 661bde9
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 68 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Image Minifier
![CI](https://github.com/jeziellago/image-minifier/workflows/CI/badge.svg?branch=master) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/fb95312d03504525b38f4789a3ae58d3)](https://www.codacy.com/gh/jeziellago/image-minifier/dashboard?utm_source=github.com&utm_medium=referral&utm_content=jeziellago/image-minifier&utm_campaign=Badge_Grade)
![](minifier_logo.png)
![CI](https://github.com/jeziellago/image-minifier/workflows/CI/badge.svg?branch=master)

Minifier is a lightweight (**21KB**) android library for image resizing, format changing and quality focusing in reduce file size.
## How it works?
Expand All @@ -26,7 +25,9 @@ val minifiedFile: File = MinifierFactory.create(context)
resize(1200, 720)
convertTo(CompressFormat.JPEG)
}
.minify(Dispatchers.IO)
.minifyWith(Dispatchers.Default)
.onSuccess { minified -> /* success */ }
.onFailure { error -> /* failure */ }
```
## Transformations
- Resize
Expand Down Expand Up @@ -58,6 +59,6 @@ allprojects {
- Module `build.gradle`
```
dependencies {
implementation 'com.github.jeziellago:image-minifier:0.1.1'
implementation 'com.github.jeziellago:image-minifier:0.2.0'
}
```
9 changes: 4 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
buildscript {
ext.kotlin_version = '1.4.10'
ext.kotlin_version = '1.6.10'
repositories {
google()
jcenter()
mavenCentral()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.android.tools.build:gradle:7.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

allprojects {
repositories {
google()
jcenter()

mavenCentral()
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
30 changes: 8 additions & 22 deletions minifier/build.gradle
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
compileSdkVersion 32
buildToolsVersion "30.0.3"

defaultConfig {
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
targetSdkVersion 32

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = "1.8"
}

}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'androidx.exifinterface:exifinterface:1.3.0-alpha01'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7"
implementation 'androidx.exifinterface:exifinterface:1.3.3'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"

testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
3 changes: 1 addition & 2 deletions minifier/src/main/java/com/image/minifier/FileExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ import java.io.FileOutputStream

internal fun Bitmap.CompressFormat.toFileExtension() = when (this) {
Bitmap.CompressFormat.PNG -> ".png"
Bitmap.CompressFormat.WEBP -> ".webp"
Bitmap.CompressFormat.JPEG -> ".jpg"
else -> throw IllegalArgumentException("Invalid compression format: $this")
}

internal fun File.getExtensionAsBitmapFormat(): Bitmap.CompressFormat {
return when (this.extension) {
"png" -> Bitmap.CompressFormat.PNG
"webp" -> Bitmap.CompressFormat.WEBP
"jpeg", "jpg" -> Bitmap.CompressFormat.JPEG
else -> throw IllegalArgumentException("Invalid extension to Bitmap: ${this.extension}")
}
Expand Down
2 changes: 2 additions & 0 deletions minifier/src/main/java/com/image/minifier/Minifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ interface Minifier {
fun minify(result: Result<File>.() -> Unit)

suspend fun minify(dispatcher: CoroutineDispatcher = Dispatchers.Default): File

suspend fun minifyWith(dispatcher: CoroutineDispatcher): Result<File>
}

fun Minifier.resize(width: Int, height: Int) {
Expand Down
6 changes: 4 additions & 2 deletions minifier/src/main/java/com/image/minifier/MinifierImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package com.image.minifier

import android.content.Context
import android.net.Uri
import androidx.annotation.WorkerThread
import com.image.minifier.transformation.ImageTransformation
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -48,14 +47,17 @@ internal class MinifierImpl internal constructor(context: Context) : Minifier {
transformations.add(transformation)
}

@WorkerThread
override fun minify(result: Result<File>.() -> Unit) {
result(runCatching { minifyImage() })
}

override suspend fun minify(dispatcher: CoroutineDispatcher): File =
withContext(dispatcher) { minifyImage() }

override suspend fun minifyWith(dispatcher: CoroutineDispatcher): Result<File> {
return runCatching { withContext(dispatcher) { minifyImage() } }
}

private fun minifyImage(): File {
require(transformations.isNotEmpty()) { "Minifier require any transformation to work." }
var source = imageFile.copyToDir(cacheDir)
Expand Down
33 changes: 11 additions & 22 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
compileSdkVersion 32
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "com.image.kompressor.sample"
applicationId "dev.jeziellago.imageminifier.sample"
minSdkVersion 16
targetSdkVersion 29
targetSdkVersion 32
versionCode 1
versionName "1.0"

Expand All @@ -23,25 +22,15 @@ android {
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = "1.8"
}

}

dependencies {
implementation project(':minifier')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7"
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
4 changes: 3 additions & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
22 changes: 13 additions & 9 deletions sample/src/main/java/com/image/minifier/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
package com.image.minifier.sample

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.image.minifier.MinifierFactory
import com.image.minifier.colorGrayScale
import com.image.minifier.getAsBitmap
import com.image.minifier.resize
import kotlinx.android.synthetic.main.activity_main.image
import kotlinx.android.synthetic.main.activity_main.txtDescription
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.io.File
import java.io.FileOutputStream
Expand All @@ -48,13 +52,13 @@ class MainActivity : AppCompatActivity() {
resize(1200, 1200)
colorGrayScale()
}
.minify {
onSuccess { minified ->
txtDescription.text = "Original: $originalSize kb\n" +
"After minifier: ${minified.length() / 1024} kb"
image.setImageBitmap(minified.getAsBitmap())
}
}
.minifyWith(Dispatchers.Default)
.onSuccess { minified ->
findViewById<TextView>(R.id.txtDescription).text =
"Original: $originalSize kb\n" +
"After minifier: ${minified.length() / 1024} kb"
findViewById<ImageView>(R.id.image).setImageBitmap(minified.getAsBitmap())
}.onFailure { findViewById<TextView>(R.id.txtDescription).text = "Error: $it" }
}
}

Expand Down

0 comments on commit 661bde9

Please sign in to comment.