哪個庫良好的作物圖像 ,並從畫廊和相機然後裁剪圖像的圖像,並設置爲ImageView的我如何裁剪圖像從圖庫和相機加載沒有任何圖書館使用意圖?
回答
試試這個庫是我在這兩種情況下很好地工作 - https://github.com/lvillani/android-cropimage
更新
這不是完美的代碼b'z我甚至有項目之間,所以我沒有時間讓它清楚,但你可以修改並讓我知道。 很高興在這裏發表您的評論。
private static final int REQUEST_CAMERA = 0;
private static final int CAMERA_REQUEST = 1;
private static final int RESULT_LOAD_IMAGE = 2;
private static final int GALLERY_KITKAT_INTENT_CALLED = 3;
private static final int CROP_IMAGE_GALLERY = 4;
private static final int CROP_IMAGE_CAMERA = 5;
private String FilePath;
private Uri ImageUri, croppedImageUri;
private File pic_file;
private void SelectImage()
{
final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Remove", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
builder.setTitle("Upload Photo");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo")) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
pic_file = new File(Environment.getExternalStorageDirectory(),
"tmp_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
ImageUri = Uri.fromFile(pic_file);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
ImageUri);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} else if (options[item].equals("Choose from Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), RESULT_LOAD_IMAGE);
} else if (options[item].equals("Remove")) {
ivProfilePic.setImageResource(R.mipmap.ic_profile_pic);
FilePath = "";
dialog.dismiss();
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
/*
* After capture Image or Select Image from Gallary On activityResult Call
*
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
{
try {
CropImage();
} catch (Exception e) {
rootLogger.error(Utility.createLogableFormat(Utility.getStringStackTrace(e)));
}
}
else if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
try {
ImageUri = data.getData();
CropImage();
} catch (Exception e) {
rootLogger.error(Utility.createLogableFormat(Utility.getStringStackTrace(e)));
}
}
else if (requestCode == CROP_IMAGE_CAMERA) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
if (extras != null) {
FilePath = ImageUri.getPath();
File f = new File(FilePath);
Bitmap bmp_post_news = decodeSampledBitmapFromResource(f.getAbsolutePath(),120,120);
ivProfilePic.setImageBitmap(bmp_post_news);
}
}
}
else if (requestCode == CROP_IMAGE_GALLERY) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
if (extras != null) {
FilePath = croppedImageUri.getPath();
Bitmap bmp_post_news = decodeSampledBitmapFromResource(FilePath,120,120);
ivProfilePic.setImageBitmap(bmp_post_news);
}
}
}
}
public Bitmap decodeSampledBitmapFromResource(String Filepath,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(Filepath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(Filepath, options);
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
int inSampleSize = 1;
final int height = options.outHeight;
final int width = options.outWidth;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height/2;
final int halfWidth = width/2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight/inSampleSize) > reqHeight
&& (halfWidth/inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
protected void CropImage() {
CropImageIntentBuilder cropImage = new CropImageIntentBuilder(3, 4,120,120, ImageUri);
//cropImage.setOutlineColor(0xFF03A9F4);
cropImage.setSourceImage(ImageUri);
cropImage.setCircleCrop(true);
cropImage.setOutlineCircleColor(Color.WHITE);
cropImage.setOutputQuality(100);
Intent intent = cropImage.getIntent(this);
//intent.putExtra("return-data", false);
startActivityForResult(intent, CROP_IMAGE_CAMERA);
}
https://github.com/jdamcd/android-crop不能正常工作 –
你能告訴我圖像裁剪的完整說明代碼 –
@KalpeshKumawat檢查我的更新答案,並接受答案,如果有幫助。 – androidnoobdev
- 1. 問題裁剪圖像是從相機或圖書館
- 2. 如何從醫學圖書館窗口手機7裁剪圖像
- 3. 圖書館沒有加載
- 4. 從Amazon S3下載圖像時如何使用Picasso圖書館?
- 5. 我想剪裁圖像,並在圖像中查看類型。我如何從相機使用這種類型的裁剪裁剪圖像?
- 6. 如何使用默認裁剪意圖裁剪android棉花糖中的圖像?
- 7. 從現有圖像中裁剪圖像
- 8. 加載我的圖書館
- 9. 我應該需要添加NSPhotoLibraryUsageDescription關鍵info.plist。我沒有使用任何圖書館相關的圖像
- 10. 如何使用任何圖書館
- 11. 錯誤,沒有任何活動來處理意圖裁剪圖像的Android
- 12. 圖像裁剪C#沒有.net庫
- 13. 如何使用PHP調整圖像大小和裁剪圖像?
- 14. 畢加索圖書館和GridView圖像
- 15. 用Libgdx從圖像裁剪圖像
- 16. 我的圖像沒有被剪裁
- 17. 如何裁剪DICOM圖像
- 18. 如何裁剪圖像
- 19. 如何裁剪圖像?
- 20. 我如何從圖書館製作活動意圖?
- 21. 如何使用圖書館
- 22. 裁剪和調整從圖庫或相機中選擇的圖像
- 23. 在Android棒棒糖版本中使用意圖獲取圖像裁剪異常。如何在5.0版本以上使用裁剪圖像裁剪圖像?
- 24. 無法加載圖像在Android使用畢加索圖書館
- 25. Android,如何裁剪圖像視圖
- 26. 圖像庫的裁剪圖像問題
- 27. 如何從圖庫加載圖像到意圖
- 28. 如何使用畢加索圖書館不是從中間,而是從頂部來裁剪圖片?
- 29. 如何從固定尺寸的圖庫裁剪圖像
- 30. 如何使用drawImage()來裁剪圖像?
我都做到了無庫和工作正常 –
visit- http://www.appance.com/?s=crop+android –
@NewCoder你可以在github上分享您的代碼? ,我已經使圖像的分辨率不好 – Moudiz