2014-05-17 109 views
-1

我从服务器获取原始图像数据,在画布上绘制它的最佳选择是什么,我该如何操作?由于我对html和javascript很新,请给予一点见解。我想知道的是,如果我知道原始图像的宽度和高度,我如何转换原始数据并将其绘制在HTML和JavaScript的画布上。基本上我不知道我需要的原始图像的格式一些转换。javascript中原始图像数据转换

+0

你有没有在[该采取一看](http://www.w3schools.com/html/html5_canvas.asp)页面? – QueryLars

回答

0

您可以使用context.createImageData创建为imageData对象,你可以用你的RGBA数据从服务器填写:

示例代码演示:http://jsfiddle.net/m1erickson/XTATB/

// test data fill a 30x20 canvas with red pixels 

// test width/height is 30x20 
var canvasWidth=30; 
var canvasHeight=20; 

// resize the canvas 
canvas.width=canvasWidth; 
canvas.height=canvasHeight; 

// create some test data (you can use server data in production) 
var d=[]; 
for(var i=0;i<canvasWidth*canvasHeight;i++){ 
    // red & alpha=255 green & blue = 0 
    d.push(255,0,0,255); 
} 

// create a new imageData object with the specified dimensions 
var imgData=ctx.createImageData(canvasWidth,canvasHeight); 

// get the data property of the imageData object 
// this is an array that will be filled with the test data 
var data=imgData.data; 

// copy the test data (d) into the imageData.data (data) 
for(var i=0;i<data.length;i++){ 
    data[i]=d[i]; 
} 

// push the modified imageData object back to the canvas 
ctx.putImageData(imgData,0,0); 
+0

我如何首先从原始图像中提取RGBA值? – user3365783

+0

我是新来的原始图像概念,它是如何工作的。 – user3365783

+0

您的问题意味着您已经从RAW转换为可读格式。原始文件不能直接作为RGB读取而无需转换。使用原始转换器(如许多图像处理应用程序中存在的)来读取原始文件,应用调整并将结果输出为标准图像格式(.png,.jpg等)或获取RGB值。或者,你可以看看imageMagick是否支持你的特定RAW文件的转换:http://www.imagemagick.org/script/formats.php – markE