java.lang.RuntimeException: Canvas: trying to use a non-premultiplied bitmap android.graphics.Bitma

本文介绍了一种在加载图片过程中遇到的java.lang.RuntimeException异常,并提供了具体的代码示例及解决方案。通过调整Bitmap配置选项inPremultiplied为true,成功避免了使用非预乘Alpha Bitmap引发的问题。
java.lang.RuntimeException: Canvas: trying to use a non-premultiplied bitmap android.graphics.Bitmap@b272989

加载图片的时候发现上述异常。代码如下:

public static FaceImage readImage(String file_name) {
        Log.i(TAG, "Read Image file: " + file_name);

        int SHOTER_SIDE=600;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inPremultiplied = false;
        Bitmap bitmap = BitmapFactory.decodeFile(file_name, options);
        Bitmap bitmap_final = bitmap;

        int oriWidth = bitmap.getWidth();
        int oriHeight = bitmap.getHeight();
        int shorter = oriWidth < oriHeight ? oriWidth:oriHeight;
        if (shorter > SHOTER_SIDE) {
            int height = SHOTER_SIDE;
            int width = SHOTER_SIDE;
            if (oriWidth < oriHeight) {
                height = (int)((float)oriHeight / oriWidth * width);
            } else {
                width = (int)((float)oriWidth / oriHeight * height);
            }
            bitmap_final =  Bitmap.createScaledBitmap(bitmap, width, height, false);
        }

        // Copy bitmap pixels to buffer
        ByteBuffer argb_buf = ByteBuffer.allocate(bitmap_final.getByteCount());
        bitmap_final.copyPixelsToBuffer(argb_buf);

        // Generate FaceImage
        byte[] bytes = argb_buf.array();

        byte[] image_data = new byte[bytes.length/4 * 3];
        for(int i = 0; i < bytes.length; i += 4) {
            int j = i / 4;
            image_data[j * 3 + 0] = (byte)(((int)(bytes[i + 2]))&0xFF);
            image_data[j * 3 + 1] = (byte)(((int)(bytes[i + 1]))&0xFF);
            image_data[j * 3 + 2] = (byte)(((int)(bytes[i + 0]))&0xFF);
        }

        FaceImage image = new FaceImage(bitmap_final.getWidth(), bitmap_final.getHeight(), 3, image_data);

        return image;
    }
发现是options.inPremultiplied = false出的问题。注释掉就行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值