在 Android 中调整位图大小 [英] Resize Bitmap in Android

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

问题描述

我使用以下代码调整位图的大小:

I resize a bitmap using the following code:

FileOutputStream out = new FileOutputStream("/sdcard/mods.png");
Bitmap bmp = Bitmap.createScaledBitmap(pict, (int)(pict.getWidth() / totScale),  
    (int)(pict.getHeight() / totScale), false);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();

从我正在使用的相机中获取位图的代码如下:

The code for getting the bitmap from the camera that I am using is the following:

mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            pict = BitmapFactory.decodeByteArray(data, 0, data.length);
        }
});

第一张图片是我在手机上看到的(在 Astro 文件管理器中),也是当我在画布上的应用程序中绘制位图时看到的.这发生在我测试过的每台设备上(HTC Legend 和 Galaxy Tab) 第二张图片是它在我电脑上的样子.是什么导致设备上的阻塞?

The first picture is what I can see on the phone (in Astro file manager), and also when I draw the bitmap in my application on a canvas. This happens on every device I've tested on (HTC Legend and Galaxy Tab) The second picture is what it looks like on my computer. What is causing the blocks on the device?

以下是解决我的问题的方法:而不是

Here is what fixed my problem: Instead of

pict = BitmapFactory.decodeByteArray(data, 0, data.length);

我把它换成了

BitmapFactory.Options opts = new BitmapFactory.Options();               
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
pict = BitmapFactory.decodeByteArray(data, 0, data.length, opts);

推荐答案

1) 尝试在调用 createScaledBitmap() 时将过滤器参数从false"更改为true".我打赌这会解决你的问题.

1) Try changing the filter parameter from "false" to "true" in your call to createScaledBitmap(). I bet that will fix your problem.

您应该阅读这篇文章:http://www.curious-creature.org/2010/12/08/bitmap-quality-banding-and-dithering/

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

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