调整大小/缩放位图后图像质量不佳

Cba*_*bas 75 android image-manipulation

我正在写一个纸牌游戏,需要我的卡在不同情况下的大小不同.我将我的图像存储为位图,以便可以快速绘制和重绘(用于动画).

我的问题是无论我如何尝试和缩放我的图像(无论是通过matrix.postScale,matrix.preScale还是createScaledBitmap函数),它们总是出现像素化和模糊.我知道它导致问题的缩放因为在没有调整大小的情况下绘制时图像看起来很完美.

我已经完成了这两个线程中描述的每个解决方案:
在运行时调整图像大小时,运行时质量问题中的图像的android质量大小
调整

但还是没有到达任何地方.

我使用以下代码存储我的位图(到一个hashmap):

cardImages = new HashMap<Byte, Bitmap>();
cardImages.put(GameUtil.hearts_ace, BitmapFactory.decodeResource(r, R.drawable.hearts_ace));
Run Code Online (Sandbox Code Playgroud)

并使用此方法绘制它们(在Card类中):

public void drawCard(Canvas c)
{
    //retrieve the cards image (if it doesn't already have one)
    if (image == null)
        image = Bitmap.createScaledBitmap(GameUtil.cardImages.get(ID), 
            (int)(GameUtil.standardCardSize.X*scale), (int)(GameUtil.standardCardSize.Y*scale), false);

        //this code (non-scaled) looks perfect
        //image = GameUtil.cardImages.get(ID);

    matrix.reset();
    matrix.setTranslate(position.X, position.Y);

    //These methods make it look worse
    //matrix.preScale(1.3f, 1.3f);
    //matrix.postScale(1.3f, 1.3f);

    //This code makes absolutely no difference
    Paint drawPaint = new Paint();
    drawPaint.setAntiAlias(false);
    drawPaint.setFilterBitmap(false);
    drawPaint.setDither(true);

    c.drawBitmap(image, matrix, drawPaint);
}
Run Code Online (Sandbox Code Playgroud)

任何见解将不胜感激.谢谢

小智 81

使用createScaledBitmap会使您的图像看起来非常糟糕.我遇到了这个问题,我已经解决了.下面的代码将解决问题:

public Bitmap BITMAP_RESIZER(Bitmap bitmap,int newWidth,int newHeight) {    
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;

    }
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案是完美的(它是唯一适用于我的解决方案),但是你可以摆脱`middleX`和`middleY`:只需输入0并声明`canvas.drawBitmap(bitmap,0,0,new Paint(Paint.FILTER_BITMAP_FLAG) ));` (6认同)
  • 我曾经使用与Paint.FILTER_BITMAP_FLAGS不同的颜色绘制我的位图.更改为Paint.FILTER_BITMAP_FLAG已大大改善了我的结果!谢谢! (4认同)
  • 抱歉地说,但是这种解决方案不适用于我。 (3认同)

Lum*_*mis 45

我在低屏幕分辨率下有blury图像,直到我禁用了来自资源的位图加载缩放:

Options options = new BitmapFactory.Options();
    options.inScaled = false;
    Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);
Run Code Online (Sandbox Code Playgroud)

  • 好东西.请注意,a.getResources()可以缩短为getResources(),path是位图的R.drawable.id. (3认同)

War*_*ith 33

createScaledBitmap有一个标志,您可以设置是否应过滤缩放的图像.那个标志改善了位图的质量......

  • 哇,这改善了很多!谢谢,我不敢相信没有人在其他线程上提到这一点......它消除了所有锋利的边缘,但图像仍然非常模糊。关于如何消除模糊有什么想法吗? (2认同)

小智 10

用于

mPaint = new Paint(Paint.FILTER_BITMAP_FLAG); 
Run Code Online (Sandbox Code Playgroud)

Paint.FILTER_BITMAP_FLAG 对我有用

  • 谢谢,这节省了我的一天!我用它作为canvas.drawBitmap方法的参数 (2认同)

Ivo*_*Ivo 8

我假设您正在为低于3.2的Android版本(API级别<12)编写代码,因为从那时起方法的行为

BitmapFactory.decodeFile(pathToImage);
BitmapFactory.decodeFile(pathToImage, opt);
bitmapObject.createScaledBitmap(bitmap, desiredWidth, desiredHeight, false /*filter?*/);
Run Code Online (Sandbox Code Playgroud)

已经改变.

在较旧的平台(API级别<12)上,BitmapFactory.decodeFile(..)方法在默认情况下尝试返回带有RGB_565配置的Bitmap,如果它们找不到任何alpha,这会降低iamge的质量.这仍然可以,因为您可以使用强制执行A​​RGB_8888位图

options.inPrefferedConfig = Bitmap.Config.ARGB_8888
options.inDither = false 
Run Code Online (Sandbox Code Playgroud)

当图像的每个像素的alpha值为255(即完全不透明)时,会出现真正的问题.在这种情况下,即使您的Bitmap具有ARGB_8888配置,Bitmap的标志'hasAlpha'也会设置为false.如果您的*.png文件至少有一个真正的透明像素,则此标志将设置为true,您不必担心任何事情.

因此,当您想要使用创建缩放的位图时

bitmapObject.createScaledBitmap(bitmap, desiredWidth, desiredHeight, false /*filter?*/);
Run Code Online (Sandbox Code Playgroud)

该方法检查'hasAlpha'标志是否设置为true或false,并且在您的情况下将其设置为false,这将导致获得缩放的Bitmap,该Bitmap自动转换为RGB_565格式.

因此,在API级别> = 12时,会有一个名为的公共方法

public void setHasAlpha (boolean hasAlpha);
Run Code Online (Sandbox Code Playgroud)

哪个会解决这个问题.到目前为止,这只是对问题的解释.我做了一些研究,发现setHasAlpha方法已经存在了很长时间并且它是公开的,但是已被隐藏(@hide注释).以下是它在Android 2.3上的定义:

/**
 * Tell the bitmap if all of the pixels are known to be opaque (false)
 * or if some of the pixels may contain non-opaque alpha values (true).
 * Note, for some configs (e.g. RGB_565) this call is ignore, since it does
 * not support per-pixel alpha values.
 *
 * This is meant as a drawing hint, as in some cases a bitmap that is known
 * to be opaque can take a faster drawing case than one that may have
 * non-opaque per-pixel alpha values.
 *
 * @hide
 */
public void setHasAlpha(boolean hasAlpha) {
    nativeSetHasAlpha(mNativeBitmap, hasAlpha);
}
Run Code Online (Sandbox Code Playgroud)

现在这是我的解决方案提案.它不涉及任何位图数据的复制:

  1. 如果当前Bitmap实现具有公共'setHasAplha'方法,则在运行时使用java.lang.Reflect进行检查.(根据我的测试,它完全适用于API级别3,我没有测试过较低版本,因为JNI不起作用).如果制造商明确将其设为私有,受保护或删除,则可能会出现问题.

  2. 使用JNI为给定的Bitmap对象调用'setHasAlpha'方法.即使对于私有方法或字段,这也可以完美地工作.官方认为JNI不会检查您是否违反了访问控制规则.来源:http://java.sun.com/docs/books/jni/html/pitfalls.html(10.9)这给了我们很大的力量,应该明智地使用.我不会尝试修改最终字段,即使它可以工作(只是举个例子).请注意这只是一个解决方法......

这是我对所有必要方法的实现:

JAVA PART:

// NOTE: this cannot be used in switch statements
    private static final boolean SETHASALPHA_EXISTS = setHasAlphaExists();

    private static boolean setHasAlphaExists() {
        // get all puplic Methods of the class Bitmap
        java.lang.reflect.Method[] methods = Bitmap.class.getMethods();
        // search for a method called 'setHasAlpha'
        for(int i=0; i<methods.length; i++) {
            if(methods[i].getName().contains("setHasAlpha")) {
                Log.i(TAG, "method setHasAlpha was found");
                return true;
            }
        }
        Log.i(TAG, "couldn't find method setHasAlpha");
        return false;
    }

    private static void setHasAlpha(Bitmap bitmap, boolean value) {
        if(bitmap.hasAlpha() == value) {
            Log.i(TAG, "bitmap.hasAlpha() == value -> do nothing");
            return;
        }

        if(!SETHASALPHA_EXISTS) {   // if we can't find it then API level MUST be lower than 12
            // couldn't find the setHasAlpha-method
            // <-- provide alternative here...
            return;
        }

        // using android.os.Build.VERSION.SDK to support API level 3 and above
        // use android.os.Build.VERSION.SDK_INT to support API level 4 and above
        if(Integer.valueOf(android.os.Build.VERSION.SDK) <= 11) {
            Log.i(TAG, "BEFORE: bitmap.hasAlpha() == " + bitmap.hasAlpha());
            Log.i(TAG, "trying to set hasAplha to true");
            int result = setHasAlphaNative(bitmap, value);
            Log.i(TAG, "AFTER: bitmap.hasAlpha() == " + bitmap.hasAlpha());

            if(result == -1) {
                Log.e(TAG, "Unable to access bitmap."); // usually due to a bug in the own code
                return;
            }
        } else {    //API level >= 12
            bitmap.setHasAlpha(true);
        }
    }

    /**
     * Decodes a Bitmap from the SD card
     * and scales it if necessary
     */
    public Bitmap decodeBitmapFromFile(String pathToImage, int pixels_limit) {
        Bitmap bitmap;

        Options opt = new Options();
        opt.inDither = false;   //important
        opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
        bitmap = BitmapFactory.decodeFile(pathToImage, opt);

        if(bitmap == null) {
            Log.e(TAG, "unable to decode bitmap");
            return null;
        }

        setHasAlpha(bitmap, true);  // if necessary

        int numOfPixels = bitmap.getWidth() * bitmap.getHeight();

        if(numOfPixels > pixels_limit) {    //image needs to be scaled down 
            // ensures that the scaled image uses the maximum of the pixel_limit while keeping the original aspect ratio
            // i use: private static final int pixels_limit = 1280*960; //1,3 Megapixel
            imageScaleFactor = Math.sqrt((double) pixels_limit / (double) numOfPixels);
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,
                    (int) (imageScaleFactor * bitmap.getWidth()), (int) (imageScaleFactor * bitmap.getHeight()), false);

            bitmap.recycle();
            bitmap = scaledBitmap;

            Log.i(TAG, "scaled bitmap config: " + bitmap.getConfig().toString());
            Log.i(TAG, "pixels_limit = " + pixels_limit);
            Log.i(TAG, "scaled_numOfpixels = " + scaledBitmap.getWidth()*scaledBitmap.getHeight());

            setHasAlpha(bitmap, true); // if necessary
        }

        return bitmap;
    }
Run Code Online (Sandbox Code Playgroud)

加载lib并声明本机方法:

static {
    System.loadLibrary("bitmaputils");
}

private static native int setHasAlphaNative(Bitmap bitmap, boolean value);
Run Code Online (Sandbox Code Playgroud)

原生部分('jni'文件夹)

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := bitmaputils
LOCAL_SRC_FILES := bitmap_utils.c
LOCAL_LDLIBS := -llog -ljnigraphics -lz -ldl -lgcc
include $(BUILD_SHARED_LIBRARY)
Run Code Online (Sandbox Code Playgroud)

bitmapUtils.c:

#include <jni.h>
#include <android/bitmap.h>
#include <android/log.h>

#define  LOG_TAG    "BitmapTest"
#define  Log_i(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  Log_e(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)


// caching class and method IDs for a faster subsequent access
static jclass bitmap_class = 0;
static jmethodID setHasAlphaMethodID = 0;

jint Java_com_example_bitmaptest_MainActivity_setHasAlphaNative(JNIEnv * env, jclass clazz, jobject bitmap, jboolean value) {
    AndroidBitmapInfo info;
    void* pixels;


    if (AndroidBitmap_getInfo(env, bitmap, &info) < 0) {
        Log_e("Failed to get Bitmap info");
        return -1;
    }

    if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
        Log_e("Incompatible Bitmap format");
        return -1;
    }

    if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) {
        Log_e("Failed to lock the pixels of the Bitmap");
        return -1;
    }


    // get class
    if(bitmap_class == NULL) {  //initializing jclass
        // NOTE: The class Bitmap exists since API level 1, so it just must be found.
        bitmap_class = (*env)->GetObjectClass(env, bitmap);
        if(bitmap_class == NULL) {
            Log_e("bitmap_class == NULL");
            return -2;
        }
    }

    // get methodID
    if(setHasAlphaMethodID == NULL) { //initializing jmethodID
        // NOTE: If this fails, because the method could not be found the App will crash.
        // But we only call this part of the code if the method was found using java.lang.Reflect
        setHasAlphaMethodID = (*env)->GetMethodID(env, bitmap_class, "setHasAlpha", "(Z)V");
        if(setHasAlphaMethodID == NULL) {
            Log_e("methodID == NULL");
            return -2;
        }
    }

    // call java instance method
    (*env)->CallVoidMethod(env, bitmap, setHasAlphaMethodID, value);

    // if an exception was thrown we could handle it here
    if ((*env)->ExceptionOccurred(env)) {
        (*env)->ExceptionDescribe(env);
        (*env)->ExceptionClear(env);
        Log_e("calling setHasAlpha threw an exception");
        return -2;
    }

    if(AndroidBitmap_unlockPixels(env, bitmap) < 0) {
        Log_e("Failed to unlock the pixels of the Bitmap");
        return -1;
    }

    return 0;   // success
}
Run Code Online (Sandbox Code Playgroud)

而已.我们完了.我已经发布了整个代码用于复制和粘贴目的.实际的代码并不是那么大,但是进行所有这些偏执的错误检查会使它变得更大.我希望这对任何人都有帮助.


goR*_*Gon 7

良好的缩减算法(不是最近的邻居,因此没有添加像素化)只包含2个步骤(加上计算输入/输出图像裁剪的精确矩形):

  1. 使用BitmapFactory.Options :: inSampleSize - > BitmapFactory.decodeResource()的缩减尽可能接近你需要但不低于它的分辨率
  2. 通过使用Canvas :: drawBitmap()降低一点点来获得精确的分辨率

以下是SonyMobile如何解决此任务的详细说明:https://web.archive.org/web/20171011183652/http: //developer.sonymobile.com/2011/06/27/how-to-scale-images-for-您-Android的应用程序/

以下是SonyMobile scale utils的源代码:https://web.archive.org/web/20170105181810/http: //developer.sonymobile.com :80/downloads/code-example-module/image-scaling-code- 例如换机器人/