在使用javascript库处理图像大小时获取上下文的问题
Problems getting context while using a javascript library to handle image sizing
我在这里使用了一个很好的库来处理通过iphone相机传入的一些大图像,以避免这里的整个子采样戏剧。
我的绘制代码:
function imageLoaded(img, frontCamera) {
element = document.getElementById("canvas1");
var mpImg= new MegaPixImage(img);
// read the width and height of the canvas- scaled down
width = element.width; //188 94x2
height = element.height; //125
//used for side by side comparison of images
w2 = width / 2;
// stamp the image on the left of the canvas
if (frontCamera) {
mpImg.render(element, {maxWidth:94, maxHeight:125});} else{
mpImg.render(element, {maxWidth:94, maxHeight:125});}
//at this point, i want to grab the imageData drawn to the canvas using
//MegaPixImage and continue to do some more image processing, which normally
//would happen by declaring ctx=element.getContext("2d");
//more stuff here
}
图像绘制得很好,…但我似乎找不到一种方法,然后对那个图像进行图像处理。在画布上绘制图像后,我如何获得新的上下文?
也许我必须从库中运行进一步的图像处理,这样我就可以访问上下文,或者从库中剥离上下文绘图。
谢谢你的帮助!
我有一个类似的问题,实际上发现了一个有用的函数来检测子抽样,并且只在发现子抽样时使用MegaPixImage
。
在我的情况下,对于本地文件读取(iPhone相机,在你的情况下),我调用handleFileSelect
函数时,<input type="file">
值被改变(即,当一个文件被选择填充这个输入)。在这个函数中,我调用了一个通用的populateImage
JS函数,它将图像数据绘制到画布上。
handleFileSelect
函数和input
的绑定:
$("#my_file_input").bind('change', function (event) {
handleFileSelect(event);
});
function handleFileSelect(event) {
var reader,
tmp,
file = event.target.files[0];
try {
reader = new FileReader();
reader.onload = function (e) {
tmp = e.target.result.toString();
// In my case, some image data (from Androids, mostly) didn't contain necessary image data, so I added it in
if (tmp.search("image/jpeg") === -1 && tmp.search("data:base64") !== -1) {
tmp = tmp.replace("data:", "data:image/jpeg;");
}
populateImage(tmp);
};
reader.onerror = function (err) {
// Handle error as you need
};
reader.readAsDataURL(file);
} catch (error) {
// Handle error as you need
}
}
然后,我的populateImage
函数(在上面的reader.onload
函数中调用):
function populateImage(imageURL) {
var tmpImage = new Image();
$(tmpImage).load(function () {
var mpImg, mpImgData;
// If subsampling found, render using MegaPixImage fix, grab image data, and re-populate so we can use non-subsampled image.
// Note: imageCanvas is my canvas element.
if (detectSubsampling(this)) {
mpImg = new MegaPixImage(this);
mpImg.render(imageCanvas, {maxWidth: 94, maxHeight: 125});
mpImgData = imageCanvas.toDataURL("image/jpg");
populateImage(mpImgData);
return;
}
// Insert regular code to draw image to the canvas
// Note: ctx is my canvas element's context
ctx.drawImage(tmpImage, 0, 0, 94, 125); // Or whatever x/y/width/height values you need
});
$(tmpImage).error(function (event) {
// Handle error as you need
});
tmpImage.src = imageURL;
}
最后但并非最不重要的是detectSubsampling
函数。请注意,这个方法是从另一个来源找到的,而不是我自己的。
function detectSubsampling(img) {
var iw = img.naturalWidth,
ih = img.naturalHeight,
ssCanvas,
ssCTX;
if (iw * ih > 1024 * 1024) { // Subsampling may happen over megapixel image
ssCanvas = document.createElement('canvas');
ssCanvas.width = ssCanvas.height = 1;
ssCTX = ssCanvas.getContext('2d');
ssCTX.drawImage(img, -iw + 1, 0);
// Subsampled image becomes half smaller in rendering size.
// Check alpha channel value to confirm image is covering edge pixel or not.
// If alpha value is 0 image is not covering, hence subsampled.
return ssCTX.getImageData(0, 0, 1, 1).data[3] === 0;
}
return false;
}
这可能比你预想的要多,但就像我说的,我遇到了一个类似的问题,这个解决方案被证明可以在所有支持canvas的浏览器/设备上工作。
希望有帮助!
相关文章:
- 引导时间选择器在增加或减少小时和分钟的同时获取 NaN
- Mongodb,按日期差异分组并获取小时
- 如何使用moment.js获取24小时内日期之间的时差
- 获取元素左侧的像素Javascript似乎只有在窗口稍微调整大小时才起作用
- 从 javascript 中的字符串中获取差值(小时)
- 使用 Date.js 解析 12 小时格式时获取 null
- 获取列高和宽度,并在调整大小时更新
- 从天气 API 获取每小时信息
- 调整列大小时,我将如何获取网格面板的列宽
- 从时间戳中获取天数小时分钟数
- 根据小时获取时间
- 在 JavaScript 中获取一些添加了小时的日期
- 获取当前时间和中午 12 点之间的小时和分钟差异
- 获取特定小时的记录以及相应数据的平均值
- 完整日历时刻:获取当前时间加2小时
- 如何在javascript中获取日期选择器值的完整(包括小时、分钟和秒)日期格式
- 获取时区时差(以小时为单位)
- 调整窗口大小时,获取新宽度和最后一个宽度
- 获取24小时格式的时间,并使用Javascript AngularJS将其保存在任意变量中
- 从下拉菜单中获取小时分钟值,仅在javascript中的setInterval()中进行初始化