【问题标题】:zetbaitsu/Compressor resolution not workingzetbaitsu/Compressor 分辨率不工作
【发布时间】:2020-03-03 09:08:12
【问题描述】:

所以我使用 zetbaitsu/Compressor 库进行图像压缩并调整图像大小,但是当我尝试压缩原始尺寸 (3503 x 5254) 的图像尺寸时,以下高度值的结果如下

800,700,600 将给出 (876 x 1314) 的分辨率,它是原始尺寸的 1/8,并且

当值为 900+ 时,图像的分辨率为 (1752 x2627),是原始分辨率的 1/2。

那么有什么方法可以完全按照我们的标准更改尺寸,因为我尝试了其他类似的问题,但我无法获得预期的结果。

【问题讨论】:

    标签: android image-processing compression image-compression


    【解决方案1】:

    实际上只有通过为这个库创建自己的自定义约束才能实现。 这个库通常的分辨率约束(Methodcall)(Method declaration)使用如下方法(found here):

    public static int calculateInSampleSize(
                BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
    
        if (height > reqHeight || width > reqWidth) {
    
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
    
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight
                    && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }
    
        return inSampleSize;
    }
    
    

    因此,只要图像大于请求的大小,它就会将图像的大小减小 2。

    改变这一点最简单的方法可能是创建您自己的自定义约束并将calculateInSampleSize() 调用更改为您自己的方法来计算精确大小的修饰符。

    编辑: 这是一个示例约束,它应该如何工作。到目前为止我无法测试,但这应该让您了解它是如何工作的:

        public class SizeContstraint implements Constraint
        {
    
            private int _height;
    
            // Resulting image height
            public SizeContstraint(int height)
            {
                _height = height;
            }
    
            // Checks, if the constraint is valid
            @Override
            public boolean isSatisfied(@NotNull final File file)
            {
                // Get the file options (height, width, e.t.c)
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    
                // Calculate if the current image is bigger than the necessary size
                return calculateExactSizeModifier(options, _height) > 1;
            }
    
            // This method is only called, if the constraint is invald (the image is too big)
            @NotNull
            @Override
            public File satisfy(@NotNull final File file)
            {
                int height = _height;
                int width = 0;
    
                // Get the file options (height, width, e.t.c)
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    
                // Get the size modifier to be able to resize the image
                int modifier = calculateExactSizeModifier(options, height);
    
                // Calculate the width using the modifier
                width = options.outWidth / modifier;
    
                // Resize the file into a bitmap
                Bitmap bmp = id.zelory.compressor.UtilKt.decodeSampledBitmapFromFile(file, width, height);
                // Write the bitmap back into the file
                return id.zelory.compressor.UtilKt.overWrite(file, bmp, Bitmap.CompressFormat.JPEG, 100);
            }
    
            private int calculateExactSizeModifier(BitmapFactory.Options options, int reqHeight)
            {
                // Raw height and width of image
                final int height = options.outHeight;
                final int width = options.outWidth;
                int modifier = 1;
    
                // Calculate modifier if height bigger than requested height
                if (height > reqHeight)
                {
                    modifier = height / reqHeight;
                }
    
                return modifier;
            }
        }
    
    

    【讨论】:

    • 尝试改变你所说的但没有结果它仍然给出相同的结果
    • 我花了一些时间来更新。我添加了一个约束示例,它是如何工作的。
    猜你喜欢
    • 2011-02-09
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多