在 Android 中调整位图大小
我使用以下代码调整位图大小:
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();
从我正在使用的相机获取位图的代码如下:
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)。第二张图片是它在我的计算机上的样子。是什么导致设备上出现阻塞?
解决方案:
这是解决我的问题的方法: 我没有将
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);
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);
}
});
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?
Solution:
Here is what fixed my problem:
Instead of
pict = BitmapFactory.decodeByteArray(data, 0, data.length);
I replaced that with
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
pict = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1) 尝试在调用 createScaledBitmap() 时将过滤器参数从“false”更改为“true”。我打赌这会解决你的问题。
您应该阅读这篇文章:http:// www.curious-creature.org/2010/12/08/bitmap-quality-banding-and-dithering/
1) Try changing the filter parameter from "false" to "true" in your call to createScaledBitmap(). I bet that will fix your problem.
You should read this article: http://www.curious-creature.org/2010/12/08/bitmap-quality-banding-and-dithering/
第一张和第二张图是一样的。
PS
不能压缩 PNG 格式,只能压缩 JPEG。
First and second pictures are the same.
P.S.
You cant compress PNG format, only JPEG.