C语言/C++从入门到精通之用 C 语言画「心形」
小职 2021-01-19 来源 : 阅读 1765 评论 0

摘要:本文主要向大家介绍了C语言/C++从入门到精通之用 C 语言画「心形」,通过具体的内容向大家展现,希望对大家C语言/C++的学习有所帮助。

本文主要向大家介绍了C语言/C++从入门到精通之用 C 语言画「心形」,通过具体的内容向大家展现,希望对大家C语言/C++的学习有所帮助。

C语言/C++从入门到精通之用 C 语言画「心形」

在我们IT行业每天面对的就是敲代码,所以很多人无法接受这份工作,因为很无聊也很枯燥,长期工作会使人情绪低落,其实我们编程很多时候也有有趣的地方,接下来我就用一个简单的c语言作图来缓解一下气氛。


新的一年开始了,是时候作出改变了。


以下为用C语言画心形的三种方式(附代码)


画心1

C语言/C++从入门到精通之用 C 语言画「心形」

C语言/C++从入门到精通之用 C 语言画「心形」

关于%*.*s

小数点.后“*”表示输出位数,具体的数据来自参数表

printf格式字符串中,与宽度控制和精度控制有关的常量都可以换成变量,方法就是使用一个“*”代替那个常量,然后在后面提供变量给“*”。


同样,小数点.前也可以添加*,也要用户输入一个位宽值来代替,表示输出的字符所占位宽。

也就是说,前面定义输出总宽度,后面定义输出字符个数。

printf("%*.*s\n", 50, 3, a); // 50表示此次输出占位宽,

//3表示输出a数组的三个字符


画心2

C语言/C++从入门到精通之用 C 语言画「心形」

C语言/C++从入门到精通之用 C 语言画「心形」

画心3

C语言/C++从入门到精通之用 C 语言画「心形」

Linux上通过framebuffer将jpeg图片画在屏幕上


安装JPEG库

1.解压jpeg源码 tar -xzvf jpegsrc.v8a.tar.gz

2.在/home/xxx下新建jpeg目录 mkdir jpeg

3.进入jpeg源码目录jpeg-8a cd jpeg-8a

4.生成makefile脚本 ./configure --prefix=/home/xxx/jpeg

5.编译 make

6.安装 make install

安装完毕后就可以在/home/xxx/jpeg目录下看到 jpeg解码库


配置JPEG库


#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <fcntl.h>

#include <linux/fb.h>

#include <sys/mman.h>

#include <sys/ioctl.h>

#include <errno.h>

#include <string.h>

#include "jpeglib.h"

typedef struct Tag_RGB

{

unsigned char ucRed;

unsigned char ucGreen;

unsigned char ucBlue;

}St_RGB;

int main (int agrc, char *argv[])

{

int fp=0;

struct fb_var_screeninfo vinfo;

struct fb_fix_screeninfo finfo;

fp = open("/dev/fb0",O_RDWR);

if (fp < 0)

{

printf("Error : Can not open framebuffer device\n");

exit(EXIT_FAILURE);

}

if (ioctl(fp,FBIOGET_FSCREENINFO,&finfo))

{

printf("Error reading fixed information\n");

exit(EXIT_FAILURE);

}

if (ioctl(fp,FBIOGET_VSCREENINFO,&vinfo))

{

printf("Error reading variable information\n");

exit(EXIT_FAILURE);

}

printf("The mem is :%d\n", finfo.smem_len);

printf("The line_length is :%d\n", finfo.line_length);

printf("The xres is :%d\n", vinfo.xres);

printf("The yres is :%d\n", vinfo.yres);

printf("bits_per_pixel is :%d\n", vinfo.bits_per_pixel);

unsigned long screensize = 0;

screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;

//这就是把fp所指的文件中从开始到screensize大小的内容给映射出来,

//得到一个指向这块空间的指针

unsigned char *fbp =(unsigned char *)mmap(0, screensize, PROT_READ|PROT_WRITE

, MAP_SHARED, fp, 0);

if (fbp == (unsigned char*)-1)

{

printf ("Error: failed to map framebuffer device to memory./n");

exit (EXIT_FAILURE);

}

unsigned int location = 0;

struct jpeg_decompress_struct jinfo;

struct jpeg_error_mgr jerr;

/*Bind error handler*/

jinfo.err = jpeg_std_error(&jerr);

/*init jpeg decompress object*/

jpeg_create_decompress(&jinfo);

/*Bind jpg data object*/

FILE * pfJPG = fopen(argv[1], "rb");

if(NULL == pfJPG)

{

printf("open %s failed. error->%s\n", argv[1], strerror(errno));

jpeg_destroy_decompress(&jinfo);

}

else

{

jpeg_stdio_src(&jinfo, pfJPG);

jpeg_read_header(&jinfo, TRUE);

/*Start decompressing*/

jpeg_start_decompress(&jinfo);

/*Only get decompress arguments*/

//jpeg_calc_output_dimensions(&jinfo);

printf("Output Width = %d\n",jinfo.output_width);

printf("Output Height = %d\n",jinfo.output_height);

//Color Channel

printf("Output Components = %d\n",jinfo.output_components);

unsigned char** ppucRowData = NULL;

ppucRowData = (*jinfo.mem->alloc_sarray)((j_common_ptr) &jinfo, JPOOL_IMAGE

, jinfo.output_width * jinfo.output_components, 1);

unsigned int i = 0;

unsigned int j = 0;

St_RGB stColor = {0};

while (jinfo.output_scanline < jinfo.output_height) //jinfo.output_height

{

jpeg_read_scanlines(&jinfo, ppucRowData, 1);

for (i=0; i<jinfo.output_width; i++)

{

location = i*(vinfo.bits_per_pixel/8)+j*finfo.line_length;

stColor.ucRed = ppucRowData[0][i*jinfo.output_components];

stColor.ucGreen = ppucRowData[0][i*jinfo.output_components+1];

stColor.ucBlue = ppucRowData[0][i*jinfo.output_components+2];

/*直接赋值来改变屏幕上某点的颜色*/

*(fbp + location) = stColor.ucBlue;

*(fbp + location + 1) = stColor.ucGreen;

*(fbp + location + 2) = stColor.ucRed;

*(fbp + location + 3) = 0; /*是否透明*/

}

j++;

}

/*Finish Decompress*/

jpeg_finish_decompress(&jinfo);

/*Destroy Decompress Object*/

jpeg_destroy_decompress(&jinfo);

fclose(pfJPG);

}

munmap (fbp, screensize); /*解除映射*/

close (fp);

return 0;

}

编译:gcc heart.c -o ourheart -ljpeg

运行:./ourheart



关注“职坐标在线”(Zhizuobiao_Online)公众号,免费获取学习教程资料、技术就业咨询

C语言/C++从入门到精通之用 C 语言画「心形」



本文由 @小职 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程