I have an image and i want to add a signature image on it in the position that the user place it on the screen and press "save".
I have tried in this way:
fun generateComposedImage(originalImage: Bitmap, signatureImage:Bitmap,location: Offset,signatureScreen:SizeF,imageScreenSize:Size, zstackScreenSize: Size): ImageBitmap {
val composedBitmap = Bitmap.createBitmap(originalImage.width, originalImage.height, originalImage.config)
val canvas = Canvas(composedBitmap)
val paint = Paint().apply {
isAntiAlias = true
}
canvas.drawBitmap(originalImage, 0f, 0f, paint)
// Calculate the position to draw the signature image
val signatureX = ((location.x * originalImage.width) / imageScreenSize.width)
-(signatureScreen.width*originalImage.width/imageScreenSize.width)/2-
((zstackScreenSize.width-imageScreenSize.width)*originalImage.width/imageScreenSize.width)/2
val signatureY = ((location.y* originalImage.height) / imageScreenSize.height)-
(signatureScreen.height*originalImage.height/imageScreenSize.height)/2-
((zstackScreenSize.height-imageScreenSize.height)*originalImage.height/imageScreenSize.height)/2
val signatureWidth = (signatureScreen.width * originalImage.width / imageScreenSize.width)
val signatureHeight = (signatureScreen.height * originalImage.height / imageScreenSize.height)
canvas.drawBitmap(
signatureImage,
Rect(0, 0, signatureImage.width, signatureImage.height),
RectF(signatureX, signatureY,
((signatureX + signatureWidth + signatureImage.width/2f )),
((signatureY + signatureHeight +signatureImage.height/2f ))),
paint
)
return composedBitmap.asImageBitmap()
}
this code genereate me the image with the signature on it but the signature is little bit far from where i place it. in addition , in some images its increase me the size of the signature Any suggestion to fix it?