将CustomView导出为Base64编码的PNG

时间:2012-10-09 09:14:40

标签: android png base64 android-view

我遇到了一个问题,将视图上绘制的内容导出为base64编码的PNG。我做了之前的研究,发现这两个主题是关于stackoverflow的:

Android canvas to jpg

Encode and decode bitmap object in base64 string in Android

我的问题是,当我解码编码的字符串时,我的图像只显示顶部图像的大约15%。

我的代码如下:

private void doMagic() {
    View paintview = findViewById(R.id.paintView1);
    Log.d(TAG, "width: " + paintview.getWidth() + " height:" + paintview.getHeight());
    Bitmap bitmap  = Bitmap.createBitmap(paintview.getWidth(), paintview.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    paintview.draw(canvas);
    encodeTobase64(bitmap);
}

public static String encodeTobase64(Bitmap image)
{
    Bitmap immagex=image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    boolean result = immagex.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b,Base64.NO_WRAP);

    Log.d(TAG, ""+ result);
    Log.d(TAG, "baos: " + baos.size());
    Log.d(TAG, "b: " + b.length);
    Log.d(TAG, "imageEncoded: " + imageEncoded.length());
    Log.e(TAG, imageEncoded);
    return imageEncoded;
}

至于日志:

  • 记录的尺寸和解码图像的尺寸相同(其余部分 图像只是白色)
  • 结果是真的
  • baos.size()== b.length == imageEncoded.length()* 6/8

这里是编码的字符串:

iVBORw0KGgoAAAANSUhEUgAAAyAAAAGACAYAAABV8wavAAAABHNCSVQICAgIfAhkiAAAIABJREFUeJzs3XfcFNX1x/EP0uyICEqxK2LHXkDFoNhr7MYWNWpiLFFji4m9997FboK9d7HXaAR7w4qCBRVRUYHfH2f3xz77zOxOudN2vu/XKy+e3Z25cyE+u3vm3HsOiEhYg4DPgX1jjDGvg3k8BeziYBw/M/o8/yQwOMJ4qwDPRZ+OlNTJwDfAIcDcEcdYCHgLONDVpGr8CejY5Jh9gRsSuHbVt0C3iOeuBxzv8XxH4BVg97rnO1Wut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XPJmN+B/TN9sfiOwXcLX3AmYhBoJSjFsB6xS83giMGtK1z4Za+wn+dUX+DTrSYiISP68DCzX5JjHgCHJTyVXzsP2e1QtiwUjSemELVe4JsFriCRtXqxSV1pewfZPSH69CiyT9SRERCQ/emKVlRrZENv06cK7FKN3xdO076lwP7BeQtfbDlvTrrXS0grWw35f0hDkPUyyFSTLLiIiJbITze+4vwYs6eBaQ7FqTlGNwjqAJ2lxrLna6nXPD8GyQEkYgS3tEmklBwJnpnStPwDXpnQtCW8Q8FTWkxARkfy4Htihwes7A1dHHHsgVn2n6jS8G6gF8VeSb763I/AG3p2dn6PtOndXVqGcG/ulHC7HKrml4VosEJF8+hYrKS4iIsLXwJwNXv8C67AcRW3HZIDRwFIRx5oMdIl4bhBnANf5vLYF1idFpExc/b49g3VwT8M4oFdK15JwbgC2z3oSIiKSvRWx3hZ+/oZ9MY/iz8AFNY/7AZ9EHOsEku06/hj2d/VTH

我不知道我做错了什么。

1 个答案:

答案 0 :(得分:0)

所以最终我发现了,出了什么问题。

我做的每件事都是对的。但是我记录到logcat的imageEncoded String被截断了。所以只记录了超过4000个字符的Image。有关详细信息,请参阅this discussion.