Skip to content

Instantly share code, notes, and snippets.

@f1dz
Created February 8, 2021 08:52
Show Gist options
  • Save f1dz/331f71694ea6344287fcfd8ee7dd2a86 to your computer and use it in GitHub Desktop.
Save f1dz/331f71694ea6344287fcfd8ee7dd2a86 to your computer and use it in GitHub Desktop.
Image Watermark
class Watermark {
/**
* Put watermark into the photo
*/
private fun watermark() {
doAsync {
val photoDir = scanViewModel!!.session.name
val maxResolution = 1024
val bmp = MediaStore.Images.Media.getBitmap(contentResolver, fileUri)
var height = maxResolution
var width = maxResolution
if (bmp!!.width > bmp.height)
height = bmp.height * maxResolution / bmp.width
else width = bmp.width * maxResolution / bmp.height
val scaledBmp: Bitmap = Bitmap.createScaledBitmap(bmp, width, height, true)
val photo = File(FileHelper.getPhotoDir().path + "/" + photoDir)
try {
if (!photo.exists())
if (!photo.mkdir())
Log.e("WATERMARK", "Cannot create photo dir")
} catch (e: Exception) {
Log.e("WATERMARK", e.localizedMessage)
}
val fileName = "$fileName.jpg"
try {
val matrix = Matrix()
matrix.postRotate(90F)
val result: Bitmap = Bitmap.createBitmap(scaledBmp, 0, 0, scaledBmp.width, scaledBmp.height, matrix, true)
val canvas = Canvas(result)
canvas.drawBitmap(result, 0F, 0F, null)
val data = "PUT YOUR TEXT HERE"
val writer = MultiFormatWriter()
val bm: BitMatrix = writer.encode(Crypto.encrypt(data), BarcodeFormat.QR_CODE, 150, 150)
val bmpQR: Bitmap = Bitmap.createBitmap(150, 150, Bitmap.Config.ARGB_8888)
for (i in 0..149) {
for (j in 0..149) {
val color = if (bm.get(i, j)) Color.BLACK else Color.WHITE
bmpQR.setPixel(i, j, color)
}
}
val hPoint = width - 175
val textSize = 20F
val bmpRfid: Bitmap = ImageProcessor.textAsBitmap(rfid, textSize, Color.YELLOW)
val bmpDateTime: Bitmap = ImageProcessor.textAsBitmap(scan!!.createdAt, textSize, Color.YELLOW)
val bmpAbattoir: Bitmap = ImageProcessor.textAsBitmap(abattoir!!, textSize, Color.YELLOW)
val bmpLatitude: Bitmap = ImageProcessor.textAsBitmap(scan!!.latitude.toString(), textSize, Color.YELLOW)
val bmpLongitude: Bitmap = ImageProcessor.textAsBitmap(scan!!.longitude.toString(), textSize, Color.YELLOW)
val bmpCopyright: Bitmap = ImageProcessor.textAsBitmap(getString(R.string.copyright), textSize, Color.YELLOW)
canvas.drawBitmap(bmpQR, 25F, hPoint.toFloat(), null)
canvas.drawBitmap(bmpRfid, 185F, hPoint.toFloat(), null)
canvas.drawBitmap(bmpDateTime, 185F, hPoint.toFloat() + 25F, null)
canvas.drawBitmap(bmpAbattoir, 185F, hPoint.toFloat() + 50F, null)
canvas.drawBitmap(bmpLatitude, 185F, hPoint.toFloat() + 75F, null)
canvas.drawBitmap(bmpLongitude, 185F, hPoint.toFloat() + 100F, null)
canvas.drawBitmap(bmpCopyright, 185F, hPoint.toFloat() + 125F, null)
val file = File(photo, fileName)
file.createNewFile()
val fileOutputStream = FileOutputStream(file)
val bos = BufferedOutputStream(fileOutputStream)
result.compress(Bitmap.CompressFormat.JPEG, 100, bos)
bos.flush()
bos.close()
file.absolutePath
//delete original file
val original = URI("file:${FileHelper.getBaseDir()}/tmp/${fileUri!!.lastPathSegment}")
File(original).delete()
System.gc()
} catch (e: Exception) {
Log.e("WATERMARK", e.localizedMessage)
}
}
}
}
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
class ImageProcessor {
companion object {
fun textAsBitmap(text: String, textSize: Float, textColor: Int): Bitmap {
val textPadding = " $text "
val paint = Paint()
paint.textSize = textSize
paint.color = textColor
paint.textAlign = Paint.Align.LEFT
val width = (paint.measureText(textPadding) + 0.5F)
val baseline = (-paint.ascent() + 0.5F)
val height = (baseline + paint.descent() + 0.5f)
val image: Bitmap = Bitmap.createBitmap(width.toInt(), height.toInt(), Bitmap.Config.RGB_565)
val canvas = Canvas(image)
canvas.drawText(textPadding, 0F, baseline, paint)
return image
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment