从Base64解码的图像比正常图像小。 [PNG]

时间:2013-05-03 09:49:36

标签: android base64 decode

我可以成功将给定的Base64字符串转换为Android中的相应图像。 为了在我的应用程序中测试这个场景,我从drawable文件夹中获取了一个图像,并使用此网站将其转换为相应的Base64字符串:Motobit.com。我在这个网站上给出的图像是这样的: enter image description here

尺寸为23X25像素,尺寸为46.3KB。 在我的Android中使用以下代码我将此图像的Base64转换为Image,如下所示:

byte[] decodedString = Base64.decode(tabData.getString("TabIconImageData"), Base64.DEFAULT);
                            BitmapFactory.Options options = new Options();
                            options.inJustDecodeBounds = true;
                            options.inSampleSize = calculateInSampleSize(options, 500, 500);
                            options.inJustDecodeBounds = false;
                            Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length,options);
    myImageView.setImageBitmap(decodedByte);
    public static int calculateInSampleSize(BitmapFactory.Options options,
                int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;

            if (height > reqHeight || width > reqWidth) {

                // Calculate ratios of height and width to requested height and
                // width
                final int heightRatio = Math.round((float) height
                        / (float) reqHeight);
                final int widthRatio = Math.round((float) width / (float) reqWidth);

                // Choose the smallest ratio as inSampleSize value, this will
                // guarantee
                // a final image with both dimensions larger than or equal to the
                // requested height and width.
                inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
            }

            return inSampleSize;
        }

Base64字符串在图像中成功转换,但它看起来大小只是原始图像的一半。我想要原始尺寸的图像以及PNG格式。 请指导我解决这个问题。

1 个答案:

答案 0 :(得分:1)

ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Bitmap btm = decodeBase64("Base64 String");
        Bitmap bt=Bitmap.createScaledBitmap(btm, btm.getWidth(), btm.getHeight(), false);
        company_logo.setImageBitmap(bt);

和这个

public static Bitmap decodeBase64(String input) 
{
    byte[] decodedByte = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
}