如何获得向下缩放照片与固定的宽度?

问题描述 投票:1回答:2

我用的是fotoapparat.io library拍我的照片。因为我不需要那些12个或15万像素的全分辨率,我想规模的形象下调至“768×高”,而H是在摄像头方面比我heigth dependinmg;通常为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像素。

如何可以按比例缩小的图像,以固定宽度不扭曲的不同摄像机的纵横比?

android android-camera android-camera2
2个回答
0
投票

我有Java代码来重新调整位图。我用这个比例我的形象下调至“600 x高”。

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

0
投票

除了TakeInfo的答案,你应该存储图像小,因为它是可能的,但是你可以通过改变ImageView的PARAMS随时调整图像

ImageView.getLayoutParams().width = your_width;
ImageView.getLayoutParams().height = your_height;

这里的ImageView的小部件,它为我工作的样本

<ImageView
    android:id="@+id/my_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:layout_margin="5dp"
    android:layout_gravity="center"
    />
© www.soinside.com 2019 - 2024. All rights reserved.