- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
刚开始尝试JIC,却总是遇到这个错误:
Failed to execute 'drawImage' on 'CanvasRenderingContext2D': No function was found that matched the signature.
以下是我的代码:
HTML View :
<input type="file" ng-file-select="uploadJobImage($files)" class="upload" id="imgFle">
Controller 方法:
$scope.uploadJobImage = function($files){
//Images Objects
var source_image = $files[0],
target_img = $files[0];
console.log(source_image);
//(NOTE: see the examples/js/demo.js file to understand how this object could be a local image
//from your filesystem using the File API)
var output_format = {
resizeType : 'jpg',
resizeQuality : 80,
resizeMaxHeight : 456,
resizeMaxWidth : window.innerWidth,
};
target_img.src = jic.compress(source_image,output_format);
}JIC压缩方法:
var jic = {
/**
* Receives an Image Object (can be JPG OR PNG) and returns a new Image Object compressed
* @param {Image} source_img_obj The source Image Object
* @param {Integer} quality The output quality of Image Object
* @param {String} output format. Possible values are jpg and png
* @return {Image} result_image_obj The compressed Image Object
*/
compress: function(sourceImgObj, options){
console.log(sourceImgObj);
// var mime_type = "image/jpeg";
// if(typeof output_format !== "undefined" && output_format=="png"){
// mime_type = "image/png";
// }
// var cvs = document.createElement('canvas');
// cvs.width = source_img_obj.naturalWidth;
// cvs.height = source_img_obj.naturalHeight;
// var ctx = cvs.getContext("2d").drawImage(source_img_obj, 0, 0);
// var newImageData = cvs.toDataURL(mime_type, quality/100);
// var result_image_obj = new Image();
// result_image_obj.src = newImageData;
// return result_image_obj;
var outputFormat = options.resizeType;
var quality = options.resizeQuality * 100 || 70;
var mimeType = 'image/jpeg';
if (outputFormat !== undefined && outputFormat === 'png') {
mimeType = 'image/png';
}
var maxHeight = options.resizeMaxHeight || 300;
var maxWidth = options.resizeMaxWidth || 250;
var height = sourceImgObj.height;
var width = sourceImgObj.width;
// calculate the width and height, constraining the proportions
if (width > height) {
if (width > maxWidth) {
height = Math.round(height *= maxWidth / width);
width = maxWidth;
}
}
else {
if (height > maxHeight) {
width = Math.round(width *= maxHeight / height);
height = maxHeight;
}
}
var cvs = document.createElement('canvas');
cvs.width = width; //sourceImgObj.naturalWidth;
cvs.height = height; //sourceImgObj.naturalHeight;
var ctx = cvs.getContext('2d').drawImage(sourceImgObj, 0, 0, width, height);
var newImageData = cvs.toDataURL(mimeType, quality / 100);
var resultImageObj = new Image();
resultImageObj.src = newImageData;
return resultImageObj.src;
},
非常感谢您对此提供的任何帮助。
最佳答案
终于找到问题了,图片对象source_image
应该是dataURL base 64编码的图片格式,JIC才能正确处理。经过长时间的努力,问题被解决为:
test.html 文件
- 为源图像和目标图像添加了几个 div 以及一个文件选择器
<img src="" height="100%" align="middle" id="source_image">
<img src="" height="100%" align="middle" id="result_image">
<input type="file" ng-file-select="uploadJobImage($files)"
class="upload" id="imgFle">
testController.js
1) 检索文件 blob url
- 使用 var URL = window.webkitURL ||窗口.URL;
var url = URL.createObjectURL($files[0]);
代码:
// Retieves the file's blob url and passes it onto
//convertImgToBase64URL() method to convert it to base 64 encoded format
$scope.uploadJobImage = function($files){
var URL = window.webkitURL || window.URL;
var url = URL.createObjectURL($files[0]);
alert("Image quality : "+$scope.qlty);
var output_format = 'jpg';
$scope.convertImgToBase64URL(url, output_format);
};
2) 使用 Base 64 编码
然后使用 $scope.convertImgToBase64URL(url, output_format);
方法使用 base 64 编码对其进行编码:
代码:
// convertImgToBase64URL returns a dataurl corresponding to the
// input blob url
$scope.convertImgToBase64URL = function(url, outputFormat){
alert();
var canvas = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'),
img = new Image;
img.crossOrigin = 'Anonymous';
img.onload = function(){
var dataURL;
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
$scope.callback(dataURL);
// console.log("hhhh dataurl : "dataURL);
canvas = null;
};
img.src = url;
}
3)使用JIC压缩码:
- 在callback()
方法中,调用JIC图像压缩方法使用:result_image.src = jic.compress(source_image,quality,'jpg').src;
代码:
$scope.callback = function(dataURL){
console.log("callback dataURL: "+dataURL);
document.getElementById("source_image").src = dataURL;
var source_image = document.getElementById('source_image');
var result_image = document.getElementById('result_image');
if (source_image.src == "") {
alert("You must load an image first!");
return false;
}
var quality = parseInt($scope.qlty);
console.log("Quality >>" + quality);
console.log("process start...");
var time_start = new Date().getTime();
// The JIC compression Method
result_image.src = jic.compress(source_image,quality,'jpg').src;
console.log(source_image+' '+quality+' '+output_format);
result_image.onload = function(){
var image_width=$(result_image).width(),
image_height=$(result_image).height();
if(image_width > image_height){
result_image.style.width="320px";
}else{
result_image.style.height="300px";
}
result_image.style.display = "block";
}
var duration = new Date().getTime() - time_start;
console.log("process finished...");
console.log('Processed in: ' + duration + 'ms');
console.log(document.getElementById('result_image'));
}
JIC 开源图像压缩器库:
/*!
* JIC JavaScript Library v1.1
* https://github.com/brunobar79/J-I-C/
*
* Copyright 2012, Bruno Barbieri
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Date: Sat Mar 24 15:11:03 2012 -0200
*/
/**
* Create the jic object.
* @constructor
*/
var jic = {
/**
* Receives an Image Object (can be JPG OR PNG) and returns a new Image Object compressed
* @param {Image} source_img_obj The source Image Object
* @param {Integer} quality The output quality of Image Object
* @param {String} output format. Possible values are jpg and png
* @return {Image} result_image_obj The compressed Image Object
*/
compress: function(source_img_obj, quality, output_format){
var mime_type = "image/jpeg";
if(typeof output_format !== "undefined" && output_format=="png"){
mime_type = "image/png";
}
var cvs = document.createElement('canvas');
cvs.width = source_img_obj.naturalWidth;
cvs.height = source_img_obj.naturalHeight;
// alert(source_img_obj.naturalWidth);
// alert(source_img_obj.naturalHeight);
console.log(source_img_obj);
var ctx = cvs.getContext("2d").drawImage(source_img_obj, 0, 0);
var newImageData = cvs.toDataURL(mime_type, quality/100);
console.log(newImageData);
var result_image_obj = new Image();
result_image_obj.src = newImageData;
return result_image_obj;
},
/**
* Receives an Image Object and upload it to the server via ajax
* @param {Image} compressed_img_obj The Compressed Image Object
* @param {String} The server side url to send the POST request
* @param {String} file_input_name The name of the input that the server will receive with the file
* @param {String} filename The name of the file that will be sent to the server
* @param {function} successCallback The callback to trigger when the upload is succesful.
* @param {function} (OPTIONAL) errorCallback The callback to trigger when the upload failed.
* @param {function} (OPTIONAL) duringCallback The callback called to be notified about the image's upload progress.
* @param {Object} (OPTIONAL) customHeaders An object representing key-value properties to inject to the request header.
*/
upload: function(compressed_img_obj, upload_url, file_input_name, filename, successCallback, errorCallback, duringCallback, customHeaders){
var cvs = document.createElement('canvas');
cvs.width = compressed_img_obj.naturalWidth;
cvs.height = compressed_img_obj.naturalHeight;
var ctx = cvs.getContext("2d").drawImage(compressed_img_obj, 0, 0);
//ADD sendAsBinary compatibilty to older browsers
if (XMLHttpRequest.prototype.sendAsBinary === undefined) {
XMLHttpRequest.prototype.sendAsBinary = function(string) {
var bytes = Array.prototype.map.call(string, function(c) {
return c.charCodeAt(0) & 0xff;
});
this.send(new Uint8Array(bytes).buffer);
};
}
var type = "image/jpeg";
if(filename.substr(-4)==".png"){
type = "image/png";
}
var data = cvs.toDataURL(type);
data = data.replace('data:' + type + ';base64,', '');
var xhr = new XMLHttpRequest();
xhr.open('POST', upload_url, true);
var boundary = 'someboundary';
xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
// Set custom request headers if customHeaders parameter is provided
if (customHeaders && typeof customHeaders === "object") {
for (var headerKey in customHeaders){
xhr.setRequestHeader(headerKey, customHeaders[headerKey]);
}
}
// If a duringCallback function is set as a parameter, call that to notify about the upload progress
if (duringCallback && duringCallback instanceof Function) {
xhr.onprogress = function (evt) {
if (evt.lengthComputable) {
return (evt.loaded / evt.total)*100;
}
};
}
xhr.sendAsBinary(['--' + boundary, 'Content-Disposition: form-data; name="' + file_input_name + '"; filename="' + filename + '"', 'Content-Type: ' + type, '', atob(data), '--' + boundary + '--'].join('\r\n'));
xhr.onreadystatechange = function() {
if (this.readyState == 4){
if (this.status == 200) {
successCallback(this.responseText);
}else if (this.status >= 400) {
if (errorCallback && errorCallback instanceof Function) {
errorCallback(this.responseText);
}
}
}
};
}
};
关于javascript - JIC - Java 图像压缩器 : Failed to execute 'drawImage' on 'CanvasRenderingContext2D' : No function was found that matched the signature,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29207031/
有时我一直在努力理解为什么在尝试创建航路点任务时任务管理器会收到错误“无法执行执行”。我附上了我正在使用的工作流程,如果您能看一下,我将不胜感激。 1.Initialize FlightControl
我正在 Python 中使用 SQLAlchemy 核心,并且我已多次阅读文档,但仍然需要有关 engine.execute() 的说明。与 connection.execute() . 据我了解,e
在我的 Zend 框架项目中,我想检查是否设置了 cookie。如果是这种情况,我想使用 cookie 内容登录用户。 由于我必须在调用任何 Controller 之前执行此自动登录,因此我尝试将其放
我正在尝试为我创建的 2 个选择语句的 UNION 创建一个 View 。 UNION 在单独执行时工作正常 但问题是当我将它作为 View 执行时,只有 UNION 的第一部分被执行。 我正在使用的
下面我写了一个简单的例子来演示我遇到的问题。执行代码后,我得到一个 cygwin 异常 7200。我环顾四周并尝试了一些事情,但没有解决。有人可以解释为什么我得到它,我该如何解决?感谢您抽出宝贵时间,
从池中获取连接然后在连接上调用 execute 而不是直接在池对象上调用 execute 的用例是什么? 在 Pool 的文档中类,显示此示例: con = await pool.acquire()
我正在尝试通过 SQL 将变量中的 2 个值插入表中,代码完成时没有错误,但条目未显示在表中。 我尝试在即时窗口中执行代码,但这给了我一个关于括号的错误(我真的不知道如何在那里正确输入提示),所以我将
我对广播接收器有点困惑。我有一个广播接收器,它在 TIME_SET 和 TIMEZONE_CHANGED 操作时触发(代码在下面给出)。我想知道的是,当 TIME_SET 和 TIMEZONE_CHA
我必须与需要随每个请求发送访问 token 的外部服务集成。访问 token 的到期时间很短(只有几个小时)。我决定以乐观的方式使用访问 token 。我将使用当前 token 调用外部服务。如果出现
如果我在 swift 中运行以下代码,步骤 1.、2.、3. 和 4. 是否始终按此顺序执行(它们应该如此),或者如果循环存在异步执行的风险,排序等,花费的时间比预期的要长? // 1. fo
我在我的 C++ 应用程序中看到访问冲突错误。在发生违规并使用 !analyze 时将 windbg 附加到进程时,我发现访问违规是由于试图执行不可执行的地址。我知道导致此问题的正在执行的地址。什么可
在使用 Ubuntu 大约一年之后,这对我来说是第一次。 我接手了一个跟踪维修的汽车服务项目。我可以看到每个文件的完整源代码,但是有一个没有扩展名的文件,但在 Ubuntu 中,属性显示为可执行文件(
什么是 LinqPad“自动跟踪执行”和“跳转到执行点”?如何使用它们,如果你能给出一个详细的例子将不胜感激。 最佳答案 这不是一个详细的示例,但它说明了该功能。如果你有一个像 "1".Dump()
我使用 Q.js 来实现 promise 。在下面的代码中,每个方法都会进行 ajax 调用,然后返回一个 Promise。一切都按预期进行,每个方法在下一个方法开始之前执行并完成: function
我有一个类,它实现了 Runnable接口(interface),并且是一个一旦启动就会无限期运行的任务(长时间运行的线程)。 public class LongRunningTask impleme
PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: 无效的参数数量:绑定(bind)变量的数量与标记数量不匹配 我收到此错
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
想要为执行的每个 linux 命令添加 aspect:executionTime 有什么方法可以添加默认方面环境,以便必须为执行的 linux 命令获取 executionTime 最佳答案 根据 m
我正在尝试安装一个名为 MFOC 的工具.我按照其网站中提到的说明进行操作,如下所示: ebrahim@ubuntu:~$ cd Desktop/mfoc-master/ ebrahim@ubuntu
我刚开始使用 numba 来提高我的程序的性能。我已经减少了我将要呈现的情况 import numba as nb import numpy as np from time import time d
我是一名优秀的程序员,十分优秀!