你能解释一下这段代码的作用吗?

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

此代码用于压缩和调整我的应用程序中的图像图像显示模糊,大小减少到很多。你能解释这段代码是如何工作的,以及如何利用现有代码提高图像质量。

BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            final int REQUIRED_SIZE = 200;

            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                    && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            Bitmap bit1 = BitmapFactory.decodeStream(new FileInputStream(f),
                    null, o2);
android image-processing
1个回答
0
投票

您的代码只是初始化位图选项以使用decodeStream(您将使用的输入图像或文件)之后设置一个规则,即所需的大小应该> = 200宽度和高度,之后只是创建一个带有所需流输出的最终位图

BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            final int REQUIRED_SIZE = 200;

            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                    && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            Bitmap bit1 = BitmapFactory.decodeStream(new FileInputStream(f),
                    null, o2);
bit1.compress(Bitmap.CompressFormat.PNG, 100, out); //you can use this line and play with the value 100 in order to set quality of the image when its compresed

由于您的图像缩放到一定大小(> = 200宽度和高度),因此图像输出尺寸将首先取决于输入位图

如果要查看位图的尺寸,可以执行此操作

Log.e("Dimensions", bit1.getWidth()+" "+bit1.getHeight());

Bitmap.compress

compress(Bitmap.CompressFormat format,int quality,OutputStream stream)将位图的压缩版本写入指定的输出流。

编辑:正如Vladyslav Matviienko建议您可以将REQUIRED_SIZE增加到更大的值,因为就像我上面写的那样,你首先设置一个固定的大小

© www.soinside.com 2019 - 2024. All rights reserved.