Android实现拍照、选择相册图片并裁剪功能
作者:迟做总比不做强 发布时间:2022-09-22 12:25:44
标签:Android,拍照,选择图片,裁剪图片
通过拍照或相册中获取图片,并进行裁剪操作,然后把图片显示到ImageView上。
当然也可以上传到服务器(项目中绝大部分情况是上传到服务器),参考网上资料及结合项目实际情况,
测试了多款手机暂时没有发现严重问题。代码有注释,直接贴代码:
public class UploadPicActivity extends Activity implements View.OnClickListener {
private Button take_photo_btn;
private Button select_photo_btn;
private ImageView photo_iv;
//使用照相机拍照获取图片
public static final int TAKE_PHOTO_CODE = 1;
//使用相册中的图片
public static final int SELECT_PIC_CODE = 2;
//图片裁剪
private static final int PHOTO_CROP_CODE = 3;
//定义图片的Uri
private Uri photoUri;
//图片文件路径
private String picPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_pic);
initViews();
}
private void initViews() {
this.take_photo_btn = (Button) findViewById(R.id.take_photo_btn);
this.take_photo_btn.setOnClickListener(this);
this.select_photo_btn = (Button) findViewById(R.id.select_photo_btn);
this.select_photo_btn.setOnClickListener(this);
this.photo_iv = (ImageView) findViewById(R.id.photo_iv);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
//拍照
case R.id.take_photo_btn:
picTyTakePhoto();
break;
//选择图库
case R.id.select_photo_btn:
pickPhoto();
break;
}
}
/**
* 拍照获取图片
*/
private void picTyTakePhoto() {
//判断SD卡是否存在
String SDState = Environment.getExternalStorageState();
if (SDState.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//"android.media.action.IMAGE_CAPTURE"
/***
* 使用照相机拍照,拍照后的图片会存放在相册中。使用这种方式好处就是:获取的图片是拍照后的原图,
* 如果不实用ContentValues存放照片路径的话,拍照后获取的图片为缩略图有可能不清晰
*/
ContentValues values = new ContentValues();
photoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, TAKE_PHOTO_CODE);
} else {
Toast.makeText(this, "内存卡不存在", Toast.LENGTH_LONG).show();
}
}
/***
* 从相册中取图片
*/
private void pickPhoto() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, SELECT_PIC_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
//从相册取图片,有些手机有异常情况,请注意
if (requestCode == SELECT_PIC_CODE) {
if (null != data && null != data.getData()) {
photoUri = data.getData();
picPath = uriToFilePath(photoUri);
startPhotoZoom(photoUri, PHOTO_CROP_CODE);
} else {
Toast.makeText(this, "图片选择失败", Toast.LENGTH_LONG).show();
}
} else if (requestCode == TAKE_PHOTO_CODE) {
String[] pojo = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(photoUri, pojo, null, null, null);
if (cursor != null) {
int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
cursor.moveToFirst();
picPath = cursor.getString(columnIndex);
if (Build.VERSION.SDK_INT < 14) {
cursor.close();
}
}
if (picPath != null) {
photoUri = Uri.fromFile(new File(picPath));
startPhotoZoom(photoUri, PHOTO_CROP_CODE);
} else {
Toast.makeText(this, "图片选择失败", Toast.LENGTH_LONG).show();
}
} else if (requestCode == PHOTO_CROP_CODE) {
if (photoUri != null) {
Bitmap bitmap = BitmapFactory.decodeFile(picPath);
if (bitmap != null) {
//这里可以把图片进行上传到服务器操作
photo_iv.setImageBitmap(bitmap);
}
}
}
}
}
/**
* @param
* @description 裁剪图片
* @author ldm
* @time 2016/11/30 15:19
*/
private void startPhotoZoom(Uri uri, int REQUE_CODE_CROP) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra("crop", "true");
// 去黑边
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
// aspectX aspectY 是宽高的比例,根据自己情况修改
intent.putExtra("aspectX", 3);
intent.putExtra("aspectY", 2);
// outputX outputY 是裁剪图片宽高像素
intent.putExtra("outputX", 600);
intent.putExtra("outputY", 400);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
//取消人脸识别功能
intent.putExtra("noFaceDetection", true);
//设置返回的uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//设置为不返回数据
intent.putExtra("return-data", false);
startActivityForResult(intent, REQUE_CODE_CROP);
}
/**
* @param
* @description 把Uri转换为文件路径
* @author ldm
* @time 2016/11/30 15:22
*/
private String uriToFilePath(Uri uri) {
//获取图片数据
String[] proj = {MediaStore.Images.Media.DATA};
//查询
Cursor cursor = managedQuery(uri, proj, null, null, null);
//获得用户选择的图片的索引值
int image_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//返回图片路径
return cursor.getString(image_index);
}
}
布局文件长这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/take_photo_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="拍照"
android:textSize="16sp"/>
<Button
android:id="@+id/select_photo_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="选择图片"
android:textSize="16sp"/>
<ImageView
android:id="@+id/photo_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"/>
</LinearLayout>
最后不要忘记在AndroidManifest.xml中添加UploadPicActivity及权限:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>


猜你喜欢
- 一、Jsoup的主要功能Jsoup是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API
- 最近为公司做的一个Demo里面用到了ScrollView嵌套了GridView和ListView,然而在嵌套的时候我发现GridView和L
- synchronized锁的升级(偏向锁、轻量级锁及重量级锁)java同步锁前置知识点1.编码中如果使用锁可以使用synchronized关
- 是否还记得在博文「IntelliJ IDEA 安装目录的核心文件讲解」中,这张充满神秘色彩的图片呢?进入她,让我们一起感受她的魅力吧!如上图
- 本文实例讲述了Android非XML形式动态生成、调用页面的方法。分享给大家供大家参考。具体分析如下:这个问题是这样的:我们不使用XML构建
- 上一篇我们说了Android中的tween动画,这一篇我们说说frame动画,frame动画主要是实现了一种类似于gif动画的效果,就是多张
- 在我们实际业务中,可能存在多个类之间相互调用,形成了一个复杂的网状结构。这时候就需要有一种模式去“捋顺&rdqu
- 一、回传协议接口和TCP方式实现:1.接口:import java.nio.channels.SelectionKey; import ja
- 前言似乎委托对于C#而言是一种高级属性,但是我依旧希望你就算第一次看我的文章,也能有很大的收获。所以本博客的语言描述尽量简单易懂,知识点也是
- 前言前一段时间做了一个项目,需要解决中文、繁体、英文的国际化问题,所以本文将详细介绍springboot页面国际化配置的过程方法如下1.引入
- Android 完全退出的实例详解首先,在基类BaseActivity里,注册RxBus监听:public class BaseActivi
- 本文详细讲述了JAR命令的用法,对于大家学习和总结jar命令的使用有一定的帮助作用。具体如下:JAR包是Java中所特有一种压缩文档,其实大
- SpringBoot底层的一个功能 : @ConfigurationProperties@ConfigurationProperties 配
- Android 图形特效 &nbs
- 背景1> 大家都知道SpringBoot是通过main函数启动的,这里面跟踪代码到处都没有找到while(true),为什么启动后可以
- 本文实例讲述了Android实现便于批量操作可多选的图片ListView。分享给大家供大家参考,具体如下:之前项目需要实现一个可多选的图片列
- 本文实例为大家分享了六种Android常见控件的使用方法,供大家参考,具体内容如下1、TextView 主要用于界面上显示一段文本
- 引言对使用 lombok 还是有很多争议的,有些公司不建议使用,有些公司又大量使用。我们的想法是:可以使用,但是不要滥用。什么是 lombo
- 多数据源配置首先是配置文件这里采用yml配置文件,其他类型配置文件同理我配置了两个数据源,一个名字叫ds1数据源,一个名字叫ds2数据源,如
- 前言前面小空带同学们学了EditText控件,又用其实践做了个验证码功能,以为这就完了吗?然而并没有。Android在5.0以后引入了Mat