Skip to content
提供快速从系统相机、图库UI或文档UI选择图片并裁剪的功能。包含Android 6.0开始动态权限申请以及7.0文件协议(FileProvider)的处理。
Branch: master
Clone or download
Latest commit 946e815 Feb 26, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app 【需求】发布0.0.3版本 Feb 26, 2019
docs
gradle/wrapper
library
screenshot 【Feature】1.优化demo;2.添加说明文档和升级日志文件;3.添加核心API的Javadoc文档 Feb 18, 2019
.gitignore
CHANGELOG.md 【需求】添加0.0.3版本变更说明 Feb 26, 2019
LICENSE Create LICENSE Feb 21, 2019
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle 【Feature】初始化提交 Jan 31, 2019

README.md

ImagePicker

Download

背景

开发App过程中经常会有选择图片、裁剪图片的需求。实现该需求最快的方式就是通过隐式调用来调起系统提供的相机、图库UI、文档UI来进行选择,通过ACTIONcom.android.camera.action.CROP的隐式调用来调起裁剪。

简介

该库提供快速从系统相机、图库UI或文档UI选择图片并裁剪的功能,包含Android 6.0开始动态权限申请(文件存储权限)以及7.0文件协议(FileProvider)的处理。

核心功能拍照、选图、裁剪可自由搭配使用或单独使用,同时未内嵌UI,项目可高度定制自己的UI。

Demo介绍了常用的使用方式:

简介

主要功能

1.选图功能:从相机、图库UI、文档UI选择图片;

2.裁剪功能;

3.选图+裁剪组合使用;

使用方式

build.gradle添加依赖:

// 确保仓库已添加jcenter()
allprojects {
    repositories {
        ...
        jcenter()
    }
}

// 添加依赖
dependencies {
	compile 'com.lancewu:ImagePicker:<version>' 
}

<version>请替换为对应的版本号。

e.g.拍照+裁剪

需求:从相机拍照并裁剪得到一张200*400的图片;

实现代码如下:

public void clickCameraCrop(View view) {
    // 从相机拍照并裁剪
    File file = new File(getExternalCacheDir(), "camera_crop.jpg");
    // 创建裁剪参数
    ImagePicker.CropConfigBuilder cropConfigBuilder = new ImagePicker.CropConfigBuilder()
            .aspect(1, 2) // 比例1:2
            .outputSize(200, 400) // 输出大小200*400
            .outputFile(file);  // 最终文件保存路径
    // 创建选择器
    mPicker = new ImagePicker.Builder(this)
            .fromCamera(file) // 表示从相机选择,并设置拍照保存文件
            .withCrop(cropConfigBuilder) // 拍照完紧接裁剪
            .build();
    // 调用选图
    mPicker.pick(mCallback);
}

private OnImagePickerCallback mCallback = new OnImagePickerCallback() {
    @Override
    public void onPickError(@ErrorCode int errorCode) {
        // 发生错误,具体错误参考:@ErrorCode
        showToast("ImagePicker-发生错误:" + errorCode);
    }

    @Override
    public void onPickSuccess(@NonNull ImagePickerResult result) {
        // 选图/裁剪回调
        InputStream inputStream = null;
        try {
            // 从选择结果中取出文件Uri,进行想要的处理,这边直接显示
            inputStream = getContentResolver().openInputStream(result.getImageUri());
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            mImageView.setImageBitmap(bitmap);
            String text = "图片信息:宽*高=" + bitmap.getWidth() + "*" +bitmap.getHeight();
            mInfoTv.setText(text);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            StreamUtils.close(inputStream);
        }
    }

    @Override
    public void onPickCancel() {
        // 主动取消选择/裁剪时回调
        showToast("ImagePicker-取消选择");
    }

    void showToast(String msg) {
        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
    }
};

更详细的使用方式可参照Demo。

接口API

API概览

核心API-选择功能设置:ImagePicker.Builder

核心API-裁剪参数设置:ImagePicker.CropConfigBuilder

Change Log

Change Log

License

Copyright 2019 LanceWu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.