用c语言读一张图片
来源:互联网 发布:react.js阮一峰 编辑:程序博客网 时间:2024/06/05 09:27
我觉得写的很好,网上有一模一样的代码,感谢分享。
/* *This program will open a image,and output to another file *Author:hujian in nankai 2016/3/29*/#include <cstdio>#include <iostream>using namespace std;#define __FILENAME "./img/in.jpg" //this is the default image,if no input filename,the image will be usedint main(){ //the fp is the file stream of reading data //the out is the file stream of writing to file(hex) //the dst file is the file stream of dest image // FILE *fp, *out, *dstFile; //this is the image size //we will use fseek and ftell to get the size // long int size; unsigned char ch, ch1, ch2; //get the higt bites and low bites // unsigned char high, low; //this is the image name buffer // char pic_name[30]; memset(pic_name, 0, sizeof(pic_name)); //we can input the file's name,and the program will open the image and do some //oprtation,such as read data,write from image data.. ////#define INPUT#ifdef INPUT //if we want to input the filename ,just define the input. printf("\nPlease input the image's name:"); cin >> pic_name;#else //ok,you don't want to input strcpy_s(pic_name,__FILENAME);#endif INPUT printf_s("The filename is:%s\n", pic_name); //the follow lines will open the file // { //open the src image for reading the image data // fopen_s(&fp, pic_name, "rb"); //open the text stream(write) // fopen_s(&out,"res.txt", "wb+"); //open the dest image file stream // fopen_s(&dstFile,"./img/res.jpg", "wb+"); //ok,check the file stream // { if (!fp || !out || !dstFile){ printf_s("Can not open the file you input,check the filename and re-try\n"); //exit(EXIT_FAILURE); } } }//end of open file,now we get the file stream done. //we want to get the size of the image,we just use the fseek and ftell get the size // fseek(fp, 0, SEEK_END); size = ftell(fp); //printf the size printf("get the size is: %d\n", size); //rewind means jump to start of the file // rewind(fp); //traversal the image data // long int i; for (i = 0; i<size; i++) { ch = fgetc(fp); high = ch >> 4; low = ch & 0x0f; if (high < 10) high += '0'; else high += 55; if (low < 10) low += '0'; else low += 55; //write to file stream out // fputc(high, out); fputc(low, out); fputc(' ', out); //16 one line // if (i % 16 == 0) fprintf(out, "\r\n"); } //jump to start of the image file // rewind(fp); bool FormatRight = false; for (i = 0; i<size; i++) { ch1 = fgetc(fp); if (ch1 == 0xff) { ch2 = fgetc(fp); if (ch2 == 0xd8) { fputc(ch1, dstFile); fputc(ch2, dstFile); FormatRight = true; break; } } } if (FormatRight) { for (; i<size; i++) { ch1 = fgetc(fp); fputc(ch1, dstFile); if (ch1 == 0xff) { ch2 = fgetc(fp); if (ch2 == 0xd9) break; else fputc(ch2, dstFile); } } } //close the image file stream // fclose(fp); fclose(out); fclose(dstFile); printf_s("--operation done--\n"); return 0;}
0 0
- 用c语言读一张图片
- c语言最重要的一张!
- 【Unity】用Unity加载电脑C盘中的一张图片
- halcon13初探,显示一张图片(c++)
- C语言读BMP图片
- 用MFC显示一张图片
- 用NSCache存取一张图片
- 用flex将两张图片合成一张图片
- 一张图片
- 一张图片
- 图片一张
- 一张图片
- 图片一张
- 一张图片
- 最简单的openCV语言,读取和显示一张图片
- 用curl下载一张图片并保存
- 用代码分割一张图片的方法
- 用一个颜色值生成一张图片
- OpenGL基础图形编程(二)OpenGL概念建立
- 第六周项目四-静态成员应用
- 【HEVC学习与研究】38、HEVC编码过程中的块分割结构
- 数据挖掘的基本任务与要解决的问题(数据挖掘入门)
- 【Unity&C#】运行在主线程的Socket
- 用c语言读一张图片
- 同步和互斥
- 笔试携程经验
- Tsinsen A1127 方格取数
- hadoop启动后,长久不用,无法关闭
- Mysql 不同版本 说明
- SQL语言基础入门总结
- javaEE
- JAVA常用运算符极其运算优先级的总结