使用 AI 或在 android studio 中自动删除图像背景,准确率 100%

问题描述 投票:0回答:1

** 如何在android studio中使用AI自动删除图像背景? 以上是我的问题,我将提供如何以 100% 准确率去除图像背景的答案。

在此方法中,我将免费使用 Google MLKit API,在 Android Stduio 中使用 Java/Kotlin 中的位图使用人工智能来删除图像背景**

bitmap artificial-intelligence
1个回答
0
投票
implementation 'com.google.mlkit:segmentation-selfie:16.0.0-beta4'

private fun PerfoamAuto()
{
    loader.visibility=View.VISIBLE
    var bitmapFromContentUri=YourBitmap
    val client: Segmenter = Segmentation.getClient(SelfieSegmenterOptions.Builder().setDetectorMode(SINGLE_IMAGE_MODE).build())
    client.process(InputImage.fromBitmap(bitmapFromContentUri, 0))
        .addOnSuccessListener(object : OnSuccessListener<SegmentationMask?> {
            override fun onSuccess(segmentationMask: SegmentationMask?) {
                val buffer: ByteBuffer = segmentationMask!!.getBuffer()
                val width: Int = segmentationMask.getWidth()
                val height: Int = segmentationMask.getHeight()
                val createBitmap = Bitmap.createBitmap(bitmapFromContentUri.width, bitmapFromContentUri.height, bitmapFromContentUri.config)
                for (i in 0 until height) {
                    for (i2 in 0 until width) {
                        val d = buffer.float.toDouble()
                        java.lang.Double.isNaN(d)
                        createBitmap.setPixel(i2, i, Color.argb(((1.0 - d) * 255.0).toInt(), 0, 0, 0))
                    }
                }
                buffer.rewind()
                autoeraseimage= mergeToPinBitmap(bitmapFromContentUri, createBitmap)
                if (autoeraseimage != null) {
                    // Now set your auto eraseimagebitmap to your imageview
                    drawView.setBitmap(autoeraseimage)
                    loader.visibility = View.GONE

                } else {
                    loader.visibility = View.GONE
                    Toast.makeText(this@BackGroundRemoverScreen,resources.getString(R.string.please_try_again),Toast.LENGTH_SHORT).show()
                }
            }


        }).addOnFailureListener(object : OnFailureListener {
            override fun onFailure(e: Exception) {
                Toast.makeText(this@BackGroundRemoverScreen,resources.getString(R.string.please_try_again),Toast.LENGTH_SHORT).show()
                e.message
                loader.visibility = View.GONE
            }

        })
}

fun mergeToPinBitmap(bitmap: Bitmap, bitmap2: Bitmap): Bitmap {
    val createBitmap =
        Bitmap.createBitmap(bitmap2.width, bitmap2.height, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(createBitmap)
    val paint = Paint(1)
    paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OUT)
    canvas.drawBitmap(bitmap, 0.0f, 0.0f, null as Paint?)
    canvas.drawBitmap(bitmap2, 0.0f, 0.0f, paint)
    paint.xfermode = null as Xfermode?
    /*bitmap2.recycle()
    bitmap.recycle()
    */return createBitmap
}
© www.soinside.com 2019 - 2024. All rights reserved.