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. android 对话框全屏

    对话框风格 <style name="Lam.Dialog.FullScreen" parent="@style/Theme.AppCompat.Dialog&qu ...

  2. 【Big Data - ELK】ELK(ElasticSearch, Logstash, Kibana)搭建实时日志分析平台

    摘要: 前段时间研究的Log4j+Kafka中,有人建议把Kafka收集到的日志存放于ES(ElasticSearch,一款基于Apache Lucene的开源分布式搜索引擎)中便于查找和分析,在研究 ...

  3. linux每日命令(31):tar命令

    tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件.tar最初被用来在磁带上创建档案,现在,用户可以在 ...

  4. Android遍历API (1) 动画篇——克隆动画AnimationCloning

    从我学Android开始,一直就想做一件事.就是好好把APIDemo看一遍.今天开始会抽时间把Android官方的APIDemo程序全部过一遍.主要是为了两个目的:第一,复习以前学习的API用法.第二 ...

  5. opencv 无法使用 dll 动态链接库 UnsatisfiedLinkError java.library.path Can't find dependent libraries

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME) 使用如上方法加载本地 dll文件. 一般会出现两种错误: 1. UnsatisfiedLinkError ja ...

  6. CAP原理中的一致性

    CAP原理指的是,这三个要素最多只能同时实现两点,不可能三者兼顾.因此在进行分布式架构设计时,必须做出取舍.而对于分布式数据系统,分区容忍性是基本要求,否则就失去了价值.因此设计分布式数据系统,就是在 ...

  7. 如何停止和禁用Linux系统中的不需要的服务

    从Linux中删除不需要的服务 在本文中,我们将讨论一些您不需要的不需要的应用程序和服务,但它们是在操作系统安装期间默认安装的,并且不知不觉地开始吃您的系统资源. 让我们首先知道使用以下命令在系统上运 ...

  8. spring boot mybatis 整合教程

    本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper 分页 ...

  9. [原]pomelo基础知识(一)

    1.pomelo基本介绍 http://blog.gfdsa.net/2013/06/04/pomelo/pomelo_study_two/ 2.如何配置一个gate服务器 (1)首先 需要在game ...

  10. [Hinton] Neural Networks for Machine Learning - Bayesian

    Link: Neural Networks for Machine Learning - 多伦多大学 Link: Hinton的CSC321课程笔记 Lecture 09 Lecture 10 提高泛 ...