Add an option to reencode images under the advanced tab. #262

This commit is contained in:
len
2016-04-21 15:31:07 +02:00
parent bd8b9febd2
commit 0a27d4e185
9 changed files with 50 additions and 7 deletions

View File

@@ -1,5 +1,7 @@
package eu.kanade.tachiyomi.util
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import okio.BufferedSource
import okio.Okio
import java.io.File
@@ -36,4 +38,25 @@ fun BufferedSource.saveTo(stream: OutputStream) {
it.flush()
}
}
}
/**
* Saves the given source to an output stream and closes both resources.
* The source is expected to be an image, and it may reencode the image.
*
* @param stream the stream where the source is copied.
* @param reencode whether to reencode the image or not.
*/
fun BufferedSource.saveImageTo(stream: OutputStream, reencode: Boolean = false) {
if (reencode) {
use {
val bitmap = BitmapFactory.decodeStream(it.inputStream())
stream.use {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, it)
}
bitmap.recycle()
}
} else {
saveTo(stream)
}
}