閱讀371 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android調用係統拍照程序和從圖庫選取圖片,返回後調用係統裁剪工具

調用係統拍照

Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


File myImageDir = new File(TEMP_TAKE_PHOTO_FILE_PATH);

//創建圖片保存目錄
if (!myImageDir.exists()) 
{
Mylog.d(THIS, "Create the path:" + TEMP_TAKE_PHOTO_FILE_PATH);
myImageDir.mkdirs();
}

//根據時間來命名
imagFile = File.createTempFile(""+System.currentTimeMillis(), ".jpg",myImageDir);

tmpuri = Uri.fromFile(imagFile);

i.putExtra(MediaStore.EXTRA_OUTPUT, tmpuri); 


startActivityForResult(i, TAKE_PHOTO_REQUEST_CODE);


從圖庫選擇圖片

Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); // "android.intent.action.GET_CONTENT"


innerIntent.setType("image/*"); // 查看類型


// StringIMAGE_UNSPECIFIED="image/*";詳細的類型在com.google.android.mms.ContentType中


Intent wrapperIntent = Intent.createChooser(innerIntent, null);


act.startActivityForResult(wrapperIntent, SELECT_PHOTO_REQUEST_CODE);



返回後接收並調用係統裁剪工具

@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (requestCode == MediaHelper.TAKE_PHOTO_REQUEST_CODE || requestCode == MediaHelper.SELECT_PHOTO_REQUEST_CODE) {
   
if (resultCode == RESULT_OK ) {
Uri uri = null;
if(requestCode == MediaHelper.SELECT_PHOTO_REQUEST_CODE) {
uri = intent.getData();
}else if(requestCode == MediaHelper.TAKE_PHOTO_REQUEST_CODE) {
uri = MediaHelper.tmpuri;
}


if (uri != null) {

    final Intent intent1 = new Intent("com.android.camera.action.CROP"); 
      intent1.setDataAndType(uri, "image/*");
      intent1.putExtra("crop", "true");
      intent1.putExtra("aspectX", 1);
      intent1.putExtra("aspectY", 1);
      intent1.putExtra("outputX", 132);
      intent1.putExtra("outputY", 132);
      intent1.putExtra("return-data", true);
      startActivityForResult(intent1, MediaHelper.CUT_PHOTO_REQUEST_CODE);
    
}
}

else if(requestCode == MediaHelper.CUT_PHOTO_REQUEST_CODE) {
if (resultCode == RESULT_OK && intent != null) {
bm= intent.getParcelableExtra("data");

}

}



}



在裁剪圖片時,遇到有些圖片不能按照某一指定的比例進行裁剪,查看了源碼後才知道:係統的裁剪圖片默認對圖片進行人臉識別,當識別到有人臉時,會按aspectX和aspectY為1來處理,如果想設置成自定義的裁剪比例,需要設置noFaceDetection為true。

即iintent.putExtra("noFaceDetection", true);  取消人臉識別功能。

最後更新:2017-04-04 07:03:49

  上一篇:go java方法執行控製台命令工具類
  下一篇:go Hadoop裏的Partitioner和Combiner兩個階段