下面测试代码只需要全部放在一个.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,再保存的更多相关文章

  1. ios手机竖屏拍照图片旋转90°问题解决方法

    手机拍照会给图片添加一个Orientaion信息(即拍照方向),如下: 用ios手机拍照,系统会给图片加上一个方向的属性, ios相机默认的拍照方向是后摄Home键在右为正,前摄Home键在左为正. ...

  2. Winform中使用FastReport的PictureObject时通过代码设置图片源并使Image图片旋转90度

    场景 FastReport安装包下载.安装.去除使用限制以及工具箱中添加控件: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  3. opencv图片旋转90度

    #include<iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv ...

  4. CSS使jsp图片旋转90度

    <style > img{ margin:100px auto 0; -moz-transform:rotate(-90deg); -webkit-transform:rotate(-90 ...

  5. 关于android中调用系统拍照,返回图片是旋转90度

    转载博客:http://blog.csdn.net/walker02/article/details/8211628 项目开发中遇到的一个问题,对于三星手机在做手机照片选择时出现图片显示不正常,研究后 ...

  6. iOS 图片旋转方法

    iOS 图片旋转方法 通过 CGImage 或 CIImage 旋转特定角度 UIImage可通过CGImage或CIImage初始化,初始化方法分别为init(cgImage: CGImage, s ...

  7. iOS_UIImage_图片旋转

    一.目的: 有时候我们获得到的图片我们不是我们想要的方向,需要对图片进行旋转.比如:图片旋转90度180度等. 二.实现过程. 1.获取到该UIImage. 2.开启上下文. 3.上下文的具体操作. ...

  8. ffmpeg第五篇:让水印图片旋转起来

    这篇把上次挖的坑填上 ffmpeg正式篇的上一篇(传送门)说了,这一篇要让水印旋转起来,但是后面有事情一直没有时间搞,今天,它来了............ 如果想实现旋转的功能,需要使用ffmpeg过 ...

  9. windows phone 摄像头得到图片是旋转90°

    我上个随笔讲到,windows phone 拍出来的photo如果直接使用是反转了90°的. 研究了很久..终于发现问题.其实..这是使用习惯问题... CameraCaptureUI 得到的phot ...

随机推荐

  1. Jmeter--压测dubbo接口

    Dubbo Interface Demo:https://blog.csdn.net/qi_lin7/article/details/53759528 Demo2:https://blog.csdn. ...

  2. Python巨蟒全栈开发目录

    巨蟒python全栈开发-第一阶段 基础数据类型&基础 1.什么是计算机&&python的简介(待补充) 2.while循环&&格式化输出&&运 ...

  3. Python目录整合

    一.python基础篇 二.网络编程篇&&并发编程篇 三.数据库篇 -mysql -redis -mongodb 四.前端篇 -html -css -js jquery&&am ...

  4. 为ElasticSearch添加HTTP基本认证 https://github.com/Asquera/elasticsearch-http-basic/releases

    ES的HTTP连接没有提供任何的权限控制措施,一旦部署在公共网络就容易有数据泄露的风险,尤其是加上类似elasticsearch-head这样友好的前端界面,简直让你的数据瞬间裸奔在黑客的眼皮底下.项 ...

  5. linux devcie lspci,lscpu,blkdiscard,fstrim,parted,partprobe,smartctl

    blkdiscard/sparse/thin-provisioned device,like ssdfstrim--- discard unused blocks on a mounted files ...

  6. 【转】浏览器Request Header和Response Header的内容

    1)请求(客户端->服务端[request])     GET(请求的方式) /newcoder/hello.html(请求的目标资源) HTTP/1.1(请求采用的协议和版本号)     Ac ...

  7. python学习笔记(四)— 函数

    一.函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过程或子程序),在Pasc ...

  8. 设计模式C++实现——适配器模式

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/walkerkalr/article/details/29863177 模式定义:         适 ...

  9. Spark如何写入HBase/Redis/MySQL/Kafka

    一些概念 一个partition 对应一个task,一个task 必定存在于一个Executor,一个Executor 对应一个JVM. Partition 是一个可迭代数据集合 Task 本质是作用 ...

  10. Android Studio Tips

    1. 可以通过ctrl+shift+a,然后输入reformat,就能看到对应的快捷键. 如果记不得快捷键了,都可以通过ctrl+shift+a来查找. 2. [Androidstudio]的坑之[@ ...