Giter Club home page Giter Club logo

coco's Introduction

CoCo

Hex.pm Hex.pm Hex.pm Hex.pm

一款小而美的的Android系统相机拍照和系统相册选择库🐵:

  • 一行代码完成从系统相机拍照或者系统相册选择图片
  • 内部适配 7.0 FileProvider文件处理,无需自己额外处理
  • 默认图片处理器自带两种图片压缩策略,并可按需自定义图片处理器
  • 支持Activity、Fragment,图片异步处理器工作自动绑定相关生命周期
  • 完全基于Kotlin编写,与Java兼容
  • 全面适配AndroidX、配置简单,导入方便
  • 支持debug模式

Installation:

image

最新版本(Based on Android X):

最新Release 改动:
  • 修复了部分资源泄露的问题
  • 优化了内存消耗、提高了源码可读性
dependencies {
    implementation 'com.qw:coco:0.3.1'
}

此后迭代的版本全部基于AndroidX,如果你的应用还没适配,未适配Android X的版本最多支持到 0.2.0,源码在 master_old 分支,且不再维护.

未适配AndroidX的最后支持版本(分支 master_old):

dependencies {
    implementation 'com.qw:coco:0.2.0'
}

Usage:

调用系统相机拍照

  • 默认原图的情形
  CoCo.with(this)
        .take(createSDCardFile())
        .apply()
        .start(object : GetImageCallBack<CaptureResult> {
        
                override fun onSuccess(data: CaptureResult) {
                    val bitmap: Bitmap = BitmapFactory.decodeFile(data.targetFile!!.path)
                    getImageView().setImageBitmap(bitmap)
                }

                override fun onCancel() {
                    Toast.makeText(this@TakePictureActivity, "拍照取消", Toast.LENGTH_SHORT).show()
                }

                override fun onFailed(exception: Exception) {
                    Toast.makeText(this@TakePictureActivity, "拍照异常: $exception", Toast.LENGTH_SHORT).show()
                }
            })
  • 如果你需要拍完照对图片做一些处理(applyWithDispose 目前自带的处理器可支持两种压缩策略):
                CoCo.with(this)
                    .take(createSDCardFile())
                    //默认处理器使用缩放压缩法,压缩50%
//                    .applyWithDispose()
                    //也可传入参数自己控制程度和策略
                    .applyWithDispose(DefaultImageDisposer().degree(10).strategy(CompressStrategy.MATRIX))
                    .start(object : GetImageCallBack<TakeResult> {

                        override fun onSuccess(data: TakeResult) {
                            Toast.makeText(this@MainActivity, "拍照操作最终成功", Toast.LENGTH_SHORT).show()
                            iv_image.setImageBitmap(data.compressBitmap)
                        }

                        override fun onFailed(exception: Exception) {
                            Toast.makeText(
                                this@MainActivity,
                                "拍照异常: $exception",
                                Toast.LENGTH_SHORT
                            ).show()
                        }

                    })
效果图:

image

系统相册选择

APi与拍照相似,同样支持压缩,选择照片结果中提供原始Uri

  • 仅仅是从系统相册选一张图片:
 CoCo.with(this)
    .pick()
    .apply()
    .start(object :GetImageCallBack<PickResult>{

          override fun onSuccess(data: PickResult) {
              Toast.makeText(
                  this@MainActivity,
                    "选择操作最终成功 path: ${data.originUri.path}",
                          Toast.LENGTH_SHORT
                            ).show()
           }

         override fun onFailed(exception: Exception) {
                Toast.makeText(this@MainActivity, "选择异常: $exception", Toast.LENGTH_SHORT).show()
           }
     })
  • 如果需要跟拍照一样,选完后对图片做处理:
 CoCo.with(this)
    .pick(createSDCardFile())
    .applyWithDispose()
    .start(object : GetImageCallBack<PickResult> {

        override fun onDisposeStart() {
                        Toast.makeText(this@PickPictureActivity, "选择成功,开始处理", Toast.LENGTH_SHORT)
                            .show()
                    }

        override fun onSuccess(data: PickResult) {
                        Toast.makeText(
                            this@PickPictureActivity,
                            "选择操作最终成功 path: ${data.originUri.path}",
                            Toast.LENGTH_SHORT
                        ).show()
                        getImageView().setImageBitmap(data.compressBitmap)
                        tv_result.text = getImageSizeDesc(data.compressBitmap!!)
                    }


        override fun onFailed(exception: Exception) {
                    }
                })

自定义压缩策略:

    /**
     * 自定义图片处理器
     * 自定义想要处理的任意结果
     */
    class CustomDisposer : ImageDisposer {

        override fun disposeImage(originPath: String, targetSaveFile: File?): BaseResult {
            return BaseResult().also {
                val bitmap = QualityCompressor()
                    .compress(originPath, 5)
                it.targetFile = targetSaveFile
                it.compressBitmap = bitmap
            }
        }

    }
    
    fun custom() {
        CoCo.with(this)
            .take(createSDCardFile())
            .applyWithDispose(CustomDisposer())
            .start(object : GetImageCallBack<TakeResult> {

                override fun onSuccess(data: TakeResult) {
                    Toast.makeText(this@MainActivity, "自定义Disposer拍照操作最终成功", Toast.LENGTH_SHORT)
                        .show()
                    iv_image.setImageBitmap(data.compressBitmap)
                }

                override fun onFailed(exception: Exception) {
                    Toast.makeText(
                        this@MainActivity,
                        "拍照异常: $exception",
                        Toast.LENGTH_SHORT
                    ).show()
                }

            })
    }

截图:

image

image

GitHub地址

coco's People

Contributors

longshihan1 avatar rocketzly avatar soulqw avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.