转载声明:Ryan的博客文章欢迎您的转载,但在转载的同时,请注明文章的来源出处,不胜感激! :-)
http://blog.csdn.net/floodingfire/article/details/8144617
上一篇博客中,我们学习到了如何使用Android相册截图。在这篇博客中,我将向大家展示如何拍照截图。
拍照截图有点儿特殊,要知道,现在的Android智能手机的摄像头都是几百万的像素,拍出来的图片都是非常大的。因此,我们不能像对待相册截图一样使用Bitmap小图,无论大图小图都统一使用Uri进行操作。
一、首先准备好需要使用到的Uri:
1 | private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg" ;//temp file |
2 | Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION); |
二、使用MediaStore.ACTION_IMAGE_CAPTURE可以轻松调用Camera程序进行拍照:
1 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
2 | intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); |
3 | startActivityForResult(intent, TAKE_BIG_PICTURE); |
三、接下来就可以在 onActivityResult中拿到返回的数据(Uri),并将Uri传递给截图的程序。 03 |
Log.d(TAG, "TAKE_BIG_PICTURE: data = " + data); |
05 |
cropImageUri(imageUri, 800 , 400 , CROP_BIG_PICTURE); |
08 | case TAKE_SMALL_PICTURE: |
09 |
Log.i(TAG, "TAKE_SMALL_PICTURE: data = " + data); |
11 |
cropImageUri(imageUri, 300 , 150 , CROP_SMALL_PICTURE); |
可以看到,无论是拍大图片还是小图片,都是使用的Uri,只是尺寸不同而已。我们将这个操作封装在一个方法里面。 01 | private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode){ |
02 |
Intent intent = new Intent( "com.android.camera.action.CROP" ); |
03 |
intent.setDataAndType(uri, "image/*" ); |
04 |
intent.putExtra( "crop" , "true" ); |
05 |
intent.putExtra( "aspectX" , 2 ); |
06 |
intent.putExtra( "aspectY" , 1 ); |
07 |
intent.putExtra( "outputX" , outputX); |
08 |
intent.putExtra( "outputY" , outputY); |
09 |
intent.putExtra( "scale" , true ); |
10 |
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); |
11 |
intent.putExtra( "return-data" , false ); |
12 |
intent.putExtra( "outputFormat" , Bitmap.CompressFormat.JPEG.toString()); |
13 |
intent.putExtra( "noFaceDetection" , true ); |
14 |
startActivityForResult(intent, requestCode); |
四、最后一步,我们已经将数据传入裁剪图片程序,接下来要做的就是处理返回的数据了: 03 |
Log.d(TAG, "CROP_BIG_PICTURE: data = " + data); |
05 |
Bitmap bitmap = decodeUriAsBitmap(imageUri); |
06 |
imageView.setImageBitmap(bitmap); |
09 | case CROP_SMALL_PICTURE: |
11 |
Bitmap bitmap = decodeUriAsBitmap(imageUri); |
12 |
imageView.setImageBitmap(bitmap); |
14 |
Log.e(TAG, "CROP_SMALL_PICTURE: data = " + data); |
效果图:
代码托管于GitHub,会不定期更新:https://github.com/ryanhoo/PhotoCropper
基础篇:
【译】如何使用Android MediaStore裁剪大图片
上篇:
Android大图片裁剪终极解决方案(上:原理分析)
中篇:
Android大图片裁剪终极解决方案(中:从相册截图)
下篇:
Android大图片裁剪终极解决方案(下:拍照截图)
- android 高性能编程
- Android(安卓)TTS 实战一:认识 TTS
- Android将胜过Windows Mobile五大原因
- Android(安卓)Web Server
- Android尺寸标注设计大全和Android切图规范
- Android(安卓)Web Server
- Android(安卓)设定横屏,禁止屏幕旋转,Activity重置 [更新视频播放
- 关于ImageView的一些图片属性
- ADT在线安装(http://dl-ssl.google.com/android/eclipse 打不开)
随机推荐
-
android的数据库API操作
-
WebView.onDraw (Android 4.1)
-
Android 初学入门代码注释 学习笔记001 1
-
android studio adb连接不上手机 ADB ser
-
android代码实现自动关机
-
java android MD5加密
-
Android listview怎么实现滚动分页
-
Android Development Notes-2
-
自定义垂直文字view
-
Android中GPS/Map的运用