Skip to content

Commit

Permalink
feat: add samples
Browse files Browse the repository at this point in the history
  • Loading branch information
wangshouheng committed Dec 18, 2021
1 parent f177614 commit 0e503ff
Show file tree
Hide file tree
Showing 9 changed files with 520 additions and 46 deletions.
Binary file added sample/app/src/main/assets/img_lena.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions sample/app/src/main/java/me/shouheng/sample/App.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package me.shouheng.sample

import android.app.Application
import me.shouheng.compress.utils.CLog
import me.shouheng.compress.Compress
import me.shouheng.sample.custom.AssetsResource
import me.shouheng.sample.custom.AssetsResourceSource
import me.shouheng.vmlib.VMLib
import java.io.InputStream

/**
* The custom application
Expand All @@ -11,7 +14,9 @@ class App : Application() {

override fun onCreate() {
super.onCreate()
CLog.isDebug = true
VMLib.onCreate(this)
Compress.setDebug(true)
// Register custom image source type and its adapter.
Compress.registerTypeAdapter(AssetsResource::class.java, AssetsResourceSource.Adapter())
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.shouheng.sample.utils
package me.shouheng.sample.custom

import me.shouheng.compress.strategy.SimpleStrategy

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package me.shouheng.sample.custom

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import me.shouheng.compress.strategy.IImageSource
import me.shouheng.compress.strategy.Size
import me.shouheng.compress.utils.copy
import me.shouheng.compress.utils.pixelSize
import me.shouheng.utils.app.ResUtils
import java.io.File
import java.io.FileOutputStream

data class AssetsResource(val name: String)

/**
* Image source for [AssetsResource].
*
* @Author wangshouheng
* @Time 2021/12/18
*/
class AssetsResourceSource(private val res: AssetsResource): IImageSource<AssetsResource> {

override fun getSize(): Size {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
options.inSampleSize = 1
val ins = ResUtils.getAssets().open(res.name)
BitmapFactory.decodeStream(ins, null, options)
return Size(options.outWidth, options.outHeight)
}

override fun getOriginBitmapByOptions(
options: BitmapFactory.Options
): Bitmap? {
val ins = ResUtils.getAssets().open(res.name)
return BitmapFactory.decodeStream(ins, null, options)
}

override fun shouldIgnoreForSize(ignoreSize: Int): Boolean {
val ins = ResUtils.getAssets().open(res.name)
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
options.inSampleSize = 1
BitmapFactory.decodeStream(ins, null, options)
val size = options.outWidth*options.outHeight*options.pixelSize()
return size < ignoreSize shl 10
}

override fun getRotation(): Int = 0

override fun copyTo(
dest: File,
format: Bitmap.CompressFormat,
quality: Int
): Boolean {
val ins = ResUtils.getAssets().open(res.name)
return copy(ins, FileOutputStream(dest))
}

class Adapter: IImageSource.Adapter<AssetsResource> {
override fun get(
context: Context, source: AssetsResource
): IImageSource<AssetsResource> = AssetsResourceSource(source)
}
}
5 changes: 3 additions & 2 deletions sample/app/src/main/java/me/shouheng/sample/data/Data.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ enum class LaunchType {

/** Compress data source type */
enum class SourceType {
FILE, BYTE_ARRAY, BITMAP
FILE, BYTE_ARRAY, BITMAP, URI
}

/** Result type */
Expand Down Expand Up @@ -49,7 +49,8 @@ val scaleModes = arrayOf(
val sourceTypes = arrayOf(
SourceType.FILE,
SourceType.BYTE_ARRAY,
SourceType.BITMAP
SourceType.BITMAP,
SourceType.URI
)

/** Result type options. */
Expand Down
18 changes: 10 additions & 8 deletions sample/app/src/main/java/me/shouheng/sample/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import me.shouheng.compress.*
import me.shouheng.compress.listener.CompressListener
import me.shouheng.compress.naming.CacheNameFactory
import me.shouheng.compress.strategy.config.ScaleMode
import me.shouheng.compress.utils.CImageUtils
import me.shouheng.compress.utils.orientation
import me.shouheng.compress.utils.rotate
import me.shouheng.sample.R
import me.shouheng.sample.custom.AlwaysHalfAlgorithm
import me.shouheng.sample.data.*
import me.shouheng.sample.databinding.ActivityMainBinding
import me.shouheng.sample.utils.*
Expand Down Expand Up @@ -204,8 +206,7 @@ class MainActivity : CommonActivity<EmptyViewModel, ActivityMainBinding>() {

private fun compressByCustom() {
binding.ivOriginal.tag?.let {
val file = File(it as String)
Compress.with(context, file)
getCompress(it as String)
.setCompressListener(getCompressListener("Half", ResultType.FILE))
.setQuality(80)
.algorithm(AlwaysHalfAlgorithm())
Expand All @@ -227,6 +228,9 @@ class MainActivity : CommonActivity<EmptyViewModel, ActivityMainBinding>() {
val bitmap = BitmapFactory.decodeFile(path)
Compress.with(this, bitmap)
}
SourceType.URI -> {
Compress.with(this, File(path).uri(context))
}
}
}

Expand Down Expand Up @@ -316,8 +320,8 @@ class MainActivity : CommonActivity<EmptyViewModel, ActivityMainBinding>() {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = false
var bitmap = BitmapFactory.decodeFile(filePath, options)
val angle = CImageUtils.getImageAngle(File(filePath))
if (angle != 0) bitmap = CImageUtils.rotateBitmap(bitmap, angle)
val angle = File(filePath).orientation()
bitmap = bitmap.rotate(angle)
binding.ivOriginal.setImageBitmap(bitmap)
val size = bitmap.byteCount
binding.tvOriginal.text = "Original:\nwidth: ${bitmap.width}\nheight:${bitmap.height}\nsize:$size"
Expand All @@ -337,9 +341,7 @@ class MainActivity : CommonActivity<EmptyViewModel, ActivityMainBinding>() {
}

private fun displayResult(bitmap: Bitmap?) {
if (bitmap == null) {
return
}
bitmap ?: return
val actualWidth = bitmap.width
val actualHeight = bitmap.height
binding.ivResult.setImageBitmap(bitmap)
Expand Down
Loading

0 comments on commit 0e503ff

Please sign in to comment.