Android cordova-plugin-camera在PNG上添加黑色背景

时间:2015-07-12 10:53:24

标签: android image cordova base64 transparency

测试:Android 4.2& Android 5.1.1

插件:https://github.com/apache/cordova-plugin-camera

当我们从库导入带有alpha(透明)图层的PNG时,会自动添加黑色背景。

你知道如何将黑色背景替换为插件返回的base64字符串中的白色背景吗?

使用的代码:

var options = {
                        quality: 95,
                        destinationType: Camera.DestinationType.DATA_URL,
                        sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
                        allowEdit: true,
                        encodingType: Camera.EncodingType.PNG,
                        saveToPhotoAlbum: false
                    };

1 个答案:

答案 0 :(得分:0)

我找到了一种方法来阅读Android Bitmap: Convert transparent pixels to a color

然后应用于我们的代码,你必须更新CameraLauncher.java:

添加要编辑的库:

import android.graphics.Canvas; 
import android.graphics.Color; 

然后添加第595行(如果你已经添加了两个导入),这段代码从另一个线程中获取并改编:

Bitmap imageWithBG = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),bitmap.getConfig());  // Create another image the same size
imageWithBG.eraseColor(Color.WHITE);  // set its background to white, or whatever color you want
Canvas canvas = new Canvas(imageWithBG);  // create a canvas to draw on the new image
canvas.drawBitmap(bitmap, 0f, 0f, null); // draw old image on the background
bitmap.recycle();  // clear out old image
bitmap = imageWithBG;

我提出了拉取请求,也许它会集成到下一次更新中。

相关问题