试图缩小在Android的位图不工作 [英] Trying to scale down a Bitmap in Android not working

查看:116
本文介绍了试图缩小在Android的位图不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图缩小位图。总之,形象最初是从与4016宽度的ByteArray后,我缩放图像打倒工厂选项,它仍然报告图像的4016的宽度。

下面是我的code两个片段:

 位图MYBITMAP = NULL;
        @覆盖
        受保护的byte [] doInBackground(对象... PARAMS){


            最后BitmapFactory.Options选项=新BitmapFactory.Options();
            options.inJustDe codeBounds = TRUE;

            如果(options.outHeight> options.outWidth){
                options.inSampleSize = calculateInSampleSize(选项,640,960);
            } 其他 {
                options.inSampleSize = calculateInSampleSize(选项,960,640);
            }

            options.inJustDe codeBounds = FALSE;

            // myImageByteArray是4016宽
            MYBITMAP = BitmapFactory.de codeByteArray(myImageByteArray,0,myImageByteArray.length,期权);

            //此日志报表输出4016!难道不应该是较小的,因为我只是去codeD的ByteArray与选择?
            Log.d(位图,myBitmap.getWidth()+);

}

        公众诠释calculateInSampleSize(BitmapFactory.Options选项,诠释reqWidth,诠释reqHeight){
            //原始高度和宽度的图像
            最终诠释身高= options.outHeight;
            最终诠释宽度= options.outWidth;
            INT inSampleSize = 1;

            如果(高度> reqHeight ||宽度GT; reqWidth){

                //计算高度和宽度的比例来要求的高度和
                // 宽度
                最终诠释heightRatio = Math.round((浮动)的高度/(浮点)reqHeight);
                最终诠释widthRatio = Math.round((浮点)宽/(浮点)reqWidth);

                //选择最小比例为inSampleSize值,这将
                // 保证
                //与两个尺寸大于或等于最终的图像
                //的
                //请求的高度和宽度。
                inSampleSize = heightRatio< widthRatio? heightRatio:widthRatio;
            }

            返回inSampleSize;
        }
 

更新:

下面是我的code两个片段:

 位图MYBITMAP = NULL;
        @覆盖
        受保护的byte [] doInBackground(对象... PARAMS){


            最后BitmapFactory.Options选项=新BitmapFactory.Options();
            options.inJustDe codeBounds = TRUE;
            // myImageByteArray是4016宽
            MYBITMAP = BitmapFactory.de codeByteArray(myImageByteArray,0,myImageByteArray.length,期权);
            如果(options.outHeight> options.outWidth){
                options.inSampleSize = calculateInSampleSize(选项,640,960);
            } 其他 {
                options.inSampleSize = calculateInSampleSize(选项,960,640);
            }

            options.inJustDe codeBounds = FALSE;

            // myImageByteArray是4016宽
            MYBITMAP = BitmapFactory.de codeByteArray(myImageByteArray,0,myImageByteArray.length,期权);

            // 1000左右,现在这个日志报表输出。
            Log.d(位图,myBitmap.getWidth()+);

}

        公众诠释calculateInSampleSize(BitmapFactory.Options选项,诠释reqWidth,诠释reqHeight){
            //原始高度和宽度的图像
            最终诠释身高= options.outHeight;
            最终诠释宽度= options.outWidth;
            INT inSampleSize = 1;

            如果(高度> reqHeight ||宽度GT; reqWidth){

                //计算高度和宽度的比例来要求的高度和
                // 宽度
                最终诠释heightRatio = Math.round((浮动)的高度/(浮点)reqHeight);
                最终诠释widthRatio = Math.round((浮点)宽/(浮点)reqWidth);

                //选择最小比例为inSampleSize值,这将
                // 保证
                //与两个尺寸大于或等于最终的图像
                //的
                //请求的高度和宽度。
                inSampleSize = heightRatio< widthRatio? heightRatio:widthRatio;
            }

            返回inSampleSize;
        }
 

解决方案

您需要调用 .DE codeByteArray(..)两次!一旦获得宽度和 .inJustDe codeBounds 的高度设置为,然后再次与 .inSampleSize 来获得实际的缩放位图, options.outHeight options.outWidth 在code是可能为零。

呼叫 BitmapFactory.de codeByteArray(myImageByteArray,0,myImageByteArray.length,期权); 前检查 outHeight outWidth

修改

看看这个例子从谷歌的Andr​​oid开发网站

  BitmapFactory.Options选项=新BitmapFactory.Options();
options.inJustDe codeBounds = TRUE;
BitmapFactory.de codeResource(getResources(),R.id.myimage,期权);
INT imageHeight = options.outHeight;
INT ImageWidth等= options.outWidth;
 

注意 options.outHeight 用于调用德codeResources(...)。这你有你的code来解决。

I'm trying to scale down a bitmap. In short, the image is originally from a ByteArray with a width of 4016. After I scale the image down with factory options, it still reports the image with a width of 4016.

Here are two clips of my code:

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;
        }

Update:

Here are two clips of my code:

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;
        }

解决方案

You need to call .decodeByteArray(..) twice! Once to attain the width and the height with .inJustDecodeBounds set to true and then again with .inSampleSize to get the actual scaled Bitmap, options.outHeight and options.outWidth in your code are probably zero.

call BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); before checking outHeight and outWidth.

Edit

Take a look at this example from 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;

Notice that options.outHeight is used after calling decodeResources(...). This you have to fix in your code.

这篇关于试图缩小在Android的位图不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆