Description

In the field of image processing, We always to show image after modified the image raw data.It is very easy with using c, c++ and other compiled programming language. But, if you use the interpreted programming language like javascript. i think it will maybe more complicated.But, We always can find work way.

The below code is not all, Just some code snippet.

Code snippet

Here rotation ways use the temporary variable to storage temporary image every rotated. before rotatetion, should use src image.

var RotateImg = {
srcImg:"", // storage src img path, when you begin to rotate every time, you should use src image that can reduce the error of each roataiton
tmpImg:"", // storage temp image oath after rotated src image.
}; /** Use the canvas to rotate image raw data to show */
RotateImg.rotate = function(nAngle)
{
var Img = new Image();
Img.addEventListener("load",function(){
var imgW = Img.width;
var imgH = Img.height; var cvs = document.createElement("canvas");
var context = cvs.getContext( "2d" );
var degree = nAngle * Math.PI / 180;
switch ( nAngle )
{
case 0://Clockwise 0
cvs.width = imgW;
cvs.height = imgH;
context.drawImage( Img, 0, 0, imgW, imgH );
break;
case 90://Clockwise 90
cvs.width = imgH;
cvs.height = imgW;
context.rotate( degree );
context.drawImage( Img, 0, -imgH, imgW, imgH );
break;
case 180://Clockwise 180
cvs.width = imgW;
cvs.height = imgH;
context.rotate( degree );
context.drawImage( Img, -imgW, -imgH, imgW, imgH );
break;
case 270://Clockwise 270
cvs.width = imgH;
cvs.height = imgW;
context.rotate( degree );
context.drawImage( Img, -imgW, 0, imgW, imgH );
break;
} /** jpeg level [0,1] */
var dataBase64 = cvs.toDataURL( "image/jpeg", 0.8 ); /** remove base64 description information before image data.---->data:image/png;base64,iVBORw0KG... */
dataBase64 = dataBase64.split( "," )[1];
dataBase64 = window.atob( dataBase64 );
var ia = new Uint8Array( dataBase64.length );
for ( var i = 0; i < dataBase64.length; i++ ) {
ia[i] = dataBase64.charCodeAt( i );
}; var blob = new Blob( [ia], {type:"image/jpeg"} ); /** storage temp image path in member */
RotateImg.tmpImg = window.URL.createObjectURL( blob );
RotateImg.showImg(RotateImg.tmpImg);
},false); Img.src = RotateImg.srcImg;
};

Here another rotation ways not use the temporary variable, Just use the src. This way is more complex.

RotateImg.rotateExampleImage = function( nAngle )
{
imgLoaded = true;
var img = new Image(); img.onload = function()
{
var imgW = img.width;
var imgH = img.height;
var degree = nAngle * Math.PI / 180;
var cvs = document.getElementById( "canvas" );
var context = cvs.getContext( "2d" ); switch ( nAngle )
{
case 0://Clockwise 0
var xyImgScale = imgW / imgH;
var xyCanvasScale = cvs.width / cvs.height;
var finalHeight = 0;
var finalWidth = 0;
if( xyImgScale >= xyCanvasScale ) {
finalWidth = cvs.width * 1;
finalHeight = finalWidth / xyImgScale;
} else {
finalHeight = cvs.height * 1;
finalWidth = finalHeight * xyImgScale;
}
var xPos = ( cvs.width - finalWidth ) / 2;
var yPos = ( cvs.height - finalHeight ) / 2; var maxV = cvs.width > cvs.height ? cvs.width : cvs.height;
context.clearRect( -maxV, -maxV, 2*maxV, 2*maxV );
context.rotate( degree );
context.drawImage( img, 0, 0, imgW, imgH, xPos, yPos,finalWidth, finalHeight );
context.rotate( -degree );
break;
case 90://Clockwise 90
var xyImgScale = imgW / imgH;
var xyCanvasScale = cvs.height / cvs.width;
var finalHeight = 0;
var finalWidth = 0;
if( xyImgScale >= xyCanvasScale ) {
finalWidth = cvs.height * 1;
finalHeight = finalWidth / xyImgScale;
} else {
finalHeight = cvs.width * 1;
finalWidth = finalHeight * xyImgScale;
}
var xPos = ( cvs.height - finalWidth ) / 2;
var yPos = ( cvs.width - finalHeight ) / 2; var maxV = cvs.width > cvs.height ? cvs.width : cvs.height;
context.clearRect( -maxV, -maxV, 2*maxV, 2*maxV );
context.rotate( degree );
context.drawImage( img, 0, 0, imgW, imgH, xPos, yPos-cvs.width , finalWidth,finalHeight );
yPos = yPos-cvs.width;
context.rotate( -degree );
break;
case 180://Clockwise 180
var xyImgScale = imgW / imgH;
var xyCanvasScale = cvs.width / cvs.height;
var finalHeight = 0;
var finalWidth = 0;
if( xyImgScale >= xyCanvasScale ) {
finalWidth = cvs.width * 1;
finalHeight = finalWidth / xyImgScale;
} else {
finalHeight = cvs.height * 1;
finalWidth = finalHeight * xyImgScale;
}
var xPos = ( cvs.width - finalWidth ) / 2;
var yPos = ( cvs.height - finalHeight ) / 2; var maxV = cvs.width > cvs.height ? cvs.width : cvs.height;
context.clearRect( -maxV, -maxV, 2*maxV, 2*maxV ); context.rotate( degree );
context.drawImage( img, 0, 0, imgW, imgH, xPos-cvs.width, yPos-cvs.height,finalWidth, finalHeight );
context.rotate( -degree );
break;
case 270://Clockwise 270
var xyImgScale = imgW / imgH;
var xyCanvasScale = cvs.height / cvs.width;
var finalHeight = 0;
var finalWidth = 0;
if( xyImgScale >= xyCanvasScale ) {
finalWidth = cvs.height * 1;
finalHeight = finalWidth / xyImgScale;
} else {
finalHeight = cvs.width * 1;
finalWidth = finalHeight * xyImgScale;
}
var xPos = ( cvs.height - finalWidth ) / 2;
var yPos = ( cvs.width - finalHeight ) / 2; var maxV = cvs.width > cvs.height ? cvs.width : cvs.height;
context.clearRect( -maxV, -maxV, 2*maxV, 2*maxV ); context.rotate( degree );
context.drawImage( img, 0, 0, imgW, imgH, xPos-cvs.height, yPos, finalWidth,finalHeight );
context.rotate( -degree );
break;
} }; img.src = RotateImg.srcImg;
}

Fit show image in view rect center

RotateImg.showImg = function( imgSrc )
{
var img = new Image();
var loaded = false; function loadHandler() {
if (loaded) {
return;
} loaded = true; /** core code for fit view rect to show image */
var imgW = img.width;
var imgH = img.height;
var xyImgScale = imgW / imgH; var mycvs = document.getElementById( "canvas" );
var xyCanvasScale = mycvs.width / mycvs.height; var finalHeight = 0;
var finalWidth = 0; if( xyImgScale >= xyCanvasScale ){
finalWidth = mycvs.width * 1;
finalHeight = finalWidth / xyImgScale;
}else{
finalHeight = mycvs.height * 1;
finalWidth = finalHeight * xyImgScale;
} var xPos = ( mycvs.width - finalWidth ) / 2;
var yPos = ( mycvs.height - finalHeight ) / 2; var context = mycvs.getContext( "2d" );
context.clearRect( 0, 0, mycvs.width, mycvs.height );
context.drawImage( img, 0, 0, img.width, img.height, xPos, yPos,finalWidth, finalHeight );
} img.addEventListener('load', loadHandler);
img.addEventListener('error', function(event){
//alert("error:");
}); img.src = imgSrc; if (img.complete) {
loadHandler();
} };

Reference

segmentfault

stackoverflow

stackoverflow

stackoverflow


Rotate image and fit show use canvas的更多相关文章

  1. html5 canvas 绘制太极图

    <div class="container"> <canvas id="myCanvas" width="400" hei ...

  2. 讲解Canvas中的一些重要方法

    Canvas所提供的各种方法根据功能来看大致可以分为几类: 第一是以drawXXX为主的绘制方法: 第二是以clipXXX为主的裁剪方法: 第三是以scale.skew.translate和rotat ...

  3. html 5 canvas 绘制太极demo

    一个练习canvas的小案例.其中若有小问题,欢迎大神拍砖……^_* 代码效果预览地址:http://code.w3ctech.com/detail/2500. <div class=" ...

  4. 转载爱哥自定义View系列--Canvas详解

    上面所罗列出来的各种drawXXX方法就是Canvas中定义好的能画什么的方法(drawPaint除外),除了各种基本型比如矩形圆形椭圆直曲线外Canvas也能直接让我们绘制各种图片以及颜色等等,但是 ...

  5. canvas图表详解系列(2):折线图

    本章建议学习时间4小时 学习方式:详细阅读,并手动实现相关代码(如果没有canvas基础,需要先学习前面的canvas基础笔记) 学习目标:此教程将教会大家如何使用canvas绘制各种图表,详细分解步 ...

  6. 基于canvas的电子始终

    //code <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...

  7. canvas旋转文本

    canvas旋转文本 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  8. 大家都能看懂的 canvas基础教程

    原文链接: http://www.shitu91.com/cms/canvasSub/index.html 01.canvas简单的认识 canvas 是html5提供给我们的一个绘图标签 默认大小 ...

  9. Canvas的flag具体的含义

    示例代码: package com.loaderman.customviewdemo; import android.content.Context; import android.graphics. ...

随机推荐

  1. OpenCV 3.2.0 + opencv_contrib+VS2017

    首先本文假定你的电脑已经配置好了OpenCV3.2.0,并且想要在此基础上,添加opencv_contrib.在学习图像识别中的特征点检测和匹配时,需要用到一些常用的算法如FREAK.Surf和Sif ...

  2. iOS-----MFMessageCompose 和 MFMailComposeViewController的使用方法

    MFMessageCompose 和 MFMailComposeViewController的使用方法 使用MFMessageComposeViewCOntroller发短信 应用想自己提供界面让用户 ...

  3. Nginx 设置rewrite规则是遇到的一个{}大括号引发的报错问题

    一个群友提到: 用nginx image_filter模块裁图,用!拼宽高能够实现,现在想用参数传宽高总是报错,配置如下:   location ~ ^/images/.* {     if ( $q ...

  4. HDU3861The King’s Problem

    HDU3861   kosaraju缩点+最小路径覆盖 为什么是最小路径覆盖呢,我们假设有一个如下DAG图 目前我们1出发到了3处,对于3的儿子4.5.6,肯定是不能彼此到达的.所以最好的情况3只能延 ...

  5. B树、B-树、B+树、B*树都是什么

    B树.B-树.B+树.B*树都是什么 B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右 ...

  6. Mac触摸板没有弹性了

    关机后,同时按启动键,空格键左边的option,command键还有p和r,听到开机声音响四声后再松开.一定要同时按!然后触摸板就可以用了. (转自知乎)

  7. Java9的新特性

    2017.9.21延期了好几次的Java9正式发布,在人工智能的时代,java还能不能持续辉煌是个问题.看看java9的新特性没什么让自己想升级的意愿,因为要么时一些特性用不到,要么时已经有其它方案代 ...

  8. 02 - Unit09:动态SQL

    动态SQL 什么是? 系统运行过程中,动态生成的SQL语句 为什么? 当我们不能确定用户操作,所要使用的具体SQL的时候. 案例: 搜索笔记功能 按用户名 笔记本名 笔记名 搜索 搜索功能 按用户 A ...

  9. [转]命令行在IIS添加虚拟目录

    来自:http://www.jb51.net/softjc/29702.htmlMkwebdir -c LocalHost -w "Default Web Site" –v Com ...

  10. pycharm -- 小技巧1 (显示文件的代码结构以及错误提示)

    背景介绍 今天上午,在调用同事昨天给的算法程序时出了点问题,于是请同事来我这边一起调代码.大致场景描述如下: 我:B神,你昨天下班前给我的那个算法程序我这边调用的时候出现错误啦,请你过来看下呗. 同事 ...