[英]Resize image captured by camera without crop
If I want to resize the captured image to a fixed height and width(example:2000px*3000px),how can I do that? 如果我想将捕获的图像调整为固定的高度和宽度(例如:2000px * 3000px),该怎么办? I don't want to use crop.I just want to save the image in server with a fixed height and width.
我不想使用作物,我只想将图像以固定的高度和宽度保存在服务器中。
This is my code: 这是我的代码:
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);
}
I have commeneted the code which I have tried.It is not giving the output as I want. 我已经尝试了代码,但并没有给出我想要的输出。
Using below code you can set max/min height and width of an Image but setting where degree is optional for rotation of image pass 0 if don't want to rotate. 使用下面的代码,您可以设置图像的最大/最小高度和宽度,但是如果不想旋转,则设置度数是可选的,以使图像通过0。
public static bitmap resizeBitmap(Bitmap bitmap){
int width = 2000; // - Dimension in pixels
int height= 2000; // - Dimension in pixels
return Bitmap.createScaledBitmap(bitmap, width, height, false);
}
Resizing bitmap 调整位图大小
int width = 120;
int height= 120;
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, width, height, false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.