android图像序列化透明png

时间:2015-06-23 13:53:51

标签: android image serialization png transparent

作为一名.net开发人员,我刚刚尝试使用我的asmx webservice中的图像。图像以nvarchar(bytearray)的形式保存在数据库中,因此我的webservice返回一个可以转换为位图的字符串。这一切都很好,但我遇到问题,试图获得透明部分的png图像。无论我尝试什么,透明部分总是白色的。问题是,这里可能有一些我做错的事情......

这就是我将我的png写入db(c#)的方式(这可能是我已经失去了透明度了吗?):

    Image toAdd = Image.FromFile(@"I:\Andoid\h1graph.png");

    string imagestring =  System.Convert.ToBase64String(imageToByteArray(toAdd));

这就是我在Android应用程序上使用它的方式:

    Bitmap bmp;

    androidHttpTransport.call(SOAP_ACTION, envelope);

    SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();

    String result = resultsRequestSOAP.toString();

    if (!result.equals("")) {

                 byte[] bloc = Base64.decode(result, Base64.DEFAULT);

                 bmp = BitmapFactory.decodeByteArray(bloc, 0, bloc.length);

                 bmp.setHasAlpha(true);

                 ByteArrayOutputStream os = new ByteArrayOutputStream();

                 bmp.compress(Bitmap.CompressFormat.PNG,  100,  os);

                 byte[] bloc2 = os.toByteArray();

                 Bitmap bPNGCompress =  BitmapFactory.decodeByteArray(bloc2, 0, bloc2.length);

                 viewModel.setThreeD(bPNGCompress) ;

    }

我已经删除了代码,因此只有必要的代码。所以,这有效,但它不保持透明度。

我猜,这是因为BitmapFactory.DecodeByteArray创建了一个没有透明度的位图,因此转换代码的其余部分不会创建透明度。

我现在挣扎了好几天,试图寻找解决方案,但我似乎找不到它。从web服务中消费png图像必须有一个最佳实践,序列化为字符串(?)。

使用资源中的png,透明度正常,所以我知道我的png是透明的,我的UI控件还可以。 (我知道编码不是最先进的,但为了避免复制粘贴所有类型的方法,我替换了一些代码,通常在方法中,使其更具可读性)

我正处于一个项目的开始阶段,正在尝试使用android,所以如果有人对如何将图像放入我的应用程序有完全不同的想法,可以随意发布想法。

1 个答案:

答案 0 :(得分:0)

尝试使用显式字母配置进行解码操作:

BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inPreferredConfig = Bitmap.Config.ARGB_8888;

//然后使用显式alpha选项调用decodeByteArray:

bmp = BitmapFactory.decodeByteArray(bloc, 0, bloc.length, opts);

//然后将相同的选项应用于压缩的bmp版本:

Bitmap bPNGCompress = BitmapFactory.decodeByteArray(bloc2, 0, bloc2.length);

<强>更新 我编写了一个简单的测试应用程序来测试代码并且它可以正常工作(即具有透明度)。 在处理图像之前丢失ALPHA位是100%。

占位符布局:

<ImageView android:background="#FF0000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_marginTop="135dp" />

代码:         ImageView imageView =(ImageView)findViewById(R.id.imageView);

    byte[] imgData = Base64.decode("iVBORw0KGgoAAAANSUhEUgAAAJIAAACSCAMAAACZpWO8AAAC0FBMVEWioqKioqKioqKioqKioqKioqKmpqaurq6urq6tra2urq6urq6lpaWkpKSurq6urq6urq6lpaWurq6urq6urq6urq6lpaWurq6pqamurq6urq6urq6urq6urq6oqKiurq6tra2urq6urq6rq6uurq6mpqaurq6urq6urq6urq6urq6urq6urq6np6eurq6kpKSjo6Opqamurq6urq6urq6urq6urq6urq6urq6ioqKkpKSpqamurq6urq6urq6urq6qqqqurq6urq6urq6srKyurq6mpqaurq6urq6urq6urq6tra2lpaWurq6urq6urq6urq6urq6urq6urq6urq6oqKiurq6oqKiurq6lpaWurq6urq6np6eurq6urq6urq6urq6urq6urq6urq6pqamurq6urq6urq6urq6srKyurq6urq6jo6Ourq6urq6urq6urq6urq6oqKioqKinp6eqqqqnp6eurq6urq6urq6pqamurq6urq6urq6urq6rq6uqqqqkpKSurq6urq6np6eurq6urq6urq6urq6jo6Ourq6urq6urq6urq6lpaWurq6urq6qqqqurq6urq6urq6urq6urq6jo6OkpKSurq6urq6urq6urq6srKympqaurq6urq6rq6uioqKurq6urq6urq6srKyjo6Ourq6urq6kpKSkpKSurq6urq6srKyurq6np6eurq6urq6lpaWurq6urq6urq6urq6mpqampqalpaWurq6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.DEFAULT);
    Bitmap bmp = BitmapFactory.decodeByteArray(imgData, 0, imgData.length);

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, os);

    byte[] compressedData = os.toByteArray();

    Bitmap bPNGCompress =  BitmapFactory.decodeByteArray(compressedData, 0, compressedData.length);

    imageView.setImageBitmap(bPNGCompress);`