Skip to content

Commit

Permalink
feat: Export images with padding and title
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten-Renner committed Jul 15, 2024
1 parent c9c9169 commit 8b0fa48
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.net.Uri
import android.os.Build
Expand Down Expand Up @@ -58,6 +61,7 @@ import kotlin.properties.Delegates
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.lang.Float.min

class PinViewerFragment : Fragment() {
private var _binding: FragmentPinViewerBinding? = null
Expand Down Expand Up @@ -91,7 +95,7 @@ class PinViewerFragment : Fragment() {
)
)
) {
generateAndExportImage(uri)
generateAndExportImage(uri, args.pin)
}
}
}
Expand Down Expand Up @@ -204,7 +208,71 @@ class PinViewerFragment : Fragment() {
return pinTable
}

private fun generateAndExportImage(uri: Uri) {
/**
* Adds a white frame, a PINcredible watermark and a title text to the original bitmap.
*
* The text size is dynamically adjusted to fit within the frame.
*/
private fun frameAndTitleBitmap(bitmap: Bitmap, title: String): Bitmap {
val frameThickness = 20
val titlePadding = bitmap.height / 20
val watermarkPadding = bitmap.height / 40
val paint = Paint()

// Draws from top to bottom
var currentY = 0f

// Dynamically adjust the text size to fit within the frame
val maxTextWidth = bitmap.width - (2 * frameThickness)
val textWidth = paint.measureText(title) // Measure the text width
paint.textSize *= (maxTextWidth / textWidth) // Adjust the text size to fit the text width
paint.textSize = min(
paint.textSize,
bitmap.height / 15f
) // Limit the text size to 1/15 of the bitmap height

// Calculate the new size for the bitmap with frame and watermark space
val newBitmapWidth = bitmap.width + (frameThickness * 2)
val newBitmapHeight =
bitmap.height + (frameThickness * 2) + paint.textSize.toInt() + titlePadding + watermarkPadding

// Create a new bitmap with the new size
val framedBitmap =
Bitmap.createBitmap(newBitmapWidth, newBitmapHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(framedBitmap)

// Draw the white frame
paint.color = Color.WHITE
canvas.drawRect(0f, 0f, newBitmapWidth.toFloat(), newBitmapHeight.toFloat(), paint)

// Draw the title
paint.color = Color.BLACK
paint.textAlign = Paint.Align.CENTER
canvas.drawText(title, newBitmapWidth / 2f, paint.textSize + titlePadding / 2f, paint)
currentY += titlePadding + paint.textSize

// Draw the original bitmap onto the new bitmap with the frame
canvas.drawBitmap(bitmap, frameThickness.toFloat(), (currentY), paint)
currentY += bitmap.height

// Draw the watermark
paint.color = Color.DKGRAY
paint.textSize = bitmap.width / 40f
context?.let {
canvas.drawText(
it.getString(R.string.export_watermark),
newBitmapWidth / 2f,
currentY + paint.textSize + watermarkPadding / 2,
paint
)
}
currentY += watermarkPadding + paint.textSize


return framedBitmap
}

private fun generateAndExportImage(uri: Uri, title: String) {
val tableView = binding.tableView
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val bitmap = Bitmap.createBitmap(
Expand All @@ -226,7 +294,7 @@ class PinViewerFragment : Fragment() {
bitmap,
{ copyResult ->
if (copyResult == PixelCopy.SUCCESS) {
saveImage(bitmap, uri)
saveImage(frameAndTitleBitmap(bitmap, title), uri)
}
},
Handler(Looper.getMainLooper())
Expand All @@ -237,7 +305,7 @@ class PinViewerFragment : Fragment() {
} else {
TableScreenshotHandler.generateTableCacheCopy(tableView) {
if (it != null) {
saveImage(it, uri)
saveImage(frameAndTitleBitmap(it, title), uri)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@
<string name="dialog_restart_title">Restart required</string>
<string name="dialog_restart_message">The change will be visible on next app start.\n\nDo you want to restart now?</string>
<string name="dialog_restart_button2">Later</string>
<string name="export_watermark">cyb3rko / PINcredible</string>
</resources>

0 comments on commit 8b0fa48

Please sign in to comment.