How to erase and restore images in Android?

I have an image, I want to delete pixels with drawing lines, I have done this successfully, however when I want to further develop the function of restoring deleted pixels with drawing lines, I really deadlocked and still haven’t found a solution for several days today. I look forward to receiving everyone’s help, thank you very much, I wish everyone a good day.

this is my class

 import android.content.Context
 import android.graphics.*
 import android.os.AsyncTask
 import android.util.AttributeSet
 import android.util.Pair
 import android.view.MotionEvent
 import android.view.View
 import android.widget.Button
 import java.lang.ref.WeakReference
 import java.util.*
 
 internal class DrawView(c: Context?, attrs: AttributeSet?) : View(c, attrs) {
private var livePath: Path
private var pathPaint: Paint
var currentBitmap: Bitmap? = null
    private set
private val cuts = Stack<Pair<Pair<Path, Paint>?, Bitmap?>>()
private val undoneCuts = Stack<Pair<Pair<Path, Paint>?, Bitmap?>>()
private var pathX = 0f
private var pathY = 0f
private var undoButton: Button? = null
private var redoButton: Button? = null
private var loadingModal: View? = null
private var currentAction: DrawViewAction? = null

enum class DrawViewAction {
    AUTO_CLEAR, MANUAL_CLEAR, ZOOM
}

fun setButtons(undoButton: Button?, redoButton: Button?) {
    this.undoButton = undoButton
    this.redoButton = redoButton
}

override fun onSizeChanged(newWidth: Int, newHeight: Int, oldWidth: Int, oldHeight: Int) {
    super.onSizeChanged(newWidth, newHeight, oldWidth, oldHeight)
    resizeBitmap(newWidth, newHeight)
}

override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    canvas.save()
    if (currentBitmap != null) {
        canvas.drawBitmap(currentBitmap!!, 0f, 0f, null)
        for (action in cuts) {
            if (action.first != null) {
                canvas.drawPath(action.first!!.first, action.first!!.second)
            }
        }
        if (currentAction == DrawViewAction.MANUAL_CLEAR) {
            canvas.drawPath(livePath, pathPaint)
        }
    }
    canvas.restore()
}

private fun touchStart(x: Float, y: Float) {
    pathX = x
    pathY = y
    undoneCuts.clear()
    redoButton!!.isEnabled = false
    if (currentAction == DrawViewAction.AUTO_CLEAR) {
        AutomaticPixelClearingTask(this).execute(
            x.toInt(), y.toInt()
        )
    } else {
        livePath.moveTo(x, y)
    }
    invalidate()
}

private fun touchMove(x: Float, y: Float) {
    if (currentAction == DrawViewAction.MANUAL_CLEAR) {
        val dx = Math.abs(x - pathX)
        val dy = Math.abs(y - pathY)
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            livePath.quadTo(pathX, pathY, (x + pathX) / 2, (y + pathY) / 2)
            pathX = x
            pathY = y
        }
    }
}

private fun touchUp() {
    if (currentAction == DrawViewAction.MANUAL_CLEAR) {
        livePath.lineTo(pathX, pathY)
        cuts.push(Pair(Pair(livePath, pathPaint), null))
        livePath = Path()
        undoButton!!.isEnabled = true
    }
}

fun undo() {
    if (cuts.size > 0) {
        val cut = cuts.pop()
        if (cut.second != null) {
            undoneCuts.push(
                Pair(
                    null, currentBitmap
                )
            )
            currentBitmap = cut.second
        } else {
            undoneCuts.push(cut)
        }
        if (cuts.isEmpty()) {
            undoButton!!.isEnabled = false
        }
        redoButton!!.isEnabled = true
        invalidate()
    }
    //toast the user
}

fun redo() {
    if (undoneCuts.size > 0) {
        val cut = undoneCuts.pop()
        if (cut.second != null) {
            cuts.push(
                Pair(
                    null, currentBitmap
                )
            )
            currentBitmap = cut.second
        } else {
            cuts.push(cut)
        }
        if (undoneCuts.isEmpty()) {
            redoButton!!.isEnabled = false
        }
        undoButton!!.isEnabled = true
        invalidate()
    }
    //toast the user
}

override fun onTouchEvent(ev: MotionEvent): Boolean {
    if (currentBitmap != null && currentAction != DrawViewAction.ZOOM) {
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                touchStart(ev.x, ev.y)
                return true
            }

            MotionEvent.ACTION_MOVE -> {
                touchMove(ev.x, ev.y)
                invalidate()
                return true
            }

            MotionEvent.ACTION_UP -> {
                touchUp()
                invalidate()
                return true
            }
        }
    }
    return super.onTouchEvent(ev)
}

private fun resizeBitmap(width: Int, height: Int) {
    if (width > 0 && height > 0 && currentBitmap != null) {
        currentBitmap = BitmapUtility.getResizedBitmap(currentBitmap!!, width, height)
        currentBitmap!!.setHasAlpha(true)
        invalidate()
    }
}

fun setBitmap(bitmap: Bitmap?) {
    currentBitmap = bitmap
    resizeBitmap(width, height)
}

fun setAction(newAction: DrawViewAction?) {
    currentAction = newAction
}

fun setStrokeWidth(strokeWidth: Int) {
    pathPaint = Paint(pathPaint)
    pathPaint.strokeWidth = strokeWidth.toFloat()
}

fun setLoadingModal(loadingModal: View?) {
    this.loadingModal = loadingModal
}

private class AutomaticPixelClearingTask(drawView: DrawView) :
    AsyncTask<Int?, Void?, Bitmap>() {
    private val drawViewWeakReference: WeakReference<DrawView> = WeakReference(drawView)
    override fun onPreExecute() {
        super.onPreExecute()
        drawViewWeakReference.get()!!.loadingModal!!.visibility = VISIBLE
        drawViewWeakReference.get()!!.cuts.push(
            Pair(
                null, drawViewWeakReference.get()!!.currentBitmap
            )
        )
    }

    override fun doInBackground(vararg points: Int?): Bitmap? {
        val oldBitmap = drawViewWeakReference.get()!!.currentBitmap
        val colorToReplace = oldBitmap!!.getPixel(points[0]!!, points[1]!!)
        val width = oldBitmap.width
        val height = oldBitmap.height
        val pixels = IntArray(width * height)
        oldBitmap.getPixels(pixels, 0, width, 0, 0, width, height)
        val rA = Color.alpha(colorToReplace)
        val rR = Color.red(colorToReplace)
        val rG = Color.green(colorToReplace)
        val rB = Color.blue(colorToReplace)
        var pixel: Int

        // iteration through pixels
        for (y in 0 until height) {
            for (x in 0 until width) {
                // get current index in 2D-matrix
                val index = y * width + x
                pixel = pixels[index]
                val rrA = Color.alpha(pixel)
                val rrR = Color.red(pixel)
                val rrG = Color.green(pixel)
                val rrB = Color.blue(pixel)
                if (rA - COLOR_TOLERANCE < rrA && rrA < rA + COLOR_TOLERANCE && rR - COLOR_TOLERANCE < rrR && rrR < rR + COLOR_TOLERANCE && rG - COLOR_TOLERANCE < rrG && rrG < rG + COLOR_TOLERANCE && rB - COLOR_TOLERANCE < rrB && rrB < rB + COLOR_TOLERANCE) {
                    pixels[index] = Color.TRANSPARENT
                }
            }
        }
        val newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        newBitmap.setPixels(pixels, 0, width, 0, 0, width, height)
        return newBitmap
    }

    override fun onPostExecute(result: Bitmap) {
        super.onPostExecute(result)
        drawViewWeakReference.get()!!.currentBitmap = result
        drawViewWeakReference.get()!!.undoButton!!.isEnabled = true
        drawViewWeakReference.get()!!.loadingModal!!.visibility = INVISIBLE
        drawViewWeakReference.get()!!.invalidate()
    }

}

companion object {
    private const val TOUCH_TOLERANCE = 4f
    private const val COLOR_TOLERANCE = 20f
}

init {
    livePath = Path()
    pathPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    pathPaint.isDither = true
    pathPaint.color = Color.TRANSPARENT
    pathPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
    pathPaint.style = Paint.Style.STROKE
    pathPaint.strokeJoin = Paint.Join.ROUND
    pathPaint.strokeCap = Paint.Cap.ROUND
}
}

I want it to look like this

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật