0
我設法使用相機拍照或從圖庫中選擇,然後將路徑傳遞給其他活動。在其他活動中,我在ImageView中顯示圖像。如何從路徑中裁剪圖像?
private void initData() {
photoPath = getIntent().getStringExtra("path");
Bitmap photo = BitmapFactory.decodeFile(photoPath);
rotatePhoto(photo);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnCrop:
cropPhoto(photoPath);
break;
case R.id.btnText:
break;
case R.id.btnFrame:
break;
}
}
private void rotatePhoto(Bitmap bitmap) {
ExifInterface exif = null;
try {
exif = new ExifInterface(photoPath);
} catch (IOException e) {
Log.d(TAG, "Rotate photo: " + e.getMessage());
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(270);
break;
}
Bitmap rotatedPhoto = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
ivEdit.setImageBitmap(rotatedPhoto);
}
private void cropPhoto(String path) {
try {
cropImageUri = Uri.parse(path);
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(cropImageUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("scale", true);
cropIntent.putExtra("outputX", 500);
cropIntent.putExtra("outputY", 500);
cropIntent.putExtra("return-data", false);
startActivityForResult(cropIntent, REQUEST_CROP);
} catch (ActivityNotFoundException e) {
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast
.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CROP) {
if (data != null) {
Bundle extras = data.getExtras();
Bitmap bb = extras.getParcelable("data");
ivEdit.setImageBitmap(bb);
}
}
}
如何裁剪此圖像然後更新ImageView?我嘗試過裁剪其他圖像,但他們必須來自畫廊。這甚至有可能嗎?如果沒有,請建議一種方法。我嘗試這樣做的方式是「無法加載照片」。
我不明白這一點,這是如何幫助我。我需要裁剪我已經在imageview中顯示的圖像,然後更新imageview。 – Esteban
爲什麼不能幫助你?你只需要使用你的路徑。所以將'String filePath = getRealPathFromURI(imageUri);'改爲'String filePath = photePath;'。代碼只是比你需要的更多,因此可能有點混亂。但它會創建一個可以分配給ImageView的調整大小的位圖。所以最好說謝謝。 – greenapps
好的,會嘗試。也許我反應過度了,因爲它很混亂。謝謝! – Esteban