图像质量的变化,而编码/解码图像的base64字符串 [英] Image quality changes while encoding/decoding image to base64 string

查看:203
本文介绍了图像质量的变化,而编码/解码图像的base64字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用我有用户选择他/她的移动图像文件,在一个TextView显示出来,然后我在一个JSON字符串导航到下一个页面之前保存(导航到下一个页面是一样的重装用新的数据组)的活性。

In my android app I am having user select an image file from his/her mobile, display it in a textview and then I am saving it in a json string before navigating to next page(navigating to next page is same as reloading the activity with new set of data).

我使用以下两种方法可绘制转换为EN codeD字符串,脱code中的字符串返回到可绘制。

I am using the below two methods for converting a Drawable to encoded string and to decode the string back to Drawable.

public String encodeImageToString(Drawable d) throws Exception{
    Bitmap bm = ((BitmapDrawable) d).getBitmap();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);     byte[] byteArrayImage = baos.toByteArray();
    Toast.makeText(getBaseContext(), "Size After encoding to string:"+byteArrayImage.length, Toast.LENGTH_LONG).show();
    String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
    bm=null;
    baos.close();
    baos=null;
    return encodedImage;
}

public Drawable decodeStringToImage(String encodedImage){   
    byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    Drawable d = new BitmapDrawable(getResources(),decodedByte);
    Toast.makeText(getBaseContext(), "Size after decoding string to image: " + decodedByte.getByteCount(), Toast.LENGTH_LONG).show();


decodedByte = null;
    return d;

}

我现在面临的问题是,我每次导航回该页面时,图像质量下降。也是ByteArray的大小,因为我使用吐司在EN codeImageToString显示(),保持每重装增加。

The issue I am facing is that every time I navigate back to the page, the image quality decreases. And also the bytearray size as I display in encodeImageToString() using the Toast, keep on increasing with every reload.

我的行动顺序
-load从选定的文件映像成的TextView(COM preSS图像加载,我没有共享高于code)
- 一旦用户浏览到下一个页面,我保存的TextView图像分成的base64 EN codeD字符串。在这里,我呼吁法恩codeImageToString(可绘制d)和图像转换为字符串,并将其存储在JSON字符串
- 一旦用户返回到网页上,我检索JSON字符串的字符串,然后来电德codeStringToImage(String s)将取回绘制对象。然后,我显示可绘制成的TextView。

My sequence of action is -load image from selected file into textview ( compress the image while loading. I have not shared that code above) -Once user navigates to next page, I save the textview image into base64 encoded String. Here I call method encodeImageToString(Drawable d) and convert image to string and store it in JSON String -Once user navigates back to the page, I retrieve the string from the JSON String and then call decodeStringToImage(String s) to get back the Drawable. I then display that Drawable into textview.

的问题是,当图像被重新加载到TextView的质量下降。每次重装降低质量。另外,作为我检查的ByteArray大小正如我在EN codeImageToString(显示)不断增加。

The problem is that when image gets reloaded into the textview the quality decreases. Every reloaded decreases the quality. Also as I checked the bytearray size as I display in encodeImageToString() keeps on increasing.

有人可以请建议,如果有什么我很想念这里。重装thhe图像字符串,然后字符串返回到图像应具有的图像的质量或尺寸没有影响。但事实并非如此在这种情况下。

Can someone please suggest if there is anything I am missing here. Reloading thhe image to string and then string back to image should have no impact on quality or size of the image. But that is not so in this case.

推荐答案

每个JPEG编码操作会降低图像质量,因此要保留编码操作,以最小的编号。

Each jpeg encoding operation will decrease the image quality, so you want to keep the number of encoding operations to a minimum.

下面是看待事物的天真的方式:对图像编码的 I 连接code(I)的结果 - &GT ; I +δ,其中δ是COM pression文物。如果你现在连接code这一次,你会做这样的:连接code(1 +δ)你浪费时间和空间编码COM pression文物的。

Here is a naive way of looking at things: The result of encoding an image I is encode(I) -> I + δ where δ are compression artifacts. If you now encode this again, you will do this: encode(I + δ) : you waste time and space encoding compression artifacts.

解决方案:当你显示图像,同时保持连接codeD的base64字符串的副本左右。当选择图像,只传递这个字符串。不要颖code中的形象。

Solution: When you display the image, also keep a copy of the encoded base64 string around. When the image is selected, just transfer this string. Don't reencode the image.

这篇关于图像质量的变化,而编码/解码图像的base64字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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