Last active
April 11, 2023 05:38
-
-
Save nareshkatta99/5fbe8a37799d5a48ea9de32af4a69ea9 to your computer and use it in GitHub Desktop.
Java way of using Compressor 3.0 (Full project https://github.com/nareshkatta99/java_compressor_example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//app/build.gradle | |
apply plugin: 'com.android.application' | |
// Required for kotlin and compressor | |
apply plugin: 'kotlin-android' | |
android { | |
compileSdkVersion 30 | |
buildToolsVersion "30.0.1" | |
defaultConfig { | |
applicationId "com.nareshkatta99.java_compressor" | |
minSdkVersion 21 | |
targetSdkVersion 30 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
implementation fileTree(dir: "libs", include: ["*.jar"]) | |
implementation 'androidx.appcompat:appcompat:1.2.0' | |
implementation 'androidx.constraintlayout:constraintlayout:2.0.1' | |
testImplementation 'junit:junit:4.12' | |
androidTestImplementation 'androidx.test.ext:junit:1.1.2' | |
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | |
// Required for kotlin and compressor | |
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | |
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version" | |
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version" | |
implementation 'com.squareup.picasso:picasso:2.71828' | |
implementation 'id.zelory:compressor:3.0.0' | |
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' | |
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0' | |
//for progress drawable | |
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nareshkatta99.java_compressor | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.lifecycle.lifecycleScope | |
import id.zelory.compressor.Compressor | |
import id.zelory.compressor.constraint.Constraint | |
import id.zelory.compressor.constraint.default | |
import kotlinx.coroutines.launch | |
import java.io.File | |
fun AppCompatActivity.compress( | |
inputFile: File, | |
callback: Callback, | |
vararg constraints: Constraint | |
) { | |
val context = this | |
var outputFile: File? = null | |
lifecycleScope.launch { | |
try { | |
outputFile = Compressor.compress(context, inputFile) { | |
if (constraints.isEmpty()) { | |
default() | |
} else { | |
for (con in constraints) | |
constraint(con) | |
} | |
}.also { | |
callback.onComplete(true, it) | |
} | |
} catch (e: Exception) { | |
callback.onComplete(false, outputFile) | |
} | |
} | |
} | |
class JavaCompressor { | |
companion object { | |
@JvmStatic | |
fun compress(activity: AppCompatActivity, inputFile: File, | |
callback: Callback, | |
vararg constraints: Constraint) { | |
activity.compress(inputFile, callback, *constraints) | |
} | |
} | |
} | |
interface Callback { | |
fun onComplete(status: Boolean, file: File?) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void startCompression() { | |
if (selected != null && selected.exists()) { | |
//add implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0" to dependencies | |
final CircularProgressDrawable drawable = new CircularProgressDrawable(this); | |
drawable.setStrokeWidth(5f); | |
drawable.setCenterRadius(25f); | |
drawable.setColorSchemeColors(ResourcesCompat.getColor(getResources(), R.color.colorPrimary, null)); | |
drawable.start(); | |
i2.setImageDrawable(drawable); | |
JavaCompressor.compress(this, selected, new Callback() { | |
@Override | |
public void onComplete(boolean status, @Nullable File file) { | |
drawable.stop(); | |
if (status) { | |
compressed = file; | |
onCompressReady(); | |
} | |
} | |
}, new DestinationConstraint(new File(getCacheDir(), "compressed.jpeg"))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Top-level build file where you can add configuration options common to all sub-projects/modules. | |
buildscript { | |
// Required for kotlin and compressor | |
ext.kotlin_version = "1.4.0" | |
ext.kotlin_coroutines_version = '1.3.8' | |
repositories { | |
google() | |
jcenter() | |
} | |
dependencies { | |
classpath "com.android.tools.build:gradle:4.0.1" | |
// Required for kotlin and compressor | |
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | |
// NOTE: Do not place your application dependencies here; they belong | |
// in the individual module build.gradle files | |
} | |
} | |
allprojects { | |
repositories { | |
google() | |
jcenter() | |
} | |
} | |
task clean(type: Delete) { | |
delete rootProject.buildDir | |
} |
In MainActivity.java,
Remove new DestinationConstraint(new File(getCacheDir(), "compressed.jpeg"))
and try.
That is just for an example.
If you want, you can use new DefaultConstraint()
did not helped. this is not compressing image
Did you tried new DefaultConstraint()
instead of new DestinationConstraint(new File(getCacheDir(), "compressed.jpeg")
?
thanks
Is it possible to do with maven dependency? Do i need to download kotlin software to run these services
I'm not actively working on this.
Creating maven dependency might take some time.
For now you have to download the kotlin library.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
did not helped. this is not compressing image