Skip to content

Commit

Permalink
Change format for sketches from png to jpeg for faster compression
Browse files Browse the repository at this point in the history
  • Loading branch information
udenr committed Aug 13, 2024
1 parent e945e8b commit 3ca58df
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,15 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH), OnDia
private val undoRedoEnabled by lazy { PreferenceManager.getDefaultSharedPreferences(this).getBoolean("settings_sketch_undo_redo", true) }

private fun emptyBitmap(): Bitmap {
return Bitmap.createBitmap(
val bitmap = Bitmap.createBitmap(
drawView.bitmap.width,
drawView.bitmap.height,
drawView.bitmap.config
)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.WHITE)
canvas.drawBitmap(bitmap, 0f, 0f, null)
return bitmap
}

@SuppressLint("ClickableViewAccessibility")
Expand All @@ -91,7 +95,7 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH), OnDia
if (oldSketch != null) {
drawView.background = oldSketch
} else {
drawView.background = BitmapDrawable(resources, Bitmap.createScaledBitmap(drawView.bitmap, initialSize!!.first, initialSize!!.second, false))
drawView.background = BitmapDrawable(resources, Bitmap.createScaledBitmap(emptyBitmap(), initialSize!!.first, initialSize!!.second, false))
}
if (state != null) {
drawView.drawBitmap(Bitmap.createScaledBitmap(state!!, initialSize!!.first, initialSize!!.second, false), 0f, 0f, null)
Expand Down Expand Up @@ -145,7 +149,7 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH), OnDia
}

override fun onNewNote() {
mFileName = "/sketch_" + System.currentTimeMillis() + ".PNG"
mFileName = "/sketch_" + System.currentTimeMillis() + ".JPG"
mFilePath = filesDir.path + "/sketches"
File(mFilePath!!).mkdirs() //ensure that the file exists
File(cacheDir.path + "/sketches").mkdirs()
Expand Down Expand Up @@ -263,7 +267,7 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH), OnDia
val newFile = File("$path.new")
withContext(Dispatchers.IO) {
FileOutputStream(newFile).use {
bitmap.compress(Bitmap.CompressFormat.PNG, 90, it)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, it)
}
// Move new file to old location
newFile.renameTo(oldFile)
Expand Down

0 comments on commit 3ca58df

Please sign in to comment.