如何保持图像质量BitmapFactory相同 [英] How to keep image quality same in BitmapFactory

查看:184
本文介绍了如何保持图像质量BitmapFactory相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经转换的位图图像到字符串保存:

I've converted an bitmap image into string to save it:

............
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

然后,我检索串位图来设置一个活动的背景就是这样:

Then I retrieve the bitmap from string to set an activity's background just like that:

byte[] temp = Base64.decode(encodedImage, Base64.DEFAULT);
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0,
                temp.length, options);
Drawable d = new BitmapDrawable(getResources(), bitmap);
getWindow().setBackgroundDrawable(d);

一切工作正常,但图像质量大大降低。我怎样才能保持图像质量和原来一样的形象?难道我做错了什么在这里,降低了质量?

Everything works fine but the image quality reduces tremendously. How can I keep the image quality same as the original image? Did I do something wrong here that have reduced the quality?

推荐答案

您遇到此图像质量和内存使用情况之间的权衡情况。看看这一行:

You are having here a tradeoff situation between picture quality and memory usage. Take a look at this line:

photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);

photo.com preSS 明显减少由第二个参数给出的一个因素的图像分辨率,不幸的是,这是你可以得到最好的质量,因为0 - 100之间,100代表你可以得到最好的质量。现在,你有另一种选择,这取决于原始图片的大小你可以只保存图像,而不COM pressing,但是要知道,大多数情况下,这并不工作,Jalvik可以抛出一个OutOfMemoryException,
希望这有助于。

photo.compress is obviously decreasing your image resolution in a factor given by the second parameter, unfortunately, this is the best quality you can get, since between 0 - 100, 100 stands for the best quality you can get. Now, you have another option, depending on the original picture's size you can just save the image without compressing it, but be aware that most cases this doesn't work and Jalvik can throw an OutofMemoryException, hope this helps.

这篇关于如何保持图像质量BitmapFactory相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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