在Appcelerator Titan中将Blob转换为位图图像

时间:2018-10-05 12:55:26

标签: android titanium appcelerator titanium-mobile appcelerator-titanium

我有一个图像的Blob,并希望将其转换为位图图像,以在appcelerator Titanium中使用android Canvas,Path和Paint。 我浏览了appcelerator文档,但找不到任何可以直接转换的方法。 我尝试将blob转换为Base64字符串,然后通过hyperloop使用android native方法创建位图对象,但未成功。

然后我尝试将base64转换为字节数组,并通过Hyperloop使用此代码创建位图,但位图为空:

             var BitmapFactory = require('android.graphics.BitmapFactory');
             var ByteArrayInputStream = require("java.io.ByteArrayInputStream");

            var stringToSaveInDatabase = Ti.Utils.base64encode(newBlob).toString();
            var bytes = [];
            for (var k = 0; k < stringToSaveInDatabase.length; k++) {
                bytes.push(stringToSaveInDatabase.charCodeAt(k));
            }
            var arrayInputStream = new ByteArrayInputStream(bytes);
            var bitmap = BitmapFactory.decodeStream(arrayInputStream); 

1 个答案:

答案 0 :(得分:0)

更新的示例:

var Activity = require('android.app.Activity')
var ImageView = require('android.widget.ImageView');
var Base64 = require('android.util.Base64');
var BitmapFactory = require('android.graphics.BitmapFactory');
var activity = new Activity(Ti.Android.currentActivity);
var RenderScript = require("android.renderscript.RenderScript");
var Allocation = require("android.renderscript.Allocation");
var Element = require("android.renderscript.Element");
var ScriptIntrinsicBlur = require("android.renderscript.ScriptIntrinsicBlur");

$.img.addEventListener("load", function() {
    $.img.toImage(function(blob) {
        var encodeByte = Base64.decode(blob.toBase64(), Base64.NO_WRAP)
        var bmp = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);

        const bmpOut = bmp.copy(bmp.getConfig(), true);
        const rs = RenderScript.create(activity);
        const blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        const allIn = Allocation.createFromBitmap(rs, bmp);
        const allOut = Allocation.createFromBitmap(rs, bmpOut);

        blurScript.setRadius(2.0);
        blurScript.setInput(allIn);
        blurScript.forEach(allOut);
        allOut.copyTo(bmpOut);
        bmp.recycle();
        rs.destroy();

        var image = new ImageView(activity);
        $.index.add(image);
        image.setImageBitmap(bmpOut);
    })
})

$.index.open();

index.xml:

<Alloy>
    <Window class="container">
        <ImageView id="img" image="/images/DefaultIcon.png" />
    </Window>
</Alloy>

bmp将是ImageView中的位图,它将应用模糊效果并将其再次添加到窗口中。