栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

目录

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
                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中注册一下就可以了

 

再来!

点击完成:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/836479.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号