试图缩小Android中的位图无法正常工作

时间:2014-01-09 22:49:35

标签: java android bitmap android-bitmap

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

2 个答案:

答案 0 :(得分:3)

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

在检查BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);outHeight之前调用outWidth

修改

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(...)后使用。您必须修复代码。

答案 1 :(得分:0)

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

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

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

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

顺便说一下,还有另一种方法可以对图像进行缩减,因为我已经显示here