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 ...
随机推荐
- sql列转行查询
test表: 执行列转行sql: select student, sum(case Course when '语文' then Score else null end) 语文, sum(case Co ...
- Android之ListView中的分割线
ListView中每个Item项之间都有分割线,设置android:footerDividersEnabled表示是否显示分割线,此属性默认为true. 1.不显示分割线只要在ListView控件中添 ...
- Cognos组织架构介绍
Cognos只是一个工具,说到Cognos相信大部分人都知道BI(商业智能,Business Intelligence). Cognos也是属于SOA架构,面向服务的体系结构,是一个组件模型,它将应用 ...
- kafka基础概念
kafka介绍 kafka is a distributed, partitiononed,replicated commited logservice. kafka是一个分布式的.易扩展的.安全性高 ...
- MySQL按照汉字的拼音排序、按照首字母分类
项目中有时候需要按照汉字的拼音排序,比如联系人列表.矿物分类等,有的还需要按拼音字母从A到Z分类显示. 如果存储汉字的字段编码使用的是GBK字符集,因为GBK内码编码时本身就采用了拼音排序的方法(常用 ...
- Hibernate-sessio缓存的操作
首先咋们看一个图: flush:首先箭头是由缓存指向数据库,即当我调用 Session.flush()方法时它会强制使数据库的记录跟缓存 中的对象状态保持同步 ,如果不一致,就会发送Sql语句 ,保持 ...
- python egg包免安装直接使用
import sys egg_path='egg.egg' sys.path.append(egg_path) import egg_sample
- 七牛云存储--内存put示例(go sdk)
啥都不说了,居然有文档,有git为啥不提供example? 自己看代码,琢磨了一下,原来是要这么用的.这里不得不吐槽一下package的命名,为啥要去io?golang自带系统包名就有io啊,哥哥. ...
- Python 之父谈放弃 Python:我对核心成员们失望至极!
Python 之父讲述退位原因,以及 Python 的未来将何去何从. 在 Python 社区,Python 的发明者 Guido Van Rossum 被称为 “仁慈的终生独裁者”(BDFL,B ...
- JDK动态代理实现源码分析
JDK动态代理实现方式 在Spring框架中经典的AOP就是通过动态代理来实现的,Spring分别采用了JDK的动态代理和Cglib动态代理,本文就来分析一下JDK是如何实现动态代理的. 在分析源码之 ...