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

來源:互聯網
上載者:User

調用系統拍照

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");

}

}

}

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

即iintent.putExtra("noFaceDetection", true);  取消Face Service功能。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.