通过第三个将一个位图合并到另一个位图

Merge one bitmap to another via the third

我有两张图片 - 一张(“背面”)显示为背景,另一张(“粘贴”)供用户拖动。 “后退”显示有一些缩放比例(图像视图上的 match_parent),因此实际上有 3 张图像在播放。我需要将“粘贴”应用于第三个(原始)图像,其位置与显示在“背面”上的位置完全相同。我试过用矩阵来做,但手动计算偏移量时运气更好,但它仍然不够,因为出现了一些奇怪的偏移量,“棒”是左边的一些像素。这甚至不是说如果图像更水平,它就不起作用。以下是我的计算方式:

val stickerBitmap = stickerView.drawToBitmap().trimBorders(Color.argb(0, 0, 0, 0))
val originalBitmap = fa
var purePageView = pageView.drawToBitmap()
val displayedBitmap = pageView.drawToBitmap().trimBorders(Color.argb(0, 0, 0, 0))
var (h1, w1) = stickerView.bounds
w1 -= binding.toolbar.height + binding.pageView.marginTop
h1 -= (binding.pageView.marginStart + binding.pageView.marginEnd + (purePageView.width - displayedBitmap.width)/2)
val x = originalBitmap.height
val y = originalBitmap.width
val x1 = displayedBitmap.height
val y1 = displayedBitmap.width
val x2 = stickerBitmap.height
val y2 = stickerBitmap.width
val h = h1 * y / y1.toFloat()
val w = x * w1 / x1.toFloat()
val x3 = x2 * x / x1
val y3 = y2 * y / y1
val scaledSticker2 = Bitmap.createScaledBitmap(stickerBitmap, y3, x3, true)
val resultBitmap = Bitmap.createBitmap(
    originalBitmap.width,
    originalBitmap.height,
    Bitmap.Config.ARGB_8888
)
val resultCanvas = Canvas(resultBitmap)
val paint = Paint()
var cf = PorterDuffColorFilter(Color.argb(255,0,255,0), PorterDuff.Mode.SRC_IN)
paint.colorFilter = cf
resultCanvas.drawBitmap(originalBitmap, 0f, 0f, null)

cf = PorterDuffColorFilter(Color.argb(255,0,255,255), PorterDuff.Mode.SRC_IN)
paint.colorFilter = cf
resultCanvas.drawBitmap(scaledSticker2, h + binding.pageView.marginStart, w+abs(binding.pageView.marginTop-binding.pageView.marginBottom), paint)

cf = PorterDuffColorFilter(Color.argb(255,0,123,123), PorterDuff.Mode.SRC_IN)
paint.colorFilter = cf
resultCanvas.drawBitmap(scaledSticker2, h + binding.pageView.marginStart, w+abs(binding.pageView.marginTop-binding.pageView.marginBottom), paint)

来自 trimBorders 代码。可能缺少什么?有完全不同的方法吗?

最后,需要减去图像视图和父视图之间的任何附加 space 以及图像视图内部视图和图像之间的附加 space。我修改 trimBitmap 也 return 找到图像顶部和左侧的绳索,并对减法结果进行缩放。

val originalBitmap = BitmapFactory.decodeFile(
    File(
        context.filesDir,
        originalName
    ).path,
    BitmapFactory.Options().apply { inPreferredConfig = Bitmap.Config.ARGB_8888 })
val swb = IntArray(2)
val stickerBitmap = stickerView.drawToBitmap().trimBorders(Color.argb(0, 0, 0, 0), swb)
val displayedBitmap = pageView.drawToBitmap().trimBorders(Color.argb(0, 0, 0, 0))
val (w1, h1) = swb
val wB = (pageView.width - displayedBitmap.width) / 2
val hB = (pageView.height - displayedBitmap.height) / 2
val x = originalBitmap.height
val y = originalBitmap.width
val x1 = displayedBitmap.height
val y1 = displayedBitmap.width
val x2 = stickerBitmap.height
val y2 = stickerBitmap.width
val h = (h1 - (toolbar.height + pageView.marginTop + hB)) * y / y1.toFloat()
val w = x * (w1 - (wB + pageView.marginStart)) / x1.toFloat()
val x3 = x2 * x / x1
val y3 = y2 * y / y1
val scaledSticker2 = Bitmap.createScaledBitmap(stickerBitmap, y3, x3, true)
val resultBitmap = Bitmap.createBitmap(
    originalBitmap.width,
    originalBitmap.height,
    Bitmap.Config.ARGB_8888
)
val resultCanvas = Canvas(resultBitmap)

resultCanvas.drawBitmap(originalBitmap, 0f, 0f, null)
resultCanvas.drawBitmap(scaledSticker2, w, h, null)