生活随笔
收集整理的這篇文章主要介紹了
android调用照相机拍照获取照片并做简单剪裁
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.調用系統的照相機程序
Intent?intent?=?new?Intent(MediaStore.ACTION_IMAGE_CAPTURE); ?startActivityForResult(intent,?TAKE_PHOTO_WITH_DATA);? 2.在onActivityResult中處理返回的data
final Bitmap photo = data.getParcelableExtra("data");
如果不需要做剪切處理就可以直接使用圖片了,比如輸出到ImageView上
p_w_picpathView.setImageBitmap(photo);
如果需要做剪切處理,就繼續向下執行
3.做剪切處理
Intent?intent?=?new?Intent("com.android.camera.action.CROP"); ???????intent.setType("p_w_picpath/*"); ???????intent.putExtra("data",?data); ???????intent.putExtra("crop",?"true"); ???????intent.putExtra("aspectX",?1); ???????intent.putExtra("aspectY",?1); ???????intent.putExtra("outputX",?128); ???????intent.putExtra("outputY",?128); ???????intent.putExtra("return-data",?true); ?artActivityForResult(intent,?PHOTO_PICKED_WITH_DATA);? 4.在onActivityResult中處理
與第二步相同。
完整代碼:
public?class?SdCardUriTestActivity?extends?Activity?{ ??????????private?Button?btnTake?=?null; ?????private?ImageView?lblImage?=?null; ???? ?????private?static?final?int?PHOTO_PICKED_WITH_DATA?=?3021; ?????private?static?final?int?CAMERA_WITH_DATA?=?3023; ???? ?????@Override?????public?void?onCreate(Bundle?savedInstanceState)?{ ?????????super.onCreate(savedInstanceState); ?????????setContentView(R.layout.main); ???????? ?????????btnTake?=?(Button)findViewById(R.id.btnTake); ?????????lblImage?=?(ImageView)findViewById(R.id.lblImage); ?????????btnTake.setOnClickListener(new?View.OnClickListener()?{ ???????????? ?????????????@Override?????????????public?void?onClick(View?v)?{ ??????????????????????????????????Intent?intent?=?new?Intent(MediaStore.ACTION_IMAGE_CAPTURE); ?????????????????startActivityForResult(intent,?CAMERA_WITH_DATA); ?????????????} ?????????}); ???????? ?????} ???? ?????@Override?????protected?void?onActivityResult(int?requestCode,?int?resultCode,?Intent?data)?{ ??????????????????if(resultCode!=RESULT_OK) ?????????????return; ?????????switch(requestCode){ ?????????case?CAMERA_WITH_DATA: ?????????????final?Bitmap?photo?=?data.getParcelableExtra("data"); ?????????????if(photo!=null){ ?????????????????doCropPhoto(photo); ?????????????} ?????????case?PHOTO_PICKED_WITH_DATA: ?????????????Bitmap?photo1?=?data.getParcelableExtra("data"); ?????????????if(photo1!=null){ ?????????????????lblImage.setImageBitmap(photo1); ?????????????} ???????????? ?????????} ?????} ???? ?????protected?void?doCropPhoto(Bitmap?data){ ?????????Intent?intent?=?getCropImageIntent(data); ?????????startActivityForResult(intent,?PHOTO_PICKED_WITH_DATA); ?????} ???? ?????public?static?Intent?getCropImageIntent(Bitmap?data)?{ ?????????Intent?intent?=?new?Intent("com.android.camera.action.CROP"); ?????????intent.setType("p_w_picpath/*"); ?????????intent.putExtra("data",?data); ?????????intent.putExtra("crop",?"true"); ?????????intent.putExtra("aspectX",?1); ?????????intent.putExtra("aspectY",?1); ?????????intent.putExtra("outputX",?128); ?????????intent.putExtra("outputY",?128); ?????????intent.putExtra("return-data",?true); ?????????return?intent; ?????} ?}? ?
轉載于:https://blog.51cto.com/whatandroid/1136712
總結
以上是生活随笔為你收集整理的android调用照相机拍照获取照片并做简单剪裁的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。