【发布时间】:2018-01-19 19:56:25
【问题描述】:
我一直在网上搜索以解决我的问题,但我开始认为我没有以正确的方式进行搜索。
假设我有下面的图片,我想做的是用用户上传的图片替换蓝色区域,但我不知道该怎么做。
我找到的最接近的是this,但这是绘制一个红色矩形,其中给出了特定坐标,我需要(我认为)是查找/检测这些坐标并替换他们用图像。 有没有人遇到过这个?你能为我指出正确的方向吗?真的卡在这里了!
注意:我放在这里的图像只是为了问题的目的,这不是我的图像。 感谢您的时间,问候
编辑:
好的,我应该首先将代码放在这里,为此我深表歉意,正如我之前所说,这是我正在使用 phonegap 和 jquery mobile 做的应用程序,该应用程序使用底部的导航栏,其中一个页面称为“预览”,这是我需要图像功能才能工作的地方,但我错过了一些东西,我认为是缺乏知识:
我的 index.html:
<div data-role="page" data-url="preview" id="preview">
<div data-role="header" style="background-color: rgb(182, 202, 51)" data-position="fixed">
<h1 id="titlePreview">PREVIEW</h1>
<a href="#language" id="langBtn" style="width: 20px;height: 20px; background:none; " href="#" data-icon="comment" data-iconpos="notext" class="ui-btn-left"></a>
</div>
<div id="previewSaco" class="containerPreviewSaco">//if i remove this div, the navbar on the bottom disapears.
<canvas id="myCanvas" width="200" height="200">//i´m testing with this size, but yes the image is supose to have a 100% with, in fact the image as 1940x2372.
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar" class="ui-bar">
<ul>
//in here i have my several "li" for navegation and i think it´s irrelevant for my question.
</ul>
</div>
</div>
</div>
我的 .js 文件:
$(document).on("click", "#btnPreview", function (event) {
var ctx = document.getElementById('myCanvas').getContext('2d');
var img = new Image;
img.onload = function(){
ctx.drawImage(img,0,0);
img2.src = 'img/capa.png';
};
var img2 = new Image;
img2.onload = function(){
ctx.globalCompositeOperation = 'destination-over';
//ctx.scale(x, y);
//ctx.rotate(angle);
//ctx.translate(x, y);
//or
//ctx.transform(a, b, c, d, e, f);
ctx.drawImage(img2,0,0);
};
var uri1 = 'img/previewSaco1.png'; //this is the main image, which is suppose to load when i click in the page "preview", **and it´s not happening** and i have create the transparent area to were the uploaded image by the user is also suppose to go.
var uri2 = 'img/capa.png';//this image is a dummy logo to test in the bigger image
img.src = uri1;
});
注意:当我运行应用程序时,phonegap 桌面总是这样说:未定义的内容安全策略已修改为:https://ssl.gstatic.com;style-src' self' 'unsafe-inline' 数据:blob:;media-src *;img-src * 'self' 数据:content:;script-src * 'unsafe-inline' 'unsafe-eval' 数据:blob:;">
【问题讨论】:
-
您必须遍历画布图像的各个像素,并检查它们的颜色。您可能不想使用一个固定的颜色值,而是实现某种阈值。如果像素符合您的替换标准,那么您可以从另一张图像中获得相应的像素值......当然,这需要两个图像具有相同的尺寸,否则您必须先定义如何处理这种情况最重要的是(放大/缩小其中一个图像以匹配另一个,这也可能会拉伸/扭曲它,或者仅将过程应用于有限的部分,...)
-
您是否受限于任何浏览器的任何版本?另外,您是否可以选择在后端服务中处理图像?
-
你能用 alpha 蒙版图像覆盖图像吗?还是您需要将最终图像设为包含所有数据的 1 张图像?
-
我有一个解决方案,我只是不确定它是否跨浏览器,我正在完成笔,给我几分钟。
-
谢谢大家的回复,我忘了说这是一个应用程序,我也在使用phonegap,我不知道这是否会影响...... Lars-Kristian Johansen,是的!我们的目标是只把它变成一张图片。
标签: javascript html css html5-canvas