首页 > 解决方案 > 更改 IMAGE_CAPTURE Intent 的图片大小/分辨率

问题描述

是否可以更改用于拍照的 Intent 设置以设置生成图像的大小或分辨率?

所以现在我拍了一张照片,生成的图像是用 16MP 和 4608x2304 拍摄的。

我想知道是否可以获得结果图像,例如(例如):2MP 和 460x230 ...

我知道有这种方法:

intent.putExtra("outputX", 460);
intent.putExtra("outputY", 230);

但我正在寻找适用于所有设备的东西(因为当然它们都没有相同的图像尺寸,如果我用硬编码值裁剪它们会很糟糕)......

希望你能明白我的问题是什么..

标签: javaandroidimage

解决方案


您需要拍照而不将其保存在文件中:

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;
}

推荐阅读