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. Apache Spark 2.2.0 正式发布

    本章内容: 待整理 参考文献: Apache Spark 2.2.0正式发布 Spark Release 2.2.0

  2. 第一部分:开发前的准备-第八章 Android SDK与源码下载

    第8章 Android SDK与源码下载 如果你是新下载的SDK,请阅读一下步骤了解如何设置SDK.如果你已经下载使用过SDK,那么你应该使用AVD Manager,来更新即可. 下面是构建Andro ...

  3. [APM] 解读2016之APM国内篇:快速增长的APM市场和技术

    前言 2016年是APM技术和市场快速发展的一年,在这一年里APM市场特别是国内的市场取得了极大的增长,用户对APM价值的认识和接受度也有了很大的提升,国内市场已基本完成了用户教育和市场培养的阶段.与 ...

  4. default listener is not configured in grid infrastructure home

    Oracle Restart enable database creation requries Default listener configured and running in Grid Inf ...

  5. Ubuntu python3 安装pip

    python2:sudo apt-get install python-pip python3:sudo apt-get install python3-pip 切换python2和python3的版 ...

  6. 如何在wiced平台上编译,运行智能彩灯 实时控制彩灯的色调和开关

    https://github.com/WildDogTeam/demo-c-rgblight/tree/master/src/device 恢复出厂的GPIO口在demo_platform.h声明为W ...

  7. android 监听动画对象后不能播放动画

    采用监听  AnimationListener 发现不能播放动画了. 解决办法: 将动画的启动方式:animation.startnow去掉,改为如下即可 view.startAnimation(an ...

  8. Sword redis存取二进制数据

    #include "hiredis/hiredis.h" /* redis头文件 */ #include <stdio.h> #include <stdlib.h ...

  9. 在windows下安装git后没有ssh文件夹

    在windows7下安装git后,运行 cd ~/.ssh $ bash: cd: /c/Users/Administrator/.ssh: No such file or directory 出现以 ...

  10. yum常用命令大全

    yum命令是在Fedora和RedHat以及SUSE中基于rpm的软件包管理器,它可以使系统管理人员交互和自动化地更细与管理RPM软件包,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性 ...