为什么在Android的Webview中将位图转换为Base64字符串时会显示黑色背景?

11

我正在使用一个代码将两个图像合并成一个,使用canvas。我将图像显示在ImageView上,看起来很好。但是当我尝试在WebView中展示时,它会在右边显示黑色背景。我尝试在HTML中更改背景颜色,但它没有改变颜色或透明度。有人能帮忙吗?结果在这里 上面的图片是在ImageView中,下面的是在WebView中。

public class MyBimapTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView img1 = (ImageView) findViewById(R.id.ImageView01);

    img1.setVisibility(View.INVISIBLE);
    Drawable dra1 = img1.getDrawable();
    Bitmap map1 = ((BitmapDrawable) dra1).getBitmap();
    ImageView img2 = (ImageView) findViewById(R.id.ImageView02);
    img2.setVisibility(View.INVISIBLE);
    Drawable dra2 = img2.getDrawable();
    Bitmap map2 = ((BitmapDrawable) dra2).getBitmap();

    // ***
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    map1.compress(Bitmap.CompressFormat.JPEG, 100, baos);

    byte[] b = baos.toByteArray();
    String abc = Base64.encodeBytes(b);

    byte[] byt = null;
    try {
        byt = Base64.decode(abc);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    map1 = BitmapFactory.decodeByteArray(byt, 0, byt.length);

    // ***
    Bitmap map = combineImages(map1, map2);
    ByteArrayOutputStream bbb = new ByteArrayOutputStream();
    map.compress(Bitmap.CompressFormat.JPEG, 100, bbb);

    byte[] bit = bbb.toByteArray();

    String imgToString = Base64.encodeBytes(bit);

    String imgTag = "<img src='data:image/jpg;base64," + imgToString
            + "' align='left' bgcolor='ff0000'/>";

    WebView webView = (WebView) findViewById(R.id.storyView);
    webView.loadData(imgTag, "text/html", "utf-8");
    Drawable end = new BitmapDrawable(map);

    ImageView img3 = (ImageView) findViewById(R.id.ImageView03);
    img3.setImageBitmap(map);
}

public Bitmap combineImages(Bitmap c, Bitmap s) { 
    Bitmap cs = null;
    int width, height = 0;

    width = c.getWidth() + (s.getWidth() / 2);
    height = c.getHeight() + (s.getHeight() / 2);

    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas comboImage = new Canvas(cs);

    comboImage.drawBitmap(c, 0f, 0f, null);
    comboImage.drawBitmap(s, c.getWidth() - (s.getWidth() / 2), c
            .getHeight()
            - (s.getHeight() / 2), null);
    return cs;
}

}

1个回答

28

JPEG 格式不支持 alpha 透明度,这就是为什么将原始图像转换为 JPEG 后透明背景会变成黑色的原因。

改用 PNG 格式代替:

 map1.compress(Bitmap.CompressFormat.PNG, 100, baos); 

并且

String imgTag = "<img src='data:image/png;base64," + imgToString               
    + "' align='left' bgcolor='ff0000'/>";   

非常感谢......之前使用了JPEG压缩格式。 在此获取更多详细信息: http://developer.android.com/reference/android/graphics/Bitmap.CompressFormat.html - CoDe
直到compress()为止,答案很完美。但是,imgTag应该放在哪里?你能帮我吗? - VVB
@VVB:我在回答中提到的两行代码都是对 OP 发布的代码进行的修改。在他的代码中,找到这一行 webView.loadData(imgTag, "text/html", "utf-8"); - 这是添加标签的位置。 - MusiGenesis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接