8

我正在尝试缩小位图。简而言之,图像最初来自一个宽度为 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;
        }
4

2 回答 2

3

你需要打.decodeByteArray(..)两次电话!一次获得宽度和高度.inJustDecodeBounds设置为true然后再次.inSampleSize获得实际缩放的位图,options.outHeight并且options.outWidth在您的代码中可能为零。

BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);在检查outHeight和之前打电话outWidth

编辑

看看Google 的 Android Dev 网站上的这个例子:

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(...)

于 2014-01-09T22:57:01.473 回答
0

你没有用任何会设置它的“outHeight”和“outWidth”的东西填充bitmapOptions。

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

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

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

顺便说一句,还有另一种下采样图像的方法,正如我在这里展示的那样。

于 2014-01-10T10:22:13.520 回答