gpt4 book ai didi

java - 更改 IMAGE_CAPTURE Intent 的图片大小/分辨率

转载 作者:行者123 更新时间:2023-12-02 10:55:50 24 4
gpt4 key购买 nike

是否可以更改拍照 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;
}

关于java - 更改 IMAGE_CAPTURE Intent 的图片大小/分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51761373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com