java - 更改 IMAGE_CAPTURE Intent 的图片大小/分辨率
问题描述
是否可以更改用于拍照的 Intent 设置以设置生成图像的大小或分辨率?
所以现在我拍了一张照片,生成的图像是用 16MP 和 4608x2304 拍摄的。
我想知道是否可以获得结果图像,例如(例如):2MP 和 460x230 ...
我知道有这种方法:
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;
}
推荐阅读
- mysql - 导入到 phpmyadmin 时,特定查询返回解析错误
- python - 从图像中提取相同的点
- mysql - #1442 - 无法更新存储函数/触发器中的表,因为它已被使用
- swift - iOS如何创建一个带有标签的UIView边框
- php - 为什么我的数据没有使用 eloquent 存储在数据库中?
- angular - Angular 响应式表格中的嵌套输入搜索
- android - 如何使导航抽屉在导航图中的每个操作都保持打开状态
- node.js - NodeJS Azure Sequelize SQL Server - 错误:请手动安装繁琐的包
- git - 'reset --hard' 的 git “uncheckout” 不会产生预期的结果
- c# - 在不提供位置 URI 的情况下返回 POST 响应