-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vinod Baste
committed
Jul 23, 2022
1 parent
b2e6870
commit 9ac41e4
Showing
47 changed files
with
1,322 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
plugins { | ||
id 'com.android.library' | ||
id 'org.jetbrains.kotlin.android' | ||
} | ||
|
||
android { | ||
compileSdk 31 | ||
|
||
defaultConfig { | ||
minSdk 21 | ||
targetSdk 31 | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
consumerProguardFiles "consumer-rules.pro" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation 'androidx.core:core-ktx:1.7.0' | ||
implementation 'androidx.appcompat:appcompat:1.4.2' | ||
implementation 'com.google.android.material:material:1.6.1' | ||
testImplementation 'junit:junit:4.13.2' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.3' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' | ||
} |
Empty 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
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 |
24 changes: 24 additions & 0 deletions
24
ImageCompressor/src/androidTest/java/com/android/imagecompressor/ExampleInstrumentedTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.android.imagecompressor | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.android.imagecompressor.test", appContext.packageName) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.android.imagecompressor"> | ||
|
||
</manifest> |
212 changes: 212 additions & 0 deletions
212
ImageCompressor/src/main/java/com/android/imagecompressor/compressImageUtils.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
@file:Suppress("DEPRECATION") | ||
|
||
package com.android.imagecompressor | ||
|
||
import android.content.Context | ||
import android.graphics.* | ||
import android.location.Location | ||
import android.media.ExifInterface | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.os.Environment | ||
import android.provider.MediaStore | ||
import android.util.Log | ||
import java.io.* | ||
import java.nio.file.Files | ||
import kotlin.math.roundToInt | ||
|
||
object ImageCompressUtils { | ||
|
||
//compress the image | ||
@JvmOverloads | ||
fun compressImage( | ||
context: Context, | ||
imagePath: String?, | ||
imageName: String?, | ||
imageQuality: Int = 50 | ||
): String { | ||
|
||
var filePath = "" | ||
try { | ||
var scaledBitmap: Bitmap? = null | ||
val options = BitmapFactory.Options() | ||
// by setting this field as true, the actual bitmap pixels are not loaded in the memory. | ||
// Just the bounds are loaded. If you try the use the bitmap here, you will get null. | ||
options.inJustDecodeBounds = true | ||
var actualHeight = options.outHeight | ||
var actualWidth = options.outWidth | ||
|
||
val imageFile = File(imagePath!!) | ||
val fileContent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
Files.readAllBytes(imageFile.toPath()) | ||
} else { | ||
File(imagePath).readBytes() | ||
} | ||
var bmp = BitmapFactory.decodeByteArray(fileContent, 0, fileContent.size, options) | ||
|
||
//max Height and width values of the compressed image is taken as 1024x912 | ||
val maxHeight = 1024.0f | ||
val maxWidth = 912.0f | ||
var imgRatio = actualWidth / actualHeight.toFloat() | ||
val maxRatio = maxWidth / maxHeight | ||
|
||
//width and height values are set maintaining the aspect ratio of the image | ||
if (actualHeight > maxHeight || actualWidth > maxWidth) { | ||
when { | ||
imgRatio < maxRatio -> { | ||
imgRatio = maxHeight / actualHeight | ||
actualWidth = (imgRatio * actualWidth).toInt() | ||
actualHeight = maxHeight.toInt() | ||
} | ||
imgRatio > maxRatio -> { | ||
imgRatio = maxWidth / actualWidth | ||
actualHeight = (imgRatio * actualHeight).toInt() | ||
actualWidth = maxWidth.toInt() | ||
} | ||
else -> { | ||
actualHeight = maxHeight.toInt() | ||
actualWidth = maxWidth.toInt() | ||
} | ||
} | ||
} | ||
|
||
//setting inSampleSize value allows to load a scaled down version of the original image | ||
options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight) | ||
//inJustDecodeBounds set to false to load the actual bitmap | ||
options.inJustDecodeBounds = false | ||
//this removes the redundant quality of the image | ||
options.inPurgeable = true | ||
//this options allow android to claim the bitmap memory if it runs low on memory | ||
options.inInputShareable = true | ||
|
||
options.inTempStorage = ByteArray(16 * 1024) | ||
try { | ||
//load the bitmap from its path | ||
bmp = BitmapFactory.decodeFile(imagePath, options) | ||
} catch (exception: OutOfMemoryError) { | ||
exception.printStackTrace() | ||
} | ||
try { | ||
scaledBitmap = | ||
Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888) | ||
} catch (exception: OutOfMemoryError) { | ||
exception.printStackTrace() | ||
} | ||
val ratioX = actualWidth / options.outWidth.toFloat() | ||
val ratioY = actualHeight / options.outHeight.toFloat() | ||
val middleX = actualWidth / 2.0f | ||
val middleY = actualHeight / 2.0f | ||
val scaleMatrix = Matrix() | ||
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY) | ||
val canvas = Canvas(scaledBitmap!!) | ||
canvas.setMatrix(scaleMatrix) | ||
canvas.drawBitmap( | ||
bmp, | ||
middleX - bmp.width / 2, | ||
middleY - bmp.height / 2, | ||
Paint(Paint.FILTER_BITMAP_FLAG) | ||
) | ||
|
||
//check the rotation of the image and display it properly | ||
val exif: ExifInterface | ||
try { | ||
exif = ExifInterface(imagePath.toString()) | ||
val orientation = exif.getAttributeInt( | ||
ExifInterface.TAG_ORIENTATION, 0 | ||
) | ||
Log.d("EXIF", "Exif: $orientation") | ||
val matrix = Matrix() | ||
when (orientation) { | ||
6 -> { | ||
matrix.postRotate(90f) | ||
Log.d("EXIF", "Exif: $orientation") | ||
} | ||
3 -> { | ||
matrix.postRotate(180f) | ||
Log.d("EXIF", "Exif: $orientation") | ||
} | ||
8 -> { | ||
matrix.postRotate(270f) | ||
Log.d("EXIF", "Exif: $orientation") | ||
} | ||
} | ||
scaledBitmap = Bitmap.createBitmap( | ||
scaledBitmap, 0, 0, | ||
scaledBitmap.width, scaledBitmap.height, matrix, | ||
true | ||
) | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} catch (e: java.lang.Exception) { | ||
e.printStackTrace() | ||
} | ||
val out: FileOutputStream? | ||
filePath = imageName?.let { getOutputMediaFile(it, context) }!!.absolutePath | ||
try { | ||
out = FileOutputStream(filePath) | ||
|
||
//write the compressed bitmap at the destination specified by filename. | ||
scaledBitmap!!.compress(Bitmap.CompressFormat.JPEG, imageQuality, out) | ||
} catch (e: FileNotFoundException) { | ||
e.printStackTrace() | ||
} | ||
} catch (e: java.lang.Exception) { | ||
e.printStackTrace() | ||
} | ||
|
||
return filePath | ||
} | ||
|
||
} | ||
|
||
private fun getOutputMediaFile(imageName: String, context: Context): File? { | ||
var imageFile1: File? = null | ||
try { | ||
imageFile1 = createImageFile(context, imageName) | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
if (imageFile1!!.exists()) imageFile1.delete() | ||
var imageNew: File? = null | ||
try { | ||
imageNew = createImageFile(context, imageName) | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
return imageNew | ||
} | ||
|
||
private fun calculateInSampleSize( | ||
options: BitmapFactory.Options, | ||
reqWidth: Int, | ||
reqHeight: Int | ||
): Int { | ||
val height = options.outHeight | ||
val width = options.outWidth | ||
var inSampleSize = 1 | ||
try { | ||
if (height > reqHeight || width > reqWidth) { | ||
val heightRatio = | ||
(height.toFloat() / reqHeight.toFloat()).roundToInt() | ||
val widthRatio = | ||
(width.toFloat() / reqWidth.toFloat()).roundToInt() | ||
inSampleSize = if (heightRatio < widthRatio) heightRatio else widthRatio | ||
} | ||
val totalPixels = width * height.toFloat() | ||
val totalReqPixelsCap = reqWidth * reqHeight * 2.toFloat() | ||
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) { | ||
inSampleSize++ | ||
} | ||
} catch (e: java.lang.Exception) { | ||
e.printStackTrace() | ||
} | ||
return inSampleSize | ||
} | ||
|
||
@Throws(IOException::class) | ||
private fun createImageFile(context: Context, FileName: String): File { | ||
return File( | ||
context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) | ||
.toString() + File.separator + FileName + ".png" | ||
) | ||
} |
17 changes: 17 additions & 0 deletions
17
ImageCompressor/src/test/java/com/android/imagecompressor/ExampleUnitTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.android.imagecompressor | ||
|
||
import org.junit.Test | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Example local unit test, which will execute on the development machine (host). | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
class ExampleUnitTest { | ||
@Test | ||
fun addition_isCorrect() { | ||
assertEquals(4, 2 + 2) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
Oops, something went wrong.