我找到了一个用于擦除背景的库。我将代码从库复制到我的项目中。我想要另一个实现。我想要当用户单击按钮来打开相机并且结果可以擦除背景时。你能帮我吗对不起我的英语不好。

package com.example.backgrounderaser

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.github.gabrielbb.cutout.CutOut
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private companion object {
        private const val REQUEST_CODE_FOR_CAMERA = 191
        private const val CUTOUT_ACTIVITY_REQUEST_CODE = 368
        private lateinit var uri: Uri
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val imageIconUri: Uri = getUriFromDrawable(R.drawable.image_icon)
        imageView.setImageURI(imageIconUri)
        imageView.tag = imageIconUri

        fab.setOnClickListener {
            val testImageUri: Uri = getUriFromDrawable(R.drawable.test_image)
            CutOut.activity()
                .src(testImageUri)
                .bordered()
                .noCrop()
                .start(this)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == CUTOUT_ACTIVITY_REQUEST_CODE) {
            val imageUri = CutOut.getUri(data)
            // Save the image using the returned Uri here
            // Save the image using the returned Uri here
            imageView.setImageURI(imageUri)
            imageView.tag = imageUri
        }
    }

    private fun getUriFromDrawable(drawableId: Int): Uri? {
        return Uri.parse(
            "android.resource://$packageName/drawable/" + applicationContext.resources.getResourceEntryName(
                drawableId
            )
        )
    }
}

最佳答案

1-为了使事情变得简单,请添加以下库:EasyImage

   implementation 'com.github.jkwiecien:EasyImage:3.0.4'
2-这是有关如何使用库来满足您的需求的完整代码
class MainActivity  : AppCompatActivity() {
    lateinit var easyImage: EasyImage
    lateinit var capture: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        capture = findViewById(R.id.camera)

        capture.setOnClickListener {
            openCamera()
        }
    }

    private fun openCamera() {
        easyImage = EasyImage.Builder(applicationContext).build()
        easyImage.openCameraForImage(this@MainActivity )
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            easyImage.handleActivityResult(requestCode, resultCode, data, this@MainActivity , object : EasyImage.Callbacks {
                override fun onCanceled(source: MediaSource) {

                }

                override fun onImagePickerError(error: Throwable, source: MediaSource) {

                }

                override fun onMediaFilesPicked(imageFiles: Array<MediaFile>, source: MediaSource) {
                    //imageFiles[0].file
                    //imageFiles[0].file is the captured Image, use it as you want

                 //  val drawable = Drawable.createFromPath(imageFiles[0].file.absolutePath)
                 //  img.setImageDrawable(drawable)

                        CutOut.activity()
                        .src(Uri.fromFile(imageFiles[0].file))
                        .bordered()
                        .noCrop()
                        .start(this)

                }
            })
        }
    }
}

10-04 20:18