簡體   English   中英

在不使用意圖的情況下在android中裁剪圖像

[英]Cropping an image in android without using intents

目前我使用默認的Android相機意圖來裁剪圖像。 但是這在某些設備中無法正常工作。 在純Android設備中,不會調用裁剪或返回數據為空。 有沒有其他方法來實現相同的目標。

目前我使用以下代碼開始裁剪,

Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setType("image/*");
if(picUri != null) {
    cropIntent.setDataAndType(picUri, "image/*");
}
String tmpName = imageDir.getAbsolutePath() + File.separator + "img.jpg";
File tmpFile = new File(tmpName);
if (tmpFile.exists()) {
    tmpFile.delete();
}
Uri tmpUri = Uri.fromFile(tmpFile);
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 16);
cropIntent.putExtra("aspectY", 9);
cropIntent.putExtra("outputX", 320);
cropIntent.putExtra("outputY", 180);
cropIntent.putExtra("return-data", true);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, tmpUri);
startActivityForResult(cropIntent, PIC_CROP);

看看這個圖書館。 這支持在不使用相機裁剪意圖的情況下裁剪圖像。 它還支持許多其他功能,如旋轉等。它易於實現

提供的樣本也寫得很好。

如果圖像可以加載到內存中,您可以使用BitmapFactory.decodeFile加載到Bitmap,並使用createBitmap在Bitmap上進行裁剪 小心使用任意圖像執行此操作,因為如果內存不足以加載圖像,則解碼將崩潰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM