android使用android-crop实现任意比例的图片裁剪

android使用android-crop实现任意比例的图片裁剪,第1张

目录

1. 序言

1. 需求

2. 环境

2. 功能实现

1. 导包

2.  更改工具类  

3. 直接使用!!

 4. 运行


1. 序言 1. 需求

        毕设是关于翻译的一个Android app,这就包含了图片的翻译,虽然我使用的是有道智云的翻译API,但是Android端对于图片的预处理还是要自己来实现。前面我也写道过利用Android自带的裁剪功能实现简易的图片裁剪,文章地址,但是对于任意比例这个要求,还是小巫见大巫,没什么实质性的作用。

2. 环境

Android studio2021.1.1,Android 11,语言用的Kotlin。

秉着我可以不用,但是我不能不会的想法,我花了1天的时间看了郭霖大神的《第一行代码》第三版其中的kotlin部分,学会了一些皮毛基础。然后发现Kotlin相对于Java比较简洁,再加上Google官方推荐使用Kotlin所以我就转型使用Kotlin来编写项目。(还有一件事)我在该项目中没有使用已经被弃用的startActivityForResult和OnActivityResult方法,而是使用最新推荐的registerForActivityResult(registerForActivityResult的相关文章),如果你想使用startActivityForResult可以参考这篇文章

2. 功能实现 1. 导包

        在module层的build.gradle添加依赖

    implementation 'com.soundcloud.android:android-crop:1.0.1@aar'

        这里使用的是GitHub上大佬的项目包,相关地址

2.  更改工具类  

        因为我是用的registerForActivityResult,所以我改写了一个GitHub大佬的一个工具类。

import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import com.soundcloud.android.crop.CropImageActivity

class MyCrop private constructor(source: Uri, destination: Uri) {
    private val cropIntent = Intent()

    fun asSquare(): MyCrop {
        cropIntent.putExtra("aspect_x", 1)
        cropIntent.putExtra("aspect_y", 1)
        return this
    }

    fun start(activity: Activity, crop: ActivityResultLauncher) {
        //  activity.startActivityForResult(this.getIntent(activity), requestCode);
        crop.launch(getIntent(activity))
    }

    private fun getIntent(context: Context): Intent {
        cropIntent.setClass(context, CropImageActivity::class.java)
        return cropIntent
    }

    companion object {
        const val RESULT_ERROR = 404
        fun of(source: Uri, destination: Uri): MyCrop {
            return MyCrop(source, destination)
        }

        fun getError(result: Intent): Throwable {
            return result.getSerializableExtra("error") as Throwable
        }

        fun pickImage(activity: Activity, pick: ActivityResultLauncher) {
            try {
                pick.launch("image/*")
            } catch (`var`: ActivityNotFoundException) {
                showImagePickerError(activity)
            }
        }

        private fun showImagePickerError(context: Context) {
            Toast.makeText(context, "No image sources available", Toast.LENGTH_SHORT).show()
        }
    }

    init {
        cropIntent.data = source
        cropIntent.putExtra("output", destination)
    }
}

        可以对比一下添加依赖得到的Crop.class,这里我不做详细说明,就是简单的改了一下startActivityForResult的相关代码。

3. 直接使用!!

 新建项目什么的我就不多说了。直接上使用的代码。自己写一个按钮点击响应函数

MyCrop.pickImage(this, pick)///工具类方法,使用Android自带的获取图片功能

其中pick是我定义的一个ActivityResultLauncher 变量

private lateinit var pick: ActivityResultLauncher

这个变量的初始化一定要在OnCreat函数中。

        pick = registerForActivityResult(ActivityResultContracts.GetContent()){
            if (it != null) {
                /**
                 * 获取到的图片Uri就是返回回来的it
                 */
                beginCrop(it)
            }
        }

beginCrop函数:

    private fun beginCrop(source: Uri) {
        val uriFile = File(filesDir, "cropped.jpeg") //这是app私有的文件存储路径
        val uriTo = Uri.fromFile(uriFile) //私有的路径可以直接用Uri.fromFile获取相应的Uri
        MyCrop.of(source, uriTo).start(this, crop)
    }

crop是我定义的一个ActivityResultLauncher 变量(和pick是差不多的):

private lateinit var crop: ActivityResultLauncher

/**
* 初始化
*/
crop = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
    Glide.with(this).load(filesDir.absolutePath + "/cropped.jpeg").into(binding.image)
}

binding.image是一个ImageView控件(关于Viewbinding可以自己查一下,Java Kotlin都可以用),当然也可以直接findViewById

使用的Glide是一款强大的图片显示开源工具,需要添加相应依赖:

    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

最后就是别忘了在AndroidManifest中添加权限:

还需要动态添加读取权限,这里就不做赘叙。可以看我之前的文章

 4. 运行

话不多说直接运行

 点击相册中的图片时,app直接闪退,看报错是

Unable to find explicit activity class {com.example.test/com.soundcloud.android.crop.CropImageActivity}; have you declared this activity in your AndroidManifest.xml?

很明显,它说我没在清单中注册这个activity(com.soundcloud.android.crop.CropImageActivity)这个是android-crop的一个用于裁剪的activity,所以在AndroidManifest中注册一下就可以了

再来!

点击完成:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/735661.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存