【问题标题】:Trying to scale down a Bitmap in Android not working尝试在 Android 中缩小位图不起作用
【发布时间】:2014-01-28 17:58:12
【问题描述】:

我正在尝试缩小位图。简而言之,图片最初来自一个宽度为 4016 的 ByteArray。我使用工厂选项缩小图片后,它仍然报告宽度为 4016 的图片。

这是我的代码的两个片段:

Bitmap myBitmap = null;
        @Override
        protected byte[] doInBackground(Object... params) {


            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;

            if (options.outHeight > options.outWidth) {
                options.inSampleSize = calculateInSampleSize(options, 640, 960);
            } else {
                options.inSampleSize = calculateInSampleSize(options, 960, 640);
            }

            options.inJustDecodeBounds = false;

            //myImageByteArray is 4016 wide
            myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);

            //This log statement outputs 4016!!! Shouldn't it be smaller since I just decoded the byteArray with options?
            Log.d("bitmap", myBitmap.getWidth()+"");

}

        public 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) {

                // Calculate ratios of height and width to requested height and
                // width
                final int heightRatio = Math.round((float) height / (float) reqHeight);
                final int widthRatio = Math.round((float) width / (float) reqWidth);

                // Choose the smallest ratio as inSampleSize value, this will
                // guarantee
                // a final image with both dimensions larger than or equal to
                // the
                // requested height and width.
                inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
            }

            return inSampleSize;
        }

更新:

这是我的代码的两个片段:

Bitmap myBitmap = null;
        @Override
        protected byte[] doInBackground(Object... params) {


            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            //myImageByteArray is 4016 wide
            myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);
            if (options.outHeight > options.outWidth) {
                options.inSampleSize = calculateInSampleSize(options, 640, 960);
            } else {
                options.inSampleSize = calculateInSampleSize(options, 960, 640);
            }

            options.inJustDecodeBounds = false;

            //myImageByteArray is 4016 wide
            myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);

            //This log statement outputs around 1000 now. 
            Log.d("bitmap", myBitmap.getWidth()+"");

}

        public 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) {

                // Calculate ratios of height and width to requested height and
                // width
                final int heightRatio = Math.round((float) height / (float) reqHeight);
                final int widthRatio = Math.round((float) width / (float) reqWidth);

                // Choose the smallest ratio as inSampleSize value, this will
                // guarantee
                // a final image with both dimensions larger than or equal to
                // the
                // requested height and width.
                inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
            }

            return inSampleSize;
        }

【问题讨论】:

    标签: java android bitmap android-bitmap


    【解决方案1】:

    您需要拨打.decodeByteArray(..) 两次!一次达到宽度和高度,将.inJustDecodeBounds 设置为true,然后再次使用.inSampleSize 获得实际缩放的位图,代码中的options.outHeightoptions.outWidth 可能为零。

    在检查outHeightoutWidth 之前致电BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);

    编辑

    看看这个来自Google's Android Dev site的例子:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    

    请注意,options.outHeight调用decodeResources(...) 之后使用。这个你必须在你的代码中修复。

    【讨论】:

    • 你的回答让我有点困惑。你能进一步解释一下吗?
    • 我发布了更新。我只添加了一行。希望这就是你的意思。请告诉我。谢谢
    • 是的!只是想确保我没有遗漏任何东西。我对您的第一个答案感到困惑,因为我认为您需要我做两件事来解决它。非常感谢!
    【解决方案2】:

    您没有使用任何会设置其“outHeight”和“outWidth”的内容填充 bitmapOptions。

    你应该看看谷歌的位图教程:

    http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    为了确定要缩放到的值是多少,您需要知道图像的大小与目标大小的比较。这就是为什么你需要解码两次。

    顺便说一句,还有另一种对图像进行下采样的方法,正如我所展示的 here

    【讨论】:

      猜你喜欢
      • 2018-07-16
      • 2017-08-27
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      • 2017-08-13
      • 2017-11-16
      • 2018-01-04
      • 1970-01-01
      相关资源
      最近更新 更多