gpt4 book ai didi

javascript - 使用 Mozactivity Firefox OS 使用前置图像作为壁纸

转载 作者:行者123 更新时间:2023-12-03 11:53:00 24 4
gpt4 key购买 nike

我想从网络获取图像并将其设置为 Firefox OS 设备上的壁纸。

到目前为止我已经尝试了两种方法,

$(document).on( 'click', '#share-image', function() {
walp = new Image()
walp.onload = function () {
var walCanvas = document.createElement('canvas');
walCanvas.width = walp.width;
walCanvas.height = walp.height;
var walCtx = walCanvas.getContext('2d');
walCtx.drawImage(walp,0,0);
walCanvas.toBlob( function (walBlob) {
var Activitiy = new MozActivity({
name: 'share',
data: {
type: 'image/*',
number: 1,
blobs: [walBlob]
}
});
});


};
walp.src = image; //image is the link to jpeg.
});

还有这个...

var shareImage = document.querySelector("#share-image"),
imgToShare = document.querySelector('#prependedImage');
alert(imgToShare);
if (shareImage && imgToShare) {
shareImage.onclick = function () {
if(imgToShare.naturalWidth > 0) {
// Create dummy canvas
var blobCanvas = document.createElement("canvas");
blobCanvas.width = imgToShare.width;
blobCanvas.height = imgToShare.height;
// Get context and draw image
var blobCanvasContext = blobCanvas.getContext("2d");
blobCanvasContext.drawImage(imgToShare, 0, 0);
// Export to blob and share through a Web Activitiy
blobCanvas.toBlob(function (blob) {
new MozActivity({
name: "share",
data: {
type: "image/*",
number: 1,
blobs: [blob]
}
});
});
}
else {
alert("Image failed to load, can't be shared");
}
};
}

两者都适用于本地保存的图像,但不适用于从远程位置获取的图像。

我也尝试过以下方法。

  var pre = '<img width="100%" data-l10n-id="prependedImage" id="prependedImage" src="'+image+'" />';
$('#image1').prepend(pre);

$('#prependedImage').attr('src',image);

两者都有效,但都不显示壁纸菜单,除非它是本地保存的图像。我是 javascript 的新手,希望得到一个解释代码示例有什么问题的答案。谢谢。

最佳答案

你能尝试这样的事情吗:

    var xhr = new XMLHttpRequest({
mozSystem: true
});
xhr.open("GET", "http://25.media.tumblr.com/4751d1eccf9311ee0e05bdff819a7248/tumblr_n2yxzxzxr81rsg8blo1_250.png", true);
xhr.responseType = "blob";
xhr.onload = function () {
//sample activity
var activity = new MozActivity({
name: "share",
data: {
type: "image/*",
number:1,
blobs: [this.response],
filenames:["wallpapertest.png"]
},
});
};
xhr.onerror = function () {
alert("Error with System XHR");
};
xhr.send();

您还需要将应用程序设置为特权并在 list 中添加 systemxhr 权限:

  "permissions": {  
"systemXHR": {}
},
"type": "privileged"

关于javascript - 使用 Mozactivity Firefox OS 使用前置图像作为壁纸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25726661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com