import { Component, Inject, forwardRef } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Screenshot } from '@ionic-native/screenshot';
import { GlobalFunction } from'../../services/GlobalFuntion';
import { StorageService } from'../../services/StorageService';
// import { Global } from '../../services/Global';
import { InterfaceService } from '../../services/InterfaceService';
import { PushService } from '../../services/PushService';
/**
* Generated class for the SharingPage page.
*
* See http://ionicframework.com/docs/components/#navigation for more info
* on Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-sharing',
templateUrl: 'sharing.html',
})
export class SharingPage {
public canvasImage;
public imageSize : number = 3;
public userListInfo : any[];
constructor(public navCtrl: NavController,
public navParams: NavParams,
public screenshot: Screenshot,
@Inject (forwardRef (() => GlobalFunction)) private globalFunction: GlobalFunction,
@Inject (forwardRef (() => StorageService)) private storageService: StorageService,
@Inject (forwardRef (() => InterfaceService)) public interfaceService: InterfaceService,
@Inject (forwardRef (() => PushService)) public pushService: PushService) {
pushService.sharingEvent$.subscribe(data => this.update(data));
}
ionViewDidLoad() {
console.log('ionViewDidLoad SharingPage');
this.getUserInfo();
}
public update(data) {
this.userListInfo = data;
for(let z=0;z<this.userListInfo.length;z++){
this.attendanceClick(
this.userListInfo[z].userID,
this.userListInfo[z].headImgurl,
this.userListInfo[z].userName,
this.userListInfo[z].company
);
}
console.log('共多少人及格'+this.userListInfo.length);
}
public getUserInfo() {
this.interfaceService.doQueryTest();
}
//添加图片
public addIMage() {
setTimeout(()=>{
this.imageSize++;
if(this.imageSize<this.userListInfo.length){
this.attendanceClick(
this.userListInfo[this.imageSize].userID,
this.userListInfo[this.imageSize].headImgurl,
this.userListInfo[this.imageSize].userName,
this.userListInfo[this.imageSize].company
);
} else {
console.log('全部图片打印完成');
}
}, 1000);
}
//测试绘图
attendanceClick(userID,headImg,userName,company) {
let base64Image = 'assets/images/sharing.png';
//加水印
var canvas = document.createElement('canvas');
var cxt = canvas.getContext('2d');
cxt.fillStyle = 'green';
cxt.fillRect(10, 10, 100, 100);
var img = new Image();
img.src = base64Image;
img.onload = () => {
var date: string = new Date().toLocaleDateString();
var datetime: string = date;//添加日期
canvas.height = img.height;
canvas.width = img.width;
cxt.drawImage(img,0,0,img.width,img.height,0,0,img.width,img.height);
cxt.save();
cxt.font = 20 + "px Arial";
cxt.textBaseline = 'middle';//更改字号后,必须重置对齐方式,否则居中麻烦。设置文本的垂直对齐方式
cxt.textAlign = 'center';
let ftop = 715;
let fleft = 630;
cxt.fillStyle="#000";
cxt.fillText(datetime,fleft,ftop);//文本元素在画布居中方式
try {
let tempImage = new Image();
// tempImage.setAttribute('crossOrigin', 'anonymous');
// tempImage.src = headImg;
tempImage.onload = () => {
let tempImageX = 180;
let tempImageY = 310;
let tempImageW = 140;
let tempImageH = 140;
cxt.drawImage(tempImage,tempImageX,tempImageY,tempImageW,tempImageH);
// 用户名
let tempTextData = userName;
cxt.save();
cxt.font = 40 + "px Arial";
cxt.textBaseline = 'middle';
cxt.textAlign = 'left';
let tempNameX = 360;
let tempNameY = 350;
cxt.fillStyle="#000";
cxt.fillText(tempTextData,tempNameX,tempNameY);
 
// 公司名字
let tempCompanyData = company;
if(this.globalFunction.isNull(tempCompanyData)){
tempCompanyData = '平安人寿';
}
cxt.save();
cxt.font = 40 + "px Arial";
cxt.textBaseline = 'middle';
cxt.textAlign = 'left';
let tempCompanyX = 360;
let tempCompanyY = 420;
cxt.fillText(tempCompanyData,tempCompanyX,tempCompanyY);
this.canvasImage = canvas.toDataURL("image/jpg");
let tempSrc = this.canvasImage.substring(22);
this.interfaceService.doUpdateRankingPath(userID,tempSrc);
}
tempImage.crossOrigin="anonymous";//这是关键
tempImage.src = headImg;//顺序也很重要
} catch (error) {
console.log('出现错误'+error);
}
 
}
}
//测试截图
public testScreenshots() {
this.screenshot.save('jpg', 20, 'myscreenshot.jpg').then((res)=>{
this.globalFunction.showAlert('save成功'+res.filePath);
},(err)=>{
this.globalFunction.showAlert('save失败'+err);
});
}
}
按自己实际需求写的
这里来的的参考
参考链接http://www.cnblogs.com/huihuihui/p/6930434.html

记录一下ionic canvas图片,还有canvas里面的图片跨域的问题的更多相关文章

  1. 图片ping、JSONP和CORS跨域

    置顶文章:<纯CSS打造银色MacBook Air(完整版)> 上一篇:<由外边距合并到BFC> 作者主页:myvin 博主QQ:851399101(点击QQ和博主发起临时会话 ...

  2. JSONP、图片Ping、XMLHttpRequest2.0等跨域资源请求(CORS)

    跨域:当协议.主域名.子域名.端口号中任意一个不相同时都不算同一个域,而在不同域之间请求数据即为跨域请求.解决方法有以下几种(如有错误欢迎指出)以请求图片url为例: 1.通过XMLHttpReque ...

  3. .NET压缩图片保存 .NET CORE WebApi Post跨域提交 C# Debug和release判断用法 tofixed方法 四舍五入 (function($){})(jQuery); 使用VUE+iView+.Net Core上传图片

    .NET压缩图片保存   需求: 需要将用户后买的图片批量下载打包压缩,并且分不同的文件夹(因:购买了多个用户的图片情况) 文章中用到了一个第三方的类库,Nuget下载 SharpZipLib 目前用 ...

  4. js 利用iframe和location.hash跨域解决的方法,java图片上传回调JS函数跨域

    奶奶的:折腾了我二天,最终攻克了!网上有非常多样例. 但跟我的都不太一样,费话不多说了,上图   上代码: IE ,firefix,chrome 測试通过 js :这个主页面,部分代码, functi ...

  5. Ajax跨域、Json跨域、Socket跨域和Canvas跨域等同源策略限制的解决方法

    同源是指同样的协议.域名.port,三者都同样才属于同域.不符合上述定义的请求,则称为跨域. 相信每一个开发者都曾遇到过跨域请求的情况,尽管情况不一样,但问题的本质都能够归为浏览器出于安全考虑下的同源 ...

  6. js获取url参数、图片转本地base64跨域问题

    获取url参数是经常需要用的一个方法,url上的参数可以让我们的程序执行更灵活. 图片转本地也是很实用的,因为海报合成通常只支持本地. 下面我们来看看这些功能的实现: 获取所有参数,采用split拆分 ...

  7. canvas图片的跨域问题

    科普文章from MDN 实践证明这篇里的回答对的: .起个服务器再在chrome里试一下,应该会跑通. .右键chrome,属性,在目标后面加上(有个空格) --allow-file-access- ...

  8. 使用HTML5 canvas做地图(3)图片加载平移放大缩小

    终于开始可以写代码了,手都开始痒了.这里的代码仅仅是在chrome检测过,我可以肯定的是IE10以下浏览器是行不通,我一直在考虑,是不是使用IE禁止看我的篇博客,就是这群使用IE的人,给我加了很多工作 ...

  9. WebRTC从摄像头获取图片传入canvas

    WebRTC从摄像头获取图片传入canvas 前面我们已经能够利用WebRTC的功能,通过浏览器打开摄像头,并把预览的图像显示在video元素中. 接下来我们尝试从视频中截取某一帧,显示在界面上. h ...

随机推荐

  1. linux删除软连接

    #mkdir test_chk #touch test_chk/test.txt #vim test_chk/test.txt (这一步随便在这个test.txt里写点东东即可) 下面我们来创建tes ...

  2. 每日英语:Doc, Do I Need A Juice Cleanse?

    Some drink only vegetable juice. Others soak in Epsom salts. It's all in the pursuit of ridding the ...

  3. Hadoop 2.x 安装常见问题FAQ(一) NodeManager 无法启动问题解决

    一.问题描述 在搭建 Hadoop hadoop-2.4.1 集群的最后一步启动集群,在命令窗口并没有报任何错误,但是Slave 节点的 NodeManager进程始终启动不起来.随后查看了后台启动日 ...

  4. WebMisSharp升级说明,最新版本1.6.0

    尊敬的C3 AM.C3 FX.WebMisSharp用户您好: 非常感谢长期来您对WebMisSharp系列产品的支持,您的使用和反馈是我们进步的最大动力.在你们的帮助下我们又向前迈进了一步,我们功能 ...

  5. Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope

    以下内容摘自部分网友的,并加上了自己的理解 @Service用于标注业务层组件(我们通常定义的service层就用这个) @Controller用于标注控制层组件(如struts中的action.Sp ...

  6. C# 窗口和程序的退出

    Application.Exit(); // 通知所有消息泵必须终止,并且在处理了消息以后关闭所有应用程序窗口. // 由 .NET Compact Framework 支持. Form.Close( ...

  7. java+Quartz实现定时任务

    1.首先:导入quartz相关的jar包,这里我用的是maven构建的项目,pom.xml文件导入如下: <dependency> <groupId>org.quartz-sc ...

  8. 随笔:JS对象无new构造原理

    var myFun = function(words) { if (!(this instanceof myFun)) { return new myFun(words); } this.name = ...

  9. tar分层压缩

    在Linux下使用 tar 命令来将文件打包并压缩是很通常的用法了. 可是Linux的文件系统对文件大小有限制,也就是说一个文件最大不能超过2G,如果压缩包的的内容很大,最后 的结果就会超过2G,又或 ...

  10. python 爬虫练习

    bs去除特定标签. # url import easygui as g import urllib.request from bs4 import BeautifulSoup import os im ...