Android实现拍照、选择相册图片并裁剪功能

Android实现拍照、选择相册图片并裁剪功能,第1张

概述通过拍照或相册中获取图片,并进行裁剪 *** 作,然后把图片显示到ImageView上。

通过拍照或相册中获取图片,并进行裁剪 *** 作,然后把图片显示到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);    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,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);  //获得用户选择的图片的索引值  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"/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android实现拍照、选择相册图片并裁剪功能全部内容,希望文章能够帮你解决Android实现拍照、选择相册图片并裁剪功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1147549.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存