Android实现拍照截图功能
作者:Kriss ❀ 时间:2023-06-21 04:43:23
本文将向大家展示如何拍照截图。
先看看效果图:
拍照截图有点儿特殊,要知道,现在的Android智能手机的摄像头都是几百万的像素,拍出来的图片都是非常大的。因此,我们不能像对待相册截图一样使用Bitmap小图,无论大图小图都统一使用Uri进行操作。
一、首先准备好需要使用到的Uri:
private
static
final
String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp
file
Uri
imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The
Uri to store the big bitmap
二、使用MediaStore.ACTION_IMAGE_CAPTURE可以轻松调用Camera程序进行拍照:
Intent
intent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action
is capture
intent.putExtra(MediaStore.EXTRA_OUTPUT,
imageUri);
startActivityForResult(intent,
TAKE_BIG_PICTURE);//or
TAKE_SMALL_PICTURE
三、接下来就可以在 onActivityResult中拿到返回的数据(Uri),并将Uri传递给截图的程序。
switch
(requestCode) {
case
TAKE_BIG_PICTURE:
Log.d(TAG,
"TAKE_BIG_PICTURE:
data = "
+ data);//it
seems to be null
//TODO
sent to crop
cropImageUri(imageUri,
800,
400,
CROP_BIG_PICTURE);
break;
case
TAKE_SMALL_PICTURE:
Log.i(TAG,
"TAKE_SMALL_PICTURE:
data = "
+ data);
//TODO
sent to crop
cropImageUri(imageUri,
300,
150,
CROP_SMALL_PICTURE);
break;
default:
break;
}
可以看到,无论是拍大图片还是小图片,都是使用的Uri,只是尺寸不同而已。我们将这个操作封装在一个方法里面
private
void
cropImageUri(Uri uri, int
outputX, int
outputY, int
requestCode){
Intent
intent = new
Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri,
"image/*");
intent.putExtra("crop",
"true");
intent.putExtra("aspectX",
2);
intent.putExtra("aspectY",
1);
intent.putExtra("outputX",
outputX);
intent.putExtra("outputY",
outputY);
intent.putExtra("scale",
true);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
uri);
intent.putExtra("return-data",
false);
intent.putExtra("outputFormat",
Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection",
true);
//
no face detection
startActivityForResult(intent,
requestCode);
}
四、最后一步,我们已经将数据传入裁剪图片程序,接下来要做的就是处理返回的数据了:
switch
(requestCode) {
case
CROP_BIG_PICTURE://from
crop_big_picture
Log.d(TAG,
"CROP_BIG_PICTURE:
data = "
+ data);//it
seems to be null
if(imageUri
!= null){
Bitmap
bitmap = decodeUriAsBitmap(imageUri);
imageView.setImageBitmap(bitmap);
}
break;
case
CROP_SMALL_PICTURE:
if(imageUri
!= null){
Bitmap
bitmap = decodeUriAsBitmap(imageUri);
imageView.setImageBitmap(bitmap);
}else{
Log.e(TAG,
"CROP_SMALL_PICTURE:
data = "
+ data);
}
break;
default:
break;
}
标签:Android,拍照截图
0
投稿
猜你喜欢
C#使用windows服务发送邮件
2022-05-31 11:54:55
WheelPicker自定义时间选择器控件
2023-05-16 18:30:05
解决Tomcat启动报异常java.lang.ClassNotFoundException问题
2023-07-27 03:33:01
使用IDEA开发配置Java Web的初始化过程
2022-09-25 16:33:38
springboot+swagger2.10.5+mybatis-plus 入门详解
2023-03-15 01:58:38
Java实现经典游戏飞机大战-I的示例代码
2023-07-30 15:45:22
java实现单链表之逆序
2023-05-03 02:01:54
flutter BottomAppBar实现不规则底部导航栏
2023-06-19 23:40:19
Android PullToRefreshLayout下拉刷新控件的终结者
2021-06-22 02:15:00
JavaWeb Spring依赖注入深入学习
2022-04-02 08:20:05
mybatis createcriteria和or的区别说明
2021-09-27 09:20:40
Java中super和this关键字详解
2023-03-18 15:49:08
详解Mybatis的二级缓存配置
2023-03-20 10:48:37
Java二分查找算法与数组处理的应用实例
2022-07-11 01:26:57
java LeetCode刷题稍有难度的贪心构造算法
2022-01-06 20:56:21
c#使用正则表达式匹配字符串验证URL示例
2023-01-01 10:40:10
深入android Unable to resolve target 'android-XX'详解
2023-05-29 13:21:33
unity实现摄像头跟随
2021-07-26 12:50:10
解决bufferedReader.readLine()读到最后发生阻塞的问题
2022-07-15 06:05:38
Android控件View的文字周围添加图标
2023-02-20 04:04:44