Can't set paint to canvas.drawBitmap() - android

I follow a tutorial and the code is error in the line of drawBitmap()
val paint = Paint()
paint.colorFilter = ColorFilter.lighting(Color.LightGray, Color.Magenta)
val canvas = android.graphics.Canvas(resultBmp)
canvas.drawBitmap(resultBmp,0f,0f, paint)
I tried but the solution doesn't work
val paint : Paint? = null

Related

Create border for custom drawable mask when image transform with Android

I have this masked transformation class, that can receive any mask:
class MaskedTransformation(private val maskDrawable: Drawable) : Transformation {
override fun key(): String = maskDrawable.hashCode().toString()
override fun transform(source: Bitmap): Bitmap? {
val result = Bitmap.createBitmap(source.width, source.height, Bitmap.Config.ARGB_8888)
val mCanvas = Canvas(result)
val o = BitmapFactory.Options()
o.inSampleSize = 2
val mask = Bitmap.createBitmap(source.width, source.height, Bitmap.Config.ARGB_8888)
val tmpCanvas = Canvas(mask)
val maskRaw9Patch = maskDrawable as? NinePatchDrawable
maskRaw9Patch?.setBounds(0, 0, tmpCanvas.width, tmpCanvas.height)
maskRaw9Patch?.draw(tmpCanvas)
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
mCanvas.drawBitmap(source, 0f, 0f, null)
mCanvas.drawBitmap(mask, 0f, 0f, paint)
paint.xfermode = null
source.recycle()
return result
}
For example, we can have this form: or any other.
How to add border (shape) around it? Please help!

Kotlin - thermal printer Arabic text never printed

Am using DantSu library to print receipts but Arabic text is never printed
here is my code
val printer = EscPosPrinter(
deviceConnection,
printerData.printerDpi,
printerData.printerWidthMM,
printerData.printerNbrCharactersPerLine,
EscPosCharsetEncoding("windows-1252", 16)
)
According to this comment on the issue on the library .. we found a solution but not totally correct
private fun getBitmap(text: String): Bitmap? {
val paint = Paint()
paint.setTextSize(28f)
val bounds = Rect()
paint.getTextBounds(text, 0, text.length, bounds)
val width: Int = bounds.width()
val height: Int = bounds.height()
val bitmap = Bitmap.createBitmap(width * 2, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.WHITE)
canvas.drawText(text, 20f, 20f, paint)
return bitmap
}

Path is filled with color instead showing as contour

I faced the following problem: I need to show the contour of the human face over bitmap. I have coordinates of the face contour points. Here's my code:
private fun detectContours(
it: FirebaseVisionFace,
icon: Bitmap
) {
val faceContour = it.getContour(FirebaseVisionFaceContour.FACE)
val mutableBitmap = icon.copy(
Bitmap.Config.ARGB_8888, true
)
val canvas = Canvas(mutableBitmap)
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
paint.color = Color.parseColor("#99ff0000")
val path = Path()
path.moveTo(faceContour.points[0].x, faceContour.points[0].y)
faceContour.points.forEach {
path.lineTo(it.x, it.y)
}
path.close()
canvas.drawPath(path, paint)
binding.ivPhoto.setImageBitmap(mutableBitmap)
}
Here I use path.moveTo(...) and I expect that mutableBitmap will look like a face with the contour. But when I run the app I see the following result:
So, why the path is filled with the colour. Thanks in advance, I will appreciate any help
Fixed by changing path.drawline to canvas.drawLine
val faceContour = it.getContour(FirebaseVisionFaceContour.FACE)
var startX = faceContour.points[0].x
var startY = faceContour.points[0].y
val initialX = faceContour.points[0].x
val initialY = faceContour.points[0].y
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
paint.color = Color.parseColor("#ff0000")
faceContour.points.forEach {
canvas.drawLine(startX, startY, it.x, it.y, paint)
startX = it.x
startY = it.y
}
canvas.drawLine(startX, startY, initialX, initialY, paint)

Draw transparent bitmap on backgound

I want to draw transparent bitmaps on a background in a game. However, the bitmap is not transparent, it still has white color in the background. Here is my code
inner class Canvass(context: Context) : View(context) {
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val frame_paint: Paint = Paint()
frame_paint.style = Paint.Style.FILL
frame_paint.isAntiAlias = true
val framegame = BitmapFactory.decodeResource (resources, R.drawable.frame)
canvas.drawBitmap (framegame, 0f, 0f, frame_paint)
canvas.drawPath(mPath, mPaint)
val tennis_paint: Paint = Paint()
tennis_paint.style = Paint.Style.FILL
tennis_paint.color = Color.TRANSPARENT
tennis_paint.isAntiAlias = true
val tennis = BitmapFactory.decodeResource (resources, R.drawable.tennis_ball)
canvas.drawBitmap (tennis, 50f, 200f, tennis_paint)
}
}
I found this code, it works for me.
val tennis_paint: Paint = Paint()
tennis_paint.style = Paint.Style.FILL
tennis_paint.isAntiAlias = true
val tennis = BitmapFactory.decodeResource (resources, R.drawable.tennis_ball)
var bitmap = transparent_bitmap(tennis)
canvas.drawBitmap (bitmap, 50f, 300f, tennis_paint)
fun transparent_bitmap(bmp:Bitmap):Bitmap{
var bitmap: Bitmap = Bitmap.createBitmap(
bmp.width,
bmp.height,
Bitmap.Config.ARGB_8888
)
bitmap.setHasAlpha(true)
for (x in 1..bmp.width-1){
for (y in 1..bmp.height-1){
var pixel = bmp.getPixel(x,y)
val redValue = Color.red(pixel)
val blueValue = Color.blue(pixel)
val greenValue = Color.green(pixel)
if ((redValue==255)and(blueValue==255)and(greenValue==255)){
bitmap.setPixel(x,y,Color.argb(1,0,0,0))
}else {
bitmap.setPixel(x,y,Color.rgb(redValue,greenValue,blueValue))
}
}
}
return bitmap
}

Clear canvas area with special bitmap's form

I develop a drawing application and I need to implement eraser tool to clear special areas of pictures. Eraser must erase some circle area unevenly. It means - only center of circle should be erased completely and edges of circle should be erased partially, with alpha.
To achieve this behaviour I created the image that representes eraser's figure
Then, to erase some area of view, I draw the eraser's figure above some picture like this:
private val picture: Bitmap
private val bgPaint = Paint()
private var eraser: Bitmap
private val eraserPaint: Paint = Paint().apply {
val mode: PorterDuff.Mode = PorterDuff.Mode.SRC_OUT
xfermode = PorterDuffXfermode(mode)
color = Color.TRANSPARENT
alpha = 0
}
init {
val options = BitmapFactory.Options()
options.inMutable = true
picture = BitmapFactory.decodeResource(resources, R.drawable.petushara, options)
eraser = BitmapFactory.decodeResource(resources, R.drawable.eraser, options)
setLayerType(View.LAYER_TYPE_SOFTWARE, bgPaint)
}
override fun draw(canvas: Canvas) {
super.draw(canvas)
canvas.drawBitmap(picture, 0f, 0f, bgPaint)
canvas.drawBitmap(eraser, 100f, 300f, eraserPaint)
}
This is whole my view-class, excluding constructors. I expect that area of picture will be erased by figure of eraser, but it erases by figure of square like this:
Okay, I just found out the solution. Write code like this:
private val picture: Bitmap
private val bgPaint = Paint()
private var eraser: Bitmap
private val eraserPaint: Paint = Paint().apply {
val mode: PorterDuff.Mode = PorterDuff.Mode.XOR
xfermode = PorterDuffXfermode(mode)
}
init {
val options = BitmapFactory.Options()
options.inMutable = true
picture = BitmapFactory.decodeResource(resources, R.drawable.petushara, options)
eraser = BitmapFactory.decodeResource(resources, R.drawable.eraser, options)
setLayerType(View.LAYER_TYPE_SOFTWARE, bgPaint)
}
override fun draw(canvas: Canvas) {
super.draw(canvas)
canvas.drawBitmap(picture, 0f, 0f, bgPaint)
canvas.drawBitmap(eraser, 100f, 300f, eraserPaint)
}
Then result picture looks like my expectations:

Categories

Resources