最近利用一周左右的业余时间,终于完成了一个Android图片剪裁库,核心功能是根据自己的理解实现的,部分代码参考了Android源码的图片剪裁应用。现在将该代码开源在Github上以供大家学习和使用,地址:https://github.com/Jhuster/ImageCropper,效果如下所示:

我的大致计划是首先介绍一下这个库的用法,然后再写几篇文章介绍一下其中的一些原理和关键技术,希望对Android开发新手有所帮助。

【特性】

  1. 支持通过手势移动和缩放剪裁窗口

  2. 支持固定剪裁窗口大小、固定窗口的长宽比率

  3. 支持设置最大的窗口长和宽

  4. 支持剪裁图片的旋转

  5. 易于集成和使用

【使用方法】

  1. 修改AndroidManifest.xml文件

<activity android:name="com.ticktick.imagecropper.CropImageActivity"/>

需要添加写SDcard的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2. 启动图片剪裁界面的方法

第一种方法,使用库中封装的CropIntent来构建Intent对象:

private void startCropImage() {

    // Create a CropIntent
CropIntent intent = new CropIntent(); // Set the source image filepath/URL and output filepath/URL (Required)
intent.setImagePath("/sdcard/source.jpg");
intent.setOutputPath("/sdcard/cropped.jpg"); // Set a fixed crop window size (Optional)
intent.setOutputSize(,); // Set the max crop window size (Optional)
intent.setMaxOutputSize(,); // Set a fixed crop window's width/height aspect (Optional)
intent.setAspect(,); // Start ImageCropper activity with certain request code and listen for result
startActivityForResult(intent.getIntent(this), REQUEST_CODE_CROP_PICTURE);
}

第二种方法,自定义Intent对象:

private void startCropImage() {

    // Create explicit intent
Intent intent = new Intent(this, CropImageActivity.class); // Set the source image filepath/URL and output filepath/URL (Required)
intent.setData(Uri.fromFile(new File("/sdcard/source.jpg")));
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File("/sdcard/cropped.jpg"))); // Set a fixed crop window size (Optional)
intent.putExtra("outputX",);
intent.putExtra("outputY",); // Set the max crop window size (Optional)
intent.putExtra("maxOutputX",);
intent.putExtra("maxOutputY",); // Set a fixed crop window's width/height aspect (Optional)
intent.putExtra("aspectX",);
intent.putExtra("aspectY",); // Start ImageCropper activity with certain request code and listen for result
startActivityForResult(intent, REQUEST_CODE_CROP_PICTURE);
}

3. 获取剪裁结果

剪裁结束后,如果用户点击了“Save”按钮,则可以通过MediaStore.EXTRA_OUTPUT得到保存的图片的URL地址;如果用户点击了“Cancel”,则Activity的返回值会被设置为 RESULT_CANCEL

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != RESULT_OK) {
return;
} if (requestCode == REQUEST_CODE_CROP_PICTURE ) {
Uri croppedUri = data.getExtras().getParcelable(MediaStore.EXTRA_OUTPUT);
InputStream in = null;
try {
in = getContentResolver().openInputStream(croppedUri);
Bitmap b = BitmapFactory.decodeStream(in);
mImageView.setImageBitmap(b);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}

Android图片剪裁库的更多相关文章

  1. Android 图片裁剪库 uCrop

    引语 晚上好,我是猫咪,我的公众号「程序媛猫咪」会推荐 GitHub 上好玩的项目,挖掘开源的价值,欢迎关注我. 现在 Android 开发,离不开图片,必然也需要图片裁剪功能,这个实现可以调用系统的 ...

  2. [RN] React Native 使用开源库 react-native-image-crop-picker 实现图片选择、图片剪裁

    React Native 使用开源库 react-native-image-crop-picker 实现图片选择.图片剪裁 该库可以实现启动本地相册和照相机来采集图片,并且提供多选.图片裁剪等功能,支 ...

  3. Android图片加载库的理解

    前言     这是“基础自测”系列的第三篇文章,以Android开发需要熟悉的20个技术点为切入点,本篇重点讲讲Android中的ImageLoader这个库的一些理解,在Android上最让人头疼是 ...

  4. picasso-强大的Android图片下载缓存库

    编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! pica ...

  5. fackbook的Fresco (FaceBook推出的Android图片加载库-Fresco)

    [Android开发经验]FaceBook推出的Android图片加载库-Fresco   欢迎关注ndroid-tech-frontier开源项目,定期翻译国外Android优质的技术.开源库.软件 ...

  6. picasso_强大的Android图片下载缓存库

    tag: android pic skill date: 2016/07/09 title: picasso-强大的Android图片下载缓存库 [本文转载自:泡在网上的日子 参考:http://bl ...

  7. 毕加索的艺术——Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选

    毕加索的艺术--Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选 官网: http://square.github.i ...

  8. android图片加载库Glide

    什么是Glide? Glide是一个加载图片的库,作者是bumptech,它是在泰国举行的google 开发者论坛上google为我们介绍的,这个库被广泛的运用在google的开源项目中. Glide ...

  9. Android 图片加载库Glide 实战(二),占位符,缓存,转换自签名高级实战

    http://blog.csdn.net/sk719887916/article/details/40073747 请尊重原创 : skay <Android 图片加载库Glide 实战(一), ...

随机推荐

  1. 小程序text组件内部上边距的问题

    index.wxml: <view class="slogan"> <text> 建立跨文化的全球视野,做世界公民 </text> </v ...

  2. anaconda安装的TensorFlow版本没有model这个模块

    一.采用git bash来安装,确认已经安装了git 二.手动找到TensorFlow的模块文件夹地址,若不知道,输入以下两行代码: import tensorflow as tf tf.__path ...

  3. XX-Net的完整教程

    1.下载谷歌浏览器,安装. 2.百度搜索github,github中搜索XX-Net,下载稳定版 3.解压缩下载的文件夹,运行start.vbs文件.如果弹出管理员请求权限请允许,弹出防火墙警告,请允 ...

  4. 编写输出Hellow word!

    打开java运行环境(eclipse),然后输入以下语句,点击编译运行后即可输出“Hello World,”. public class HelloWorld { public static void ...

  5. jQuery cxSelect 联动下拉菜单

    插件简介 cxSelect 是基于 jQuery 的多级联动菜单插件,适用于省市.商品分类等联动菜单. 列表数据通过 AJAX 获取,也可以自定义,数据内容使用 JSON 格式. 同时兼容 Zepto ...

  6. vue 对图片进行拖拽到另一个位置

    1.拖动元素代码: 使用html5原生拖拽属性,在需要拖拽的图片中添加draggable="true"属性,并使用v-on添加拖动事件 2.被放置的区域事件代码: 使用html5原 ...

  7. 游标后面select 带有in时

    今天遇到一个问题,使用游标时,在给游标填充值的时候,select  语句中带有 where查询条件,并且还有 in子句. 本来我是这样写的,试了很多次都不出结果,当然number in (304010 ...

  8. luogu P4238 多项式求逆 (模板题、FFT)

    手动博客搬家: 本文发表于20181125 13:21:46, 原地址https://blog.csdn.net/suncongbo/article/details/84485718 题目链接: ht ...

  9. python操作JIRA的库简单操作

    因公司需要,我们开发的PRISM又需要和JIRA对接啦, 今天找了一个JIRA库撸了一发~~~ jira库地址: https://pypi.python.org/pypi/jira/1.0.3 简单操 ...

  10. J - Assign the task

    J - Assign the task HDU - 3974 思路:一眼秒思路<(* ̄▽ ̄*)/ dfs序+线段树. 通过dfs序把树上问题转化成线段上的问题.然后用线段树解决.    错因:都 ...