Java 更改图像的图片大小/分辨率\u捕获意图
是否可以更改拍照意图的设置以设置结果图像的大小或分辨率 现在我用16MP和4608x2304拍摄了一张照片 我想知道是否有可能得到结果图像,例如: 2MP和460x230 我知道有这样的方法:Java 更改图像的图片大小/分辨率\u捕获意图,java,android,image,Java,Android,Image,是否可以更改拍照意图的设置以设置结果图像的大小或分辨率 现在我用16MP和4608x2304拍摄了一张照片 我想知道是否有可能得到结果图像,例如: 2MP和460x230 我知道有这样的方法: intent.putExtra("outputX", 460); intent.putExtra("outputY", 230); 但我正在寻找一种适用于所有设备的东西(因为它们的图像大小都不一样,如果我用硬编码的值裁剪它们,那就糟了) 希望你能理解我的问题是什么 您需要拍摄照片而不将其保存在文件中:
intent.putExtra("outputX", 460);
intent.putExtra("outputY", 230);
但我正在寻找一种适用于所有设备的东西(因为它们的图像大小都不一样,如果我用硬编码的值裁剪它们,那就糟了)
希望你能理解我的问题是什么 您需要拍摄照片而不将其保存在文件中:
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
之后,您需要通过保存纵横比来缩放结果位图:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
Bitmap scaledImage = scaleBitmap(imageBitmap , 460, 230);
mImageView.setImageBitmap(scaledImage);
}
}
private Bitmap scaleBitmap(Bitmap bm, int maxWidth, int maxHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
if (width > height) {
// landscape
int ratio = width / maxWidth;
width = maxWidth;
height = height / ratio;
} else if (height > width) {
// portrait
int ratio = height / maxHeight;
height = maxHeight;
width = width / ratio;
} else {
// square
height = maxHeight;
width = maxWidth;
}
bm = Bitmap.createScaledBitmap(bm, width, height, true);
return bm;
}
您需要拍摄照片而不将其保存在文件中:
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
之后,您需要通过保存纵横比来缩放结果位图:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
Bitmap scaledImage = scaleBitmap(imageBitmap , 460, 230);
mImageView.setImageBitmap(scaledImage);
}
}
private Bitmap scaleBitmap(Bitmap bm, int maxWidth, int maxHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
if (width > height) {
// landscape
int ratio = width / maxWidth;
width = maxWidth;
height = height / ratio;
} else if (height > width) {
// portrait
int ratio = height / maxHeight;
height = maxHeight;
width = width / ratio;
} else {
// square
height = maxHeight;
width = maxWidth;
}
bm = Bitmap.createScaledBitmap(bm, width, height, true);
return bm;
}
必须使用浮点数获取比率,如果maxWidth或maxHeigh大于高度或宽度,则比率将为0并引发异常。必须使用浮点数获取比率,如果maxWidth或maxHeigh大于高度或宽度,则比率将为0并引发异常。