如果我想將捕獲的圖像調整爲固定的高度和寬度(例如:2000px * 3000px),我該怎麼做?我不想使用裁剪。我只想將圖像保存在具有固定高度和寬度的服務器中。調整相機捕捉的圖像而不裁剪
這是我的代碼:
if (resultCode == RESULT_OK && requestCode == REQ_CAMERA_IMAGE) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
/* int newWidth=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 622, getResources().getDisplayMetrics());
int newHeight=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, getResources().getDisplayMetrics());
Bitmap resized = Bitmap.createScaledBitmap(photo, newWidth, newHeight, true);*/
resultIv.setImageBitmap(photo);
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
int x=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 177, getResources().getDisplayMetrics());
int y=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 273, getResources().getDisplayMetrics());
cropView.of(Uri.fromFile(finalFile)).withAspect(x,y).initialize(ImageType.this);
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
/* ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);*/
int newWidth=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 75, getResources().getDisplayMetrics());
int newHeight=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 114, getResources().getDisplayMetrics());
Bitmap resized = Bitmap.createScaledBitmap(inImage, newWidth,newHeight, false);
// Bitmap resized = Bitmap.createScaledBitmap(inImage, newWidth, newHeight, true);
// Bitmap resized = Bitmap.createScaledBitmap(inImage,(int)(inImage.getWidth()*0.9), (int)(inImage.getHeight()*0.9), true);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), resized, "Title", null);
return Uri.parse(path);
}
我已經commeneted,我有tried.It沒有給輸出,因爲我想要的代碼。
你嘗試什麼嗎? –
@Sohail Zahid是的,我用我的代碼編輯了這個問題。 – Amshu