首页 > 解决方案 > 如何缩小固定宽度的照片?

问题描述

我使用fotoapparat.io 库来拍摄我的照片。由于我不需要那些 12 或 15 兆像素的全分辨率,我想将该图像缩小到“768 x H”,而 H 是我的高度,取决于相机的纵横比;通常是 4:3 所以 576 像素。

既不调用:

 photoResult.toBitmap(
                    new Function1<Resolution, Resolution>() {
                        @Override
                        public Resolution invoke(Resolution resolution) {
                            float scale = (float) 768 / resolution.width;
                            return scaled(scale).invoke(resolution);
                        }
                    }
            ) 

也没有缩放:

    photoResult.toBitmap(
        new Function1<Resolution, Resolution>() {
            @Override
            public Resolution invoke(Resolution resolution) {
                int height = 768;
                int width = 576;
                Resolution r = new Resolution(width, height);
                return scaled(1.0F).invoke(r);
            }
        }
    )

对我有帮助。第二个例子,当使用固定的 1.0F 作为缩放因子时,它会扭曲图像。或者 4/3 作为缩放因子,图像具有良好的比例,但随后缩小到 576 x 432 像素。

如何在不改变不同相机的纵横比的情况下将图像缩小到固定宽度?

标签: androidandroid-cameraandroid-camera2

解决方案


我有 java 代码来重新缩放位图。我用它把我的图像缩小到“600 x H”。

private Bitmap rescaleBitmap(Bitmap bitmap) {
        float constant = 600.0f / bitmap.getWidth();
        return Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth() * constant),
                (int) (bitmap.getHeight() * constant), true);
    }


推荐阅读