【问题标题】:Bitmap color code change after scaledown缩小后的位图颜色代码变化
【发布时间】:2018-05-13 00:38:52
【问题描述】:

我有一张颜色代码为#AAA28B(170R、162G、139B)的图像。现在假设当前图像大小是 1200x1200 现在我想让它缩小到 600x600 所以我使用下面的代码并得到位图但问题是它的位图颜色代码更改为#ABA38C,所以它的 RGB 值增加到加一比较原图颜色变成(171R,163G,140B)怎么防止呢?

请参见附上一张截图,如果原始图像直接从 android drawable 加载,其他图像则使用以下代码加载。

private void loadImage(int width, int height){

        Bitmap bitMapTest=BitmapFactory.decodeResource(getResources(),R.drawable.ic_test);

        Log.i("IMAGE","Image format is"+ bitMapTest.getConfig().name()+
                "Image size is"+ bitMapTest.hasAlpha() + "bitmap size is"+ bitMapTest.getHeight());


        Bitmap decodeBitmap=decodeSampledBitmapFromResource(getResources(),R.drawable.ic_test,width,height);
        Log.i("IMAGE","Image format is"+ decodeBitmap.getConfig().name()+
                "Image size is"+ decodeBitmap.hasAlpha() + "bitmap size is"+ decodeBitmap.getHeight());

         //adjust of alha deos not give me any help result remain same
        //decodeBitmap=adjustOpacity(decodeBitmap);

        mImgView.setImageBitmap(decodeBitmap);

    }

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                         int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPreferredConfig= Bitmap.Config.ARGB_8888;
        options.inScaled=false;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    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 = 2;

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

这是原图:-

【问题讨论】:

  • 位图怎么会有颜色代码?像素有颜色代码。
  • 我有全彩色图像,让我分享一下吧
  • decodeBitmap=adjustOpacity(decodeBitmap);。请尝试不使用。
  • I have one image which have color code 。位图和图像没有颜色代码。只有像素可以。请编辑。
  • @greenapps 很抱歉错过了颜色代码指南,我也尝试删除 decodeBitmap=adjustOpacity(decodeBitmap); 但结果相同

标签: android colors bitmap


【解决方案1】:

对于像this 这样的实体图像,似乎应用以下代码进行缩小工作正常。

Bitmap  decodeBitmap=Bitmap.createScaledBitmap(sourceBitmap,width,height,false/*Make sure filter is false*/);

【讨论】:

    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多