在Android 4.4中裁剪相机意图

aij0ehis  于 2023-03-06  发布在  Android
关注(0)|答案(2)|浏览(131)

如何在Android 4.4中裁剪相机意图?我可以使用以下选项裁剪低于4.4的相机意图

Intent pictureActionIntent = new Intent(
                            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < 19) {
    pictureActionIntent.putExtra("crop", "true");
    pictureActionIntent.putExtra("outputX", 100);
    pictureActionIntent.putExtra("outputY", 120);
    pictureActionIntent.putExtra("aspectX", 1);
    pictureActionIntent.putExtra("aspectY", 1);
    pictureActionIntent.putExtra("scale", true);
    pictureActionIntent.putExtra("return-data", true);
} else {

}

startActivityForResult(pictureActionIntent, 1);

但是在Python 4.4中,它不能在else部分工作。

wlp8pajw

wlp8pajw1#

请尝试以下代码

class CameraActivity : Activity() {
    val CAMERA_CAPTURE = 1
    val CROP_PIC = 2
    private var picUri: Uri? = null
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.camera_activity)
        val cropButton = findViewById<Button>(R.id.cropButton)
        cropButton.setOnClickListener(this)
    }

    fun onClick(v: View) {
        if (v.id == R.id.capture_btn) {
            try {
                val captureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                startActivityForResult(captureIntent, CAMERA_CAPTURE)
            } catch (exception: ActivityNotFoundException) {
                exception.printStackTrace()
                Toast.makeText(this, "exception while cropping", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        if (resultCode == RESULT_OK) {
            if (requestCode == CAMERA_CAPTURE) {
                picUri = data.data
                doCropping()
            } else if (requestCode == CROP_PIC) {
                val extras = data.extras
                val thePic = extras!!.getParcelable<Bitmap>("data")
                val picView = findViewById<ImageView>(R.id.imageView)
                picView.setImageBitmap(thePic)
            }
        }
    }

    private fun doCropping() {
        try {
            val cropIntent = Intent("com.android.camera.action.CROP")
            cropIntent.setDataAndType(picUri, "image/*")
            cropIntent.putExtra("crop", "true")
            cropIntent.putExtra("aspectX", 2)
            cropIntent.putExtra("aspectY", 1)
            cropIntent.putExtra("outputX", 256)
            cropIntent.putExtra("outputY", 256)
            cropIntent.putExtra("return-data", true)
            startActivityForResult(cropIntent, CROP_PIC)
        } catch (exception: ActivityNotFoundException) {
            exception.printStackTrace()
            Toast.makeText(this, "exception while cropping", Toast.LENGTH_SHORT).show()
        }
    }
}
sgtfey8w

sgtfey8w2#

Android中有多个用于裁剪图像的开源库。来自应用的Intent操作可能不存在于任何给定用户的设备上。
下面是一些需要考虑的库:

相关问题