2016-09-24 27 views
0

我已經實現了具有特定輸出大小的圖像裁剪意圖。但是代碼大小所做的更改不會對圖像產生任何影響,每次更改的輸出都保持不變。Android圖像裁剪:輸出x和輸出y

private void performCrop(String picUri) { 
    try { 
     //Start Crop Activity 

     Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
     // indicate image type and Uri 
     File f = new File(picUri); 
     Uri contentUri = Uri.fromFile(f); 

     cropIntent.setDataAndType(contentUri, "image/*"); 
     // set crop properties 
     cropIntent.putExtra("crop", "true"); 
     // indicate aspect of desired crop 
     cropIntent.putExtra("aspectX", 1); 
     cropIntent.putExtra("aspectY", 1); 
     // indicate output X and Y 
     cropIntent.putExtra("outputX", 400); 
     cropIntent.putExtra("outputY", 400); 

     // retrieve data on return 
     cropIntent.putExtra("return-data", true); 
     // start the activity - we handle returning in onActivityResult 
     startActivityForResult(cropIntent, RESULT_CROP); 
    } 
    // respond to users whose devices do not support the crop action 
    catch (ActivityNotFoundException anfe) { 
     // display an error message 
     String errorMessage = "your device doesn't support the crop action!"; 
     Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 

輸出x和y值的變化不會影響正在裁剪的圖像大小。圖像大小始終保持爲160 * 160。但是當我改變寬高比時,圖像大小會發生一些變化。但我需要x和y

提供具體的大小,如果有是關於圖像裁剪任何庫,並支持所有設備上的話,那就真棒

+1

爲什麼不把你的imageview設置爲你想要的尺寸並設置scaletype = centercrop? –

回答

1

設置aspectX和aspectY在outputX和outputY的比。

假設您想要結果圖像爲200 * 300像素。然後,只需將aspectX設置爲2並將aspectY設置爲3.您將獲得所需的圖像尺寸。這是更新的代碼。

private void performCrop(String picUri) { 
    try { 
     //Start Crop Activity 

     Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
     // indicate image type and Uri 
     File f = new File(picUri); 
     Uri contentUri = Uri.fromFile(f); 

     cropIntent.setDataAndType(contentUri, "image/*"); 
     // set crop properties 
     cropIntent.putExtra("crop", "true"); 
     // indicate aspect of desired crop 
     cropIntent.putExtra("aspectX", 2); 
     cropIntent.putExtra("aspectY", 3); 
     // indicate output X and Y 
     cropIntent.putExtra("outputX", 200); 
     cropIntent.putExtra("outputY", 300); 

     // retrieve data on return 
     cropIntent.putExtra("return-data", true); 
     // start the activity - we handle returning in onActivityResult 
     startActivityForResult(cropIntent, RESULT_CROP); 
    } 
    // respond to users whose devices do not support the crop action 
    catch (ActivityNotFoundException anfe) { 
     // display an error message 
     String errorMessage = "your device doesn't support the crop action!"; 
     Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
    }