最近利用一周左右的业余时间,终于完成了一个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. node jsonwebtoken

     jsonwebtoken是node版本的JWT(JSON Web Tokens)的实现.1.什么是JWT?Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于J ...

  2. JQuery的click,trigger触发a标签的click事件无效的问题分析

    今天在做一个手机端webAPP链接下载的时候,给a标签一个下载链接,但是通过 <a id="downFile" download="" href=&quo ...

  3. python的自动化测试报告

    #coding=utf-8import HTMLTestRunnerimport BeautifulReportimport unittestclass MyTest(unittest.TestCas ...

  4. [bzoj1860 ZJOI2006] 超级麻将 (线性dp)

    传送门 Description Input 第一行一个整数N(N<=100),表示玩了N次超级麻将. 接下来N行,每行100个数a1..a100,描述每次玩牌手中各种牌的数量.ai表示数字为i的 ...

  5. Python-Pandas数据处理

  6. 2018 noip 备战日志

    我是写给自己看的…… Day1 10.8 今天开始停晚修课了,开始认真备战考试了. 今天晚上效率不错,竟然不会累,应该是平时一直这个时间写作业大脑高度集中, 现在换了编程也一样可以集中到这个状态 一些 ...

  7. Locally managed (LMT) vs. Dictionary managed (DMT) tablespace

    The LMT is implemented by adding the extent management local clause to the tablespace definition syn ...

  8. js休眠

    <!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/htm ...

  9. HDU 4528

    一直在纠结怎么样表示找到了人,,,开始时竟灰笨得设两个BOOL.后来参考别人的可以使用二进制位. 另外,此处有一个剪枝就是,就到达该点之后的状态的found(即找到人的状态)在之前已出现过,可以剪去. ...

  10. 高速学会Mac上托管代码到github(具体解释)

    之前最開始的时候就一直在github浏览下载各种代码,然后弄了一下代码上传不知道咋弄就不了了之了.刚好近期有空余时间就研究了下github托管代码,这里就具体说说怎样高速的学会github上传你的代码 ...