c语言版本的
在创建文件流时,可以显示指定它的打开方式为ios::binary,也就是以二进制方式打开。但是,无论是否指定二进制方式打开文件,读写的最小单位都是字节。那么,它到底起到什么作用呢?
首先,介绍一下二进制方式打开与普通打开方式的区别,两者大的区别在于对换行符的处理方式不同。由于历史原因,Windows操作系统是用两个字符(\r\n)来表示换行符的;而Unix操作系统却是用单个字符(\n)来表示换行符的。因此,在创建文件流时,如果指定了以ios::binary方式打开,那么换行符就是单字符的;否则,就采用Windows操作系统的双字符。
总结来说,以ios::binary方式打开,换行符被解释成\n;反之,换行符被解释成\r\n。
所以为了兼容性,通常在Windous系统中通常使用ios::binary方式打开图像文件;在Unix(或类Unix)操作系统中,指定和不指定ios::binary方式没有区别。
#include // ifstream, ifstream::in
#include
#define MAX 1024*1024
using namespace std;
// 将内存中的二进制文件写成一张图片
bool write_image(const string& filename, char *pBuffer, int length){
ofstream fout(filename, ios::binary);
if(!fout){
printf("can't open = %s\n", filename.c_str());
return false;
}
fout.write(pBuffer,length);
fout.close();
return true;
}
bool write_image2(const string& filename, char *image_buf, int image_len){
int fd = open(filename.c_str(), O_CREAT | O_WRONLY | O_TRUNC);
if (fd > 0) {
int n = 0, m = 0;
do {
m = write(fd, image_buf + n, image_len - n);
n += m;
} while (n < image_len);
close(fd);
}
}
// 将磁盘中的二进制文件读到内存中
bool read_image(const string& filename, char * pBuffer, int *out_length){
ifstream fin(filename, ifstream::in | ios::binary);
if(!fin){
printf("can't open = %s\n", filename.c_str());
return false;
}
// 求图片长度
fin.seekg(0, std::ifstream::end); //将文件流指针定位到流的末尾
int length = fin.tellg();
fin.seekg(0, std::ifstream::beg); //将文件流指针重新定位到流的开始
if(length <= 0 || length > MAX){
printf("length = %d\n", length);
return false;
}
// 读取
*out_length = length;
fin.read(pBuffer, length);
fin.close();
return true;
}
int main(){
// 读图片
char * pBuffer = new char[MAX];
int length = 0;
bool succ = read_image( R"(D:\workspace\binarytree\test.jpg)", pBuffer, &length);
if(succ){
write_image(R"(D:\workspace\binarytree\save.jpg)", pBuffer, length);
write_image(R"(D:\workspace\binarytree\save.png)", pBuffer, length);
write_image(R"(D:\workspace\binarytree\save.txt)", pBuffer, length);
write_image(R"(D:\workspace\binarytree\save.bmp)", pBuffer, length);
write_image(R"(D:\workspace\binarytree\save.raw)", pBuffer, length);
}
delete [] pBuffer;
return 0;
}
int main11(){
// 读图片
char pBuffer[MAX] = {0};
int length = 0;
bool succ = read_image( R"(D:\workspace\binarytree\test.jpg)", pBuffer, &length);
if(succ){
write_image(R"(D:\workspace\binarytree\save.jpg)", pBuffer, length);
write_image(R"(D:\workspace\binarytree\save.png)", pBuffer, length);
write_image(R"(D:\workspace\binarytree\save.txt)", pBuffer, length);
write_image(R"(D:\workspace\binarytree\save.bmp)", pBuffer, length);
write_image(R"(D:\workspace\binarytree\save.raw)", pBuffer, length);
}
return 0;
}
等待研究
https://www.cnblogs.com/lyj-blogs/p/10178384.html
int main()
{
cv::Mat mat = cv::imread("/home/oceanstar/image/aa.jpg");
if(mat.empty())
{
cout << "图像不能加载!"<< endl;
return -1;
}
imshow("MyWindow", mat);
//等待直到有键按下
waitKey(0);
return 0;
}
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
const int MAX = 1024 * 1920 * 3;
bool write_image(const string& filename, char *pBuffer, int length){
ofstream fout(filename, ios::binary);
if(!fout){
printf("can't open = %s\n", filename.c_str());
return false;
}
fout.write(pBuffer,length);
fout.close();
return true;
}
int main()
{
//3通道图像
cv::Mat img2 = cv::imread("/home/oceanstar/image/aa.jpg");
if(img2.empty())
{
cout << "图像不能加载!"<< endl;
return -1;
}
char p[MAX]; //char数组
std::vector<uchar> buff;
cv::imencode(".jpg", img2, buff);
memset(p, 0, MAX);
memcpy(p, reinterpret_cast<char*>(&buff[0]), buff.size());
int length = buff.size();
char * buffer = reinterpret_cast<char *>(p);
write_image("/home/oceanstar/image/vvvv.jpg", buffer, length );
return 0;
}
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
const int img_width=1920; /** 图像的宽度*/
const int img_height = 1024; /**图像的高度*/
/**将uchar类型的转换为Mat类型*/
Mat ucharToMat(const uchar *p2)
{
Mat img(Size(img_width, img_height),CV_8UC3);
for (int i = 0; i < img_width * img_height * 3; i++)
{
img.at<Vec3b>(i / (img_width * 3), (i % (img_width * 3)) / 3)[i % 3] = p2[i];
}
return img;
}
/**数组,存放图片大小*/
///uchar p1[img_width * img_height * 3];
/**将Mat类型的数据转换为uchar类型*/
uchar* matToUchar(Mat img, uchar *p1)
{
for (int i = 0; i < img_width * img_height * 3; i++)
{
p1[i]= (uchar)img.at<Vec3b>(i / (img_width * 3), (i % (img_width * 3)) / 3)[i % 3];
}
return p1;
}
int main()
{
//3通道图像
cv::Mat img2 = cv::imread("/home/oceanstar/image/aa.jpg");
if(img2.empty())
{
cout << "图像不能加载!"<< endl;
return -1;
}
/**定义一个数组存放matToUchar的返回值*/
auto *p = new uchar[img_width * img_height * 3];
matToUchar(img2, p);
/**下面这两行代码目的是输出Mat转化为uchar的值是否正确
@(int)目的是将uchar类型的数据转换为int可以直观的看到,
uchar的数据输出的是乱码
*/
for (int i = 0; i < img_width * img_height * 3; i++){
cout << (int)p[i] << endl;
}
Mat img = ucharToMat(p);
imshow("img",img);
waitKey(0);
return 0;
}
https://blog.csdn.net/Black_Friend/article/details/89372976
cv::Mat mat = cv::imread("d:\\1.jpg");
std::string str;
std::vector<unsigned char> buff;
cv::imencode(".jpg", mat, buff);
str.resize(buff.size());
memcpy(&str[0], buff.data(), buff.size());
https://www.cnblogs.com/mdumpling/p/8179167.html