调用相机拍照后截取指定尺寸大小

2016-01-14 14:53 阅读 8,582 次 评论 0 条

概要

启动Android手机相机功能拍照,拍照后将按照指定尺寸对图片进行裁剪,在当前界面显示裁剪后的效果,这个小功能经常使用在修改头像的应用中。

》修改头像应用案例
001-demo

系统相机

调用系统相机功能,拍摄照片,并将拍摄的照片保存在指定路径,完成后在当前界面显示,执行回调方法onActivityResult(int arg0,int arg1,Intent arg2),通过arg0参数判断是否拍照返回,arg2是返回的数据

  1. /** 
  2.  * 调用系统相机 
  3.  */  
  4. private static File photoFile;  
  5. private void goToTakePhoto() {  
  6.     String tempFileName = System.currentTimeMillis() + ".jpg";  
  7.     // 指定调用相机拍照后照片的储存路径  
  8.     photoFile = new File(Environment.getExternalStorageDirectory(),  
  9.             "temp_capture" + tempFileName);  
  10.     Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  11.     cameraintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));  
  12.     startActivityForResult(cameraintent, RESULT_CAPTURE_IMAGE);  
  13.   
  14. }  

系统截图

系统截图,需要设置截图的图片尺寸,图片保存的路径、名称,同样完成截图后,执行回调方法onActivityResult(int arg0,int arg1,Intent arg2),通过arg0参数判断是否截图返回,arg2是返回的数据

  1. /** 
  2.      * 系统截图 
  3.      *  
  4.      * @param uri 
  5.      */  
  6.     private static File photoFinal;  
  7.     public static void startPhotoZoom(Context context, Uri uri) {  
  8.         Log.e("TeachCourse""------------裁剪");  
  9.         photoUploadTempName = System.currentTimeMillis() + ".jpg";  
  10.   
  11.         File ZoomFile = new File(Environment.getExternalStorageDirectory()  
  12.                 + "/temp");  
  13.         if (!ZoomFile.exists()) {  
  14.             ZoomFile.mkdirs();  
  15.         }  
  16.         photoFinal = new File(ZoomFile, photoUploadTempName);  
  17.   
  18.         Intent intent = new Intent("com.android.camera.action.CROP");  
  19.         intent.setDataAndType(uri, "image/*");  
  20.         intent.putExtra("crop""true");  
  21.         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFinal)); // 裁剪后临时储存的jpg,return为true,直接加入bundle,并finish  
  22.         intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());  
  23.         // aspectX,aspectY 是宽高的比例  
  24.         intent.putExtra("aspectX"1);  
  25.         intent.putExtra("aspectY"1);  
  26.         // outputX,outputY 是裁剪图片宽高  
  27.         intent.putExtra("outputX"700);  
  28.         intent.putExtra("outputY"700);  
  29.         intent.putExtra("scale"true);// 黑边  
  30.         intent.putExtra("scaleUpIfNeeded"true);// 黑边  
  31.   
  32.         intent.putExtra("return-data"false);  
  33.   
  34.         intent.addCategory(Intent.CATEGORY_DEFAULT);  
  35.   
  36.         ((FragmentActivity) context).startActivityForResult(intent, PHOTO_CROP);  
  37.   
  38.     }  

裁剪后效果

获取保存在手机SDCard中的图片,通过ImageView显示

  1. /** 
  2.      * 获取裁剪后照片 
  3.      *  
  4.      * @param data 
  5.      * @throws IOException 
  6.      * @throws FileNotFoundException 
  7.      */  
  8.     private ImageView mCapture_img;  
  9.     private void getImageFromSystem(Intent data) throws FileNotFoundException,  
  10.             IOException {  
  11.         if (data != null) {  
  12.             File file = photoFinal;  
  13.             if (file != null) {  
  14.   
  15.                 mCapture_img.setImageDrawable(Drawable.createFromPath(file  
  16.                         .getPath()));  
  17.                 Toast.makeText(MainActivity.this"裁剪成功", Toast.LENGTH_SHORT)  
  18.                         .show();  
  19.             }  
  20.         }  
  21.     }  

设置图像形状

自定义CircleImageView继承ImageView,重写onDraw(Canvas canvas)方法,绘制圆形头像,代码如下:

  1. @Override  
  2.     protected void onDraw(Canvas canvas) {  
  3.         if (getDrawable() == null) {  
  4.             return;  
  5.         }  
  6.   
  7.         canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius,  
  8.                 mBitmapPaint);  
  9.         if (mBorderWidth != 0) {  
  10.             canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius,  
  11.                     mBorderPaint);  
  12.         }  
  13.     }  

》圆形头像裁剪
002-circle-imageview

你可能感兴趣的文章

来源:TeachCourse每周一次,深入学习Android教程,关注(QQ158#9359$239或公众号TeachCourse)
转载请注明出处: https://www.teachcourse.cn/1233.html ,谢谢支持!

资源分享

解决Unable to load R3 module …VBoxDD.dll (VBoxDD):GetLastError=1790 解决Unable to load R3 module
Building and Running Overview Building and Running Overvi
关于Android app的launcher图标更换后,仍然显示默认的ic_launcher图标的解决方法 关于Android app的launcher图标
Conversion to Dalvik format failed Conversion to Dalvik format