C++之图片旋转90,再保存
下面测试代码只需要全部放在一个.cpp文件里就行
//#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <math.h>
#include <windows.h>
using namespace std;
#define PI 3.1415926535
//角度到弧度转化
#define RADIAN(angle) ((angle)*PI/180.0)
void Rotation(const string& srcFile,const string& desFile,int angle)
{
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bmiHeader;
FILE *pFile;
if ((pFile = fopen(srcFile.c_str(),"rb")) == NULL)
{
printf("open bmp file error.");
exit(-1);
}
//读取文件和Bitmap头信息
fseek(pFile,0,SEEK_SET);
fread(&bmfHeader,sizeof(BITMAPFILEHEADER),1,pFile);
fread(&bmiHeader,sizeof(BITMAPINFOHEADER),1,pFile);
//先不支持小于16位的位图
int bitCount = bmiHeader.biBitCount;
if (bitCount < 16)
{
exit(-1);
}
int srcW = bmiHeader.biWidth;
int srcH = bmiHeader.biHeight;
//原图像每一行去除偏移量的字节数
int lineSize = bitCount * srcW / 8;
//偏移量,windows系统要求每个扫描行按四字节对齐
int alignBytes = ((bmiHeader.biWidth * bitCount + 31) & ~31) / 8L
- bmiHeader.biWidth * bitCount / 8L;
//原图像缓存
int srcBufSize = lineSize * srcH;
BYTE* srcBuf = new BYTE[srcBufSize];
int i,j;
//读取文件中数据
for (i = 0; i < srcH; i++)
{
fread(&srcBuf[lineSize * i],lineSize,1,pFile);
fseek(pFile,alignBytes,SEEK_CUR);
}
//以图像中心为原点左上角,右上角,左下角和右下角的坐标,用于计算旋转后的图像的宽和高
POINT pLT,pRT,pLB,pRB;
pLT.x = -srcW/2;pLT.y = srcH/2;
pRT.x = srcW/2;pRT.y = srcH/2;
pLB.x = -srcW/2;pLB.y = -srcH/2;
pRB.x = srcW/2; pRB.y = -srcH/2;
//旋转之后的坐标
POINT pLTN,pRTN,pLBN,pRBN;
double sina = sin(RADIAN(angle));
double cosa = cos(RADIAN(angle));
pLTN.x = pLT.x*cosa + pLT.y*sina;
pLTN.y = -pLT.x*sina + pLT.y*cosa;
pRTN.x = pRT.x*cosa + pRT.y*sina;
pRTN.y = -pRT.x*sina + pRT.y*cosa;
pLBN.x = pLB.x*cosa + pLB.y*sina;
pLBN.y = -pLB.x*sina + pLB.y*cosa;
pRBN.x = pRB.x*cosa + pRB.y*sina;
pRBN.y = -pRB.x*sina + pRB.y*cosa;
//旋转后图像宽和高
int desWidth = max(abs(pRBN.x - pLTN.x),abs(pRTN.x - pLBN.x));
int desHeight = max(abs(pRBN.y - pLTN.y),abs(pRTN.y - pLBN.y));
//分配旋转后图像的缓存
int desBufSize = ((desWidth * bitCount + 31) / 32) * 4 * desHeight;
BYTE *desBuf = new BYTE[desBufSize];
//将所有像素都预置为白色
memset(desBuf,255,desBufSize);
//新图像每一行字节数,带有偏移量
int desLineSize = ((desWidth * bitCount + 31) / 32) * 4;
//通过新图像的坐标,计算对应的原图像的坐标
for (i = 0; i < desHeight; i++)
{
for (j = 0; j < desWidth; j++)
{
//转换到以图像为中心的坐标系,并进行逆旋转
int tX = (j - desWidth / 2)*cos(RADIAN(360 - angle)) + (-i + desHeight / 2)*sin(RADIAN(360 - angle));
int tY = -(j - desWidth / 2)*sin(RADIAN(360 - angle)) + (-i + desHeight / 2)*cos(RADIAN(360 - angle));
//如果这个坐标不在原图像内,则不赋值
if (tX > srcW / 2 || tX < -srcW / 2 || tY > srcH / 2 || tY < -srcH / 2)
{
continue;
}
//再转换到原坐标系下
int tXN = tX + srcW / 2; int tYN = abs(tY - srcH / 2);
//值拷贝
memcpy(&desBuf[i * desLineSize + j * bitCount / 8],&srcBuf[tYN * lineSize + tXN * bitCount / 8],3);
}
}
//创建目标文件
HFILE hfile = _lcreat(desFile.c_str(),0);
//文件头信息
BITMAPFILEHEADER nbmfHeader;
nbmfHeader.bfType = 0x4D42;
nbmfHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ desWidth * desHeight * bitCount / 8;
nbmfHeader.bfReserved1 = 0;
nbmfHeader.bfReserved2 = 0;
nbmfHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
//Bitmap头信息
BITMAPINFOHEADER bmi;
bmi.biSize=sizeof(BITMAPINFOHEADER);
bmi.biWidth=desWidth;
bmi.biHeight=desHeight;
bmi.biPlanes=1;
bmi.biBitCount=bitCount;
bmi.biCompression=BI_RGB;
bmi.biSizeImage=0;
bmi.biXPelsPerMeter=0;
bmi.biYPelsPerMeter=0;
bmi.biClrUsed=0;
bmi.biClrImportant=0;
//写入文件头信息
_lwrite(hfile,(LPCSTR)&nbmfHeader,sizeof(BITMAPFILEHEADER));
//写入Bitmap头信息
_lwrite(hfile,(LPCSTR)&bmi,sizeof(BITMAPINFOHEADER));
//写入图像数据
_lwrite(hfile,(LPCSTR)desBuf,desBufSize);
_lclose(hfile);
}
int main(int argc, char* argv[])
{
FILE *pFile;
if ((pFile = fopen("D:\\ZZCprot\\测试打印工具\\TestPrinter\\output\\456test.bmp","rb")) == NULL)
{
printf("open bmp file error.");
return -1;
}
string srcFile("D:\\ZZCprot\\测试打印工具\\TestPrinter\\output\\456test.bmp");
string desFile("D:\\ZZCprot\\测试打印工具\\TestPrinter\\output\\Rotation.bmp");
Rotation(srcFile,desFile,90);
system("pause");
return 0;
}
C++之图片旋转90,再保存的更多相关文章
- ios手机竖屏拍照图片旋转90°问题解决方法
手机拍照会给图片添加一个Orientaion信息(即拍照方向),如下: 用ios手机拍照,系统会给图片加上一个方向的属性, ios相机默认的拍照方向是后摄Home键在右为正,前摄Home键在左为正. ...
- Winform中使用FastReport的PictureObject时通过代码设置图片源并使Image图片旋转90度
场景 FastReport安装包下载.安装.去除使用限制以及工具箱中添加控件: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- opencv图片旋转90度
#include<iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv ...
- CSS使jsp图片旋转90度
<style > img{ margin:100px auto 0; -moz-transform:rotate(-90deg); -webkit-transform:rotate(-90 ...
- 关于android中调用系统拍照,返回图片是旋转90度
转载博客:http://blog.csdn.net/walker02/article/details/8211628 项目开发中遇到的一个问题,对于三星手机在做手机照片选择时出现图片显示不正常,研究后 ...
- iOS 图片旋转方法
iOS 图片旋转方法 通过 CGImage 或 CIImage 旋转特定角度 UIImage可通过CGImage或CIImage初始化,初始化方法分别为init(cgImage: CGImage, s ...
- iOS_UIImage_图片旋转
一.目的: 有时候我们获得到的图片我们不是我们想要的方向,需要对图片进行旋转.比如:图片旋转90度180度等. 二.实现过程. 1.获取到该UIImage. 2.开启上下文. 3.上下文的具体操作. ...
- ffmpeg第五篇:让水印图片旋转起来
这篇把上次挖的坑填上 ffmpeg正式篇的上一篇(传送门)说了,这一篇要让水印旋转起来,但是后面有事情一直没有时间搞,今天,它来了............ 如果想实现旋转的功能,需要使用ffmpeg过 ...
- windows phone 摄像头得到图片是旋转90°
我上个随笔讲到,windows phone 拍出来的photo如果直接使用是反转了90°的. 研究了很久..终于发现问题.其实..这是使用习惯问题... CameraCaptureUI 得到的phot ...
随机推荐
- 面试题思考:解释一下什么叫AOP(面向切面编程)
这种在运行时,动态地将代码切入到类的指定方法.指定位置上的编程思想就是面向切面的编程. AOP是Spring提供的关键特性之一.AOP即面向切面编程,是OOP编程的有效补充. 使用AOP技术,可以将一 ...
- DocumentFragment 不支持 innerHTML
在需要多次使用 innerHTML 的地方,一般是推荐用 DocumentFragment 来缓存,最后一次性插入 body,从而减少浏览器的渲染,提高性能,不过最近也发现一个 bug: Docume ...
- dao---service---action分层结构
此文转载于http://blog.csdn.net/jay198746/article/details/4698709 之前有看过一些ssh2中采用dao---service---action分层结构 ...
- WebSocket之获取HttpSession
WebSocket之获取HttpSession 由于WebSocket与Http协议的不同,故在使用常用的HttpSession方面就存在了一些问题.通过google翻阅到了在onOpen方法下使用H ...
- Golang学习-第一篇 Golang的简单介绍及Windows环境下安装、部署
序言 这是本人博客园第一篇文章,写的不到位之处,希望各位看客们谅解. 本人一直从事.NET的开发工作,最近在学习Golang,所以想着之前学习的过程中都没怎么好好的将学习过程记录下来.深感惋惜! 现在 ...
- ORACLE内存结构:PGA And UGA,ORACLE用户进程、服务器进程
执行一个SQL语句 执行查询语句的过程: 用户进程执行一个查询语句如select * from emp where empno=7839 用户进程和服务器进程建立连接,把改用户进程的信息存储到PGA的 ...
- [iOS微博项目 - 4.2] - 设置转发微博背景
github: https://github.com/hellovoidworld/HVWWeibo A.转发微博部分的淡灰色背景 1.需求 转发微博部分需要设置背景色 使用图片作为背景 2.思路 ...
- OSharp DbContent初始化分析
DBContent初始化 —— 关联Entity查找 一. 关联到具体的Entity 二. 通过EntityTypeConfiguration 关联到DbContent 三. ...
- php 乘除法原理
w $wdays = ceil(($wmaxutime-$wminutime)/(24*3600)); $wdays = ceil(($wmaxutime-$wminutime)/243600); 二 ...
- C++应该被看成是个语言集合——四种语言(C语言,OO语言,泛型语言,STL)
至少有三种语言: 一,C++ is C 二,C++ is an OO language 三,C++ is a genetic programming language 有的童鞋觉得难,可能是没有看清楚 ...